DataTables:禁用标题点击排序,仍然允许手动排序

时间:2013-11-18 23:46:49

标签: jquery datatables

我正在使用DataTables,并希望使用下拉菜单进行排序,而不是点击表格标题。我有下拉菜单排序功能,但我无法弄清楚如何禁用表头排序而不禁用下拉菜单排序。如何禁用 表头排序?

jsfiddle

的JavaScript

function update_sort() {
    var sort1 = $('#sort1').val();
    var sort2 = $('#sort2').val();
    var sort3 = $('#sort3').val();

    var sorting = [[sort1, 'asc']];

    if (sort2) {
        sorting.push([sort2, 'asc']);
    }

    if (sort3) {
        sorting.push([sort3, 'asc']);
    }

    var table_obj = $('table').dataTable();

    table_obj.fnDestroy();

    table_obj.dataTable({
        'bPaginate': false,
        'bFilter': false,
        'bInfo': false,
        'aaSortingFixed': sorting
    });
}

update_sort();

$('p select').change(function() {
    update_sort();
});

HTML

<p>
    Sort By:

    <select id="sort1">
        <option value="0">Column 1</option>
        <option value="1">Column 2</option>
        <option value="2">Column 3</option>
    </select>

    Then By:

    <select id="sort2">
        <option value="">---------</option>
        <option value="0">Column 1</option>
        <option value="1">Column 2</option>
        <option value="2">Column 3</option>
    </select>

    Then By:

    <select id="sort3">
        <option value="">---------</option>
        <option value="0">Column 1</option>
        <option value="1">Column 2</option>
        <option value="2">Column 3</option>
    </select>
</p>

<table>
    <thead>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
            <th>Column 3</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>A</td>
            <td>A</td>
            <td>C</td>
        </tr>
        <tr>
            <td>A</td>
            <td>C</td>
            <td>B</td>
        </tr>
        <tr>
            <td>B</td>
            <td>B</td>
            <td>A</td>
        </tr>
        <tr>
            <td>B</td>
            <td>A</td>
            <td>C</td>
        </tr>
        <tr>
            <td>C</td>
            <td>B</td>
            <td>A</td>
        </tr>
    </tbody>
</table>

1 个答案:

答案 0 :(得分:7)

您可以取消绑定点击处理程序:

table_obj.find("th").off("click.DT");

http://jsfiddle.net/bHKNQ/1