Can datatables sort a column with an input field?

前端 未结 6 1490
囚心锁ツ
囚心锁ツ 2021-01-12 17:41

I am trying to make datatables sort my columns. The first column works okay as it\'s a simple number. However the next column is an input field. When I try to make that sort

6条回答
  •  滥情空心
    2021-01-12 18:29

    You should look at this example that explains how to do sorting on input fields. Basically you declare a sorting function

    /* Create an array with the values of all the input boxes in a column */
    $.fn.dataTableExt.afnSortData['dom-text'] = function  ( oSettings, iColumn )
    {
        var aData = [];
        $( 'td:eq('+iColumn+') input', oSettings.oApi._fnGetTrNodes(oSettings) ).each( function () {
            aData.push( this.value );
        } );
        return aData;
    }
    

    And then tell to your table to use that

    $('#example').dataTable( {
        "aoColumns": [
            null,
            { "sSortDataType": "dom-text" }
        ]
    } );
    

    or wit aoColumnDefs

    $('#example').dataTable( {
        "aoColumnDefs": [{ "sSortDataType": "dom-text" , aTarget: "yourclass"}]
    } );
    

提交回复
热议问题