【发布时间】:2014-12-18 14:01:08
【问题描述】:
我的表格列的升序和降序有问题。在表头单击上,我应该能够按我想要的方式按升序和降序进行排序,但是这段代码似乎将我限制为单击一次。
jsfiddle:DEMO
jQuery
(function () {
$('#example').dataTable({
"paging": false,
"filter": false,
"order": [
[0, "desc"]
]
});
})();
$('#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();
});
当我删除一些代码时,我可以根据需要按升序和降序进行排序。这让我相信,因为我在上面的代码中有 2 个更改事件,它们可能会相互干扰。
jsfiddle:DEMO
jQuery
(function () {
$('#example').dataTable({
"paging": false,
"filter": false,
"order": [
[0, "desc"]
]
});
})();
$('#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']
]);
}
});
【问题讨论】:
标签: jquery sorting datatables