Can datatables sort a column with an input field?

前端 未结 6 1450
囚心锁ツ
囚心锁ツ 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:13

    The easiest way is to add a hidden span inside columns <span style="visibility:hidden">value of the input</span>

    0 讨论(0)
  • 2021-01-12 18:20

    Set an invisible div with the value before the input field.

    <tbody>
    <tr id="row_20" class="odd">
        <td id="refKey_20" style="text-align:center;" class="">
            1Y
        </td>
        <td class=" sorting_1">
            <div style="display:none;">160</div>
            <input type="text" value="160" size="3" name="item.Order" 
            maxlength="3" id="Order_20" >
        </td>
    
    </tr>
    <tr id="row_19" class="even">
        <td id="refKey_19" style="text-align:center;" class="">
            1X
        </td>
        <td class=" sorting_1">
            <div style="display:none;">150</div>
            <input type="text" value="150" size="3" name="item.Order" 
            maxlength="3" id="Order_19" >
        </td>
    </tr>
    </tbody>
    
    0 讨论(0)
  • 2021-01-12 18:24

    For versions of Datatables 1.10+ the names of some option variables have been changed and a new API introduced. Documentation here: http://datatables.net/examples/plug-ins/dom_sort.html.

    Here is a working version of the above accepted answer in 1.10+:

    $(document).ready(function () {
        var table = $('#example').DataTable({
            "columnDefs": [
                {
                    "orderDataType": "dom-input",
                    "targets": 0, // Just the first column
                },
            ],
        });
    });
    

    The custom sort function:

    $.fn.dataTable.ext.order['dom-input'] = function (settings, col) {
        return this.api().column(col, { order: 'index' }).nodes().map(function (td, i) {
            return $('input', td).val();
        });
    }
    
    0 讨论(0)
  • 2021-01-12 18:25

    If you decide to use the columns option where you are rending information from a JSON file you can easily add a hidden span on your render property. It appears as though DataTables looks for text to order and if it cannot find any, it will break. The example at https://datatables.net/examples/plug-ins/dom_sort.html uses a table that has already been rendered. Here is an example using an API:

    ...columns([{ 
            "data": "receivedDate",
            "render": function (data, type, row, meta)
            {
    
                if (data == "null")
                {
                    return "<input type='text' id='datepicker_" + meta.row + "' class='datepicker form-control' /><span class='hidden'><span>";
                }
                else
                {
                    return "<input type='text' id='datepicker_" + meta.row + "' class='datepicker form-control' value='" + moment(data).format("MM/DD/YYYY") + "'/><span class='hidden'>" + moment(data).format('MM/ DD / YYYY') + "</span>";
                }
            }
        }]);
    
    0 讨论(0)
  • 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"}]
    } );
    
    0 讨论(0)
  • 2021-01-12 18:35
    jQuery.fn.dataTableExt.oSort['textbox-asc'] = function (a, b) {
        var vala = $('#' + $(a).filter('input').attr('id')).val().toLowerCase();
        var valb = $('#' + $(b).filter('input').attr('id')).val().toLowerCase();
        if (vala === '')
            return 1;
        if (valb === '')
            return -1;
        return vala < valb ? -1 : vala > valb ? 1 : 0;
    };
    
    jQuery.fn.dataTableExt.oSort['textbox-desc'] = function (a, b) {
        var vala = $('#' + $(a).filter('input').attr('id')).val().toLowerCase();
        var valb = $('#' + $(b).filter('input').attr('id')).val().toLowerCase();
        if (vala === '')
            return 1;
        if (valb === '')
            return -1;
        return vala < valb ? 1 : vala > valb ? -1 : 0;
    };
    

    then use it like this

     $(datatable).dataTable({
                        "iDisplayLength": 50,
                        "bLengthChange": false,
                        "bPaginate": false,                        
                        "columns": [
                            null, { "sType": "textbox" }
                        ],
                    });
    
    0 讨论(0)
提交回复
热议问题