Datatables search with priority for a certain column results

Datatables search with priority for a certain column results

tj26tj26 Posts: 11Questions: 6Answers: 0

Hello,

I have an ajax table with a search function. When I enter data in the search it will return items in the 2nd column that start with a Y. (Based on my previous question here https://datatables.net/forums/discussion/74176)

"columnDefs": [
        { 'visible': false, 'targets': [5, 6, 7] }, // columns.... not needed
        { "searchable": false, "targets": [2, 3, 5] },
        {
            "render": function (data, type, row) {
                return (Math.round(data * 100) / 100).toFixed(2);
            }, "targets": [2, 3]
        },
        { //hidden column to get the first letter of 2nd column so absoluteOrder plugin can be used to sort on searching so
            // when searching 2nd column items with a Y are displayed at the top, then alphabetically after that.
            targets: 7,
            data: null,
            visible: false,
            type: yOnTop,
            defaultContent: '',
            render: function (data, type, row) {
                return row[1][0];
            }
        }
        ],
    orderFixed: {
            'pre': [7, 'asc']
        },



         var yOnTop = $.fn.dataTable.absoluteOrder([{
                   value: 'Y',
                   position: 'top'
   }]);

The yOnTop function works, but what i would like to have a certain column have matching priority, so when I enter 'SU' in the search it matches the 2nd column first then matches the 1st column.

So currently when I put 'SU' into search I get this result:
SURFACE OPERATIONS | YBSO
BAROSUE DAM | YSCD
SURVEY SHIP | YCHN
BONSUELO | YCSL
PLATFORM | YUSU
SUNSHINE COAST |YBSU

but I would like it in this order (where column 2 matches SU before column 1), so result would be:
PLATFORM | YUSU
SUNSHINE COAST | YBSU
SURFACE OPERATIONS |YBSO
BAROSUE DAM | YSCD
SURVEY SHIP | YCHN
BONSUELO | YCSL

I altered orderFixed to this but it still didn't match 'SU' to the 2nd column...

orderFixed: {
                'pre': [[7, 'asc'], [ 1, 'asc' ]]
            },

Thank you very much for your help.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    Does your search algorithm also return ranking data for each entry? If so, is that in a column? That is what would be need to get this to work. Can you link to your page showing what you've got so for so I can take a look at the current state of play?

    Thanks,
    Allan

  • tj26tj26 Posts: 11Questions: 6Answers: 0

    Thanks Allan

    I have made an example here: https://live.datatables.net/nequluqo/6/edit

    So I will need another column called ranking... So if SU is in the 2nd column (Code) then a ranking of 1, in 1st column a ranking of 2, column 5 (Text) a ranking of 3.
    I am unsure how and where in my code I can put this processing.

    Thank you.

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    Does your search algorithm return a ranking result? That would be the first piece of information you need, since without it, it wouldn't be possible. What algorithm / package are you using for the search?

    From your original question I got the impression you were doing the search at the server-side and then displaying the information on the client-side (e.g. you might be using Elasticsearch) - is that actually the case, or are you just using the DataTables search? It doesn't provide ranking information, it isn't designed to provide a ranking method.

    Allan

  • tj26tj26 Posts: 11Questions: 6Answers: 0

    Allan,
    Just using DataTables.
    Is there no way to do it like I did yOnTop? Where value: 'search input', position: top ??

    Thanks.

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    No sorry. Our search algorithm does not include a ranking value. It is purely pattern matching and filtering. Then the order of the table is determined by the data in the table, with the absolute plug-in interacting with that.

    It needs something to sort on that indicates where it should sort to.

    Allan

Sign In or Register to comment.