【问题标题】:Datattable sort function issue数据表排序功能问题
【发布时间】:2014-12-31 14:13:18
【问题描述】:

我的数据表无法在点击时通过“asc”和“desc”进行切换。当我摆脱表头单击功能时它工作正常,但是当像在提供的示例中那样添加它们时,在“asc”和“desc”之间切换的能力就会丢失。我的代码似乎干扰了默认数据表代码有什么答案或建议吗?

问题

我无法让表格标题在“asc”和“dec”之间切换,如果它确实有效,其他部分功能(例如搜索)将停止正常运行。

概览

我有一个按各个列搜索的表。您可以选择以两种方式之一搜索哪一列。

  1. 有一个下拉列表列出了所有列名,当您选择该选项时,相应的表格标题将排序为“asc”,然后如果您单击表格标题,您可以在“asc”和“desc”之间切换。

  2. 您也可以简单地点击表格标题,相应的选择选项就会被选中。表格标题也将始终以“asc”开头,您可以在单击时在“asc”和“desc”之间切换。

代码

JSFiddle - DEMO

$('#dtSelect').change(function () {
    var searchInput = $("#searchInput");

    if ($(this).val() == "0") {

        $("#searchBtn").on('click', function () {
            $("tbody tr td:nth-child(1):not(:contains('" + searchInput.val() + "'))").parent("tr").css("display", "none");
            $("tbody tr td:nth-child(1):contains('" + searchInput.val() + "')").parent("tr").css("display", "");
        });
    }

        else if ($(this).val() == "1") {

        $("#searchBtn").on('click', function () {
            $("tbody tr td:nth-child(2):not(:contains('" + searchInput.val() + "'))").parent("tr").css("display", "none");
            $("tbody tr td:nth-child(2):contains('" + searchInput.val() + "')").parent("tr").css("display", "");
        });
    }

        else if ($(this).val() == "2") {

        $("#searchBtn").on('click', function () {
            $("tbody tr td:nth-child(3):not(:contains('" + searchInput.val() + "'))").parent("tr").css("display", "none");
            $("tbody tr td:nth-child(3):contains('" + searchInput.val() + "')").parent("tr").css("display", "");
        });
    }

        else if ($(this).val() == "3") {

        $("#searchBtn").on('click', function () {
            $("tbody tr td:nth-child(4):not(:contains('" + searchInput.val() + "'))").parent("tr").css("display", "none");
            $("tbody tr td:nth-child(4):contains('" + searchInput.val() + "')").parent("tr").css("display", "");
        });
    }

        else if ($(this).val() == "4") {

        $("#searchBtn").on('click', function () {
            $("tbody tr td:nth-child(5):not(:contains('" + searchInput.val() + "'))").parent("tr").css("display", "none");
            $("tbody tr td:nth-child(5):contains('" + searchInput.val() + "')").parent("tr").css("display", "");
        });
    }

        else if ($(this).val() == "5") {

        $("#searchBtn").on('click', function () {
            $("tbody tr td:nth-child(6):not(:contains('" + searchInput.val() + "'))").parent("tr").css("display", "none");
            $("tbody tr td:nth-child(6):contains('" + searchInput.val() + "')").parent("tr").css("display", "");
        });
    }

});

$('#dtSelect').change(function () {
    var column = $(this).val();
    var oTable = $('#example').dataTable();
    if (column !== "") {
        oTable.fnSort([
            [column, 'asc']
        ]);
    }
});

$('th:nth-child(1):first').click(function () {
    $('#dtSelect').val(0).change();
});

$('th:nth-child(2):first').click(function () {
    $('#dtSelect').val(1).change();
});

$('th:nth-child(3):first').click(function () {
    $('#dtSelect').val(2).change();
});

$('th:nth-child(4):first').click(function () {
    $('#dtSelect').val(3).change();
});

$('th:nth-child(5):first').click(function () {
    $('#dtSelect').val(4).change();
});

$('th:nth-child(6):first').click(function () {
    $('#dtSelect').val(5).change();
});

【问题讨论】:

    标签: jquery datatable


    【解决方案1】:

    这是因为您正在链接 column header clickselecting in the drop down

    点击其中一个标题 - 比如说 Office - 它会触发 $('#dtSelect').change(function () 并将排序顺序设置为 ['Office', 'asc']

    更改以下行:

    if (column !== "") {
    

     if (column !== "" && event.currentTarget === this) {
    

    这将仅在更改下拉值时触发oTable.fnSort([[column, 'asc']]);,而不是在单击列标题时。

    jsfiddle:http://jsfiddle.net/_prakash/mu7t4e4y/19/

    【讨论】:

    • 这似乎有效,我将在我的实际代码中对其进行测试以确定。只是提醒你的 jsfiddle 不起作用我刚刚添加了建议的改变我自己。
    • 完美运行。如果你有机会,你能解释一下鳕鱼,这样我下次遇到这样的事情时可以理解。
    • 太棒了。请参阅 api.jquery.com/event.currenttarget 了解 event.currentTarget 的含义。本质上,通过在$('#dtSelect').change 块中添加event.currentTarget === this 条件,我是说只有在更改下拉值时调用下一步才会发生。而不是当从其他事件触发相同事件时 - 例如单击列标题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    • 2016-10-27
    • 2017-11-11
    相关资源
    最近更新 更多