Searching in DataTables and giving column priority on the results

Searching in DataTables and giving column priority on the 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.

Sign In or Register to comment.