我有一个数据表,从json.php文件中获取数据。它工作正常,但现在我想要一个具有各种选项的选择框(例如:'今年,去年,开单,关闭账单,逾期等),选择发送ajax后变量data: {cmd: ($('#selected').val()}
到json.php(将优化所请求的记录)和ajax.table.reload();
以便显示新记录。
这是我到目前为止所尝试的:
<select class="form-control" name="selection" id="selection">
<option value="thisMon">This Month</option>
<option value="lasMon">Last Month</option>
<option value="thisYr">This Year</option>
<option value="lasYr">Last Year</option>
<option value="open">Open</option>
<option value="closed">Closed</option>
</select>
<script>
$(document).ready(function() {
$('#selection').change(function() {
var table = $('#datatable-buttons').DataTable({
"ajax": {
url: "scripts/json-expenses.php",
dataSrc: "",
data: { length: $('#selection').val() }
},
dom: "Bfrip",
"columns": [
{ data: "id", visible: false, searchable: false },
{ data: "expense_date"},
{ data: "supplier",
render: function(data, type, row){
return '<a href="/numbers/edit-expense.php?id='+ row.id +'">'+ data +'</a>';
}
},
{ data: "amount" },
{ data: "gst" },
{ data: "status",
render: function(data, type, row) {
if (row.due_date !== null) {
var dueDateArr = row.due_date.split("-");
var dueDate = new Date(dueDateArr[0], dueDateArr[1]-1, dueDateArr[2]);
var todayDate = new Date();
}
if (data == "Not Paid" && todayDate > dueDate) {
return '<label class="label label-danger">Overdue</label>';
}
if (data === "Paid") {
return '<label class="label label-success">'+ data +'</label>';
}
else if (data === "Not Paid") {
return '<label class="label label-default">'+ data +'</label>';
}
}
},
{ data: "due_date",
defaultContent: ""
}
],
"order": [ 1, 'desc' ],
paging: false,
fixedHeader: true
});
});
$('#selection').change(function(){
table.ajax.reload();
});
});
</script>
编辑:
它只能运行一次,并在ReferenceError: Can't find variable: table
的行上提供错误table.ajax.reload();
下次我选择其他选项时,数据表会给出错误:Cannot reinitialise DataTable.
答案 0 :(得分:3)
尝试更改此功能
$(document).ready(function(){
$('#datatable-buttons').DataTable({
"ajax": {
url: "scripts/json-expenses.php",
dataSrc: "",
data: { length: $('#selection').val() }
},
dom: "Bfrip",
"columns": [
{ data: "id", visible: false, searchable: false },
{ data: "expense_date"},
{ data: "supplier",
render: function(data, type, row){
return '<a href="/numbers/edit-expense.php?id='+ row.id +'">'+ data +'</a>';
}
},
{ data: "amount" },
{ data: "gst" },
{ data: "status",
render: function(data, type, row) {
if (row.due_date !== null) {
var dueDateArr = row.due_date.split("-");
var dueDate = new Date(dueDateArr[0], dueDateArr[1]-1, dueDateArr[2]);
var todayDate = new Date();
}
if (data == "Not Paid" && todayDate > dueDate) {
return '<label class="label label-danger">Overdue</label>';
}
if (data === "Paid") {
return '<label class="label label-success">'+ data +'</label>';
}
else if (data === "Not Paid") {
return '<label class="label label-default">'+ data +'</label>';
}
}
},
{ data: "due_date",
defaultContent: ""
}
],
"order": [ 1, 'desc' ],
paging: false,
fixedHeader: true
});
$('#selection').change(function(){
$('#datatable-buttons').dataTable().fnDestroy();
var table = $('#datatable-buttons').DataTable({
"ajax": {
url: "scripts/json-expenses.php",
dataSrc: "",
data: { length: $('#selection').val() }
},
dom: "Bfrip",
"columns": [
{ data: "id", visible: false, searchable: false },
{ data: "expense_date"},
{ data: "supplier",
render: function(data, type, row){
return '<a href="/numbers/edit-expense.php?id='+ row.id +'">'+ data +'</a>';
}
},
{ data: "amount" },
{ data: "gst" },
{ data: "status",
render: function(data, type, row) {
if (row.due_date !== null) {
var dueDateArr = row.due_date.split("-");
var dueDate = new Date(dueDateArr[0], dueDateArr[1]-1, dueDateArr[2]);
var todayDate = new Date();
}
if (data == "Not Paid" && todayDate > dueDate) {
return '<label class="label label-danger">Overdue</label>';
}
if (data === "Paid") {
return '<label class="label label-success">'+ data +'</label>';
}
else if (data === "Not Paid") {
return '<label class="label label-default">'+ data +'</label>';
}
}
},
{ data: "due_date",
defaultContent: ""
}
],
"order": [ 1, 'desc' ],
paging: false,
fixedHeader: true
});
});
)};
答案 1 :(得分:2)
在数据表中设置"destroy": true
答案 2 :(得分:1)
如果在函数中定义table
,它将仅在函数和子函数中定义。
在您的代码中,您在table
中定义了$('#selection').change(function() {
,并且您将在其他$('#selection').change(function() {
中使用它,因此不会在第二个函数中定义它。
还有一个错误,因为您使用$('#selection').change(function() {
两次并尝试多次添加数据表。
$(document).ready(function() {
//no need for change, just load table on document ready
var table = $('#datatable-buttons').DataTable({
"ajax": {
url: "scripts/json-expenses.php",
dataSrc: "",
data: { length: $('#selection').val() }
},
dom: "Bfrip",
"columns": [
{ data: "id", visible: false, searchable: false },
{ data: "expense_date"},
{ data: "supplier",
render: function(data, type, row){
return '<a href="/numbers/edit-expense.php?id='+ row.id +'">'+ data +'</a>';
}
},
{ data: "amount" },
{ data: "gst" },
{ data: "status",
render: function(data, type, row) {
if (row.due_date !== null) {
var dueDateArr = row.due_date.split("-");
var dueDate = new Date(dueDateArr[0], dueDateArr[1]-1, dueDateArr[2]);
var todayDate = new Date();
}
if (data == "Not Paid" && todayDate > dueDate) {
return '<label class="label label-danger">Overdue</label>';
}
if (data === "Paid") {
return '<label class="label label-success">'+ data +'</label>';
}
else if (data === "Not Paid") {
return '<label class="label label-default">'+ data +'</label>';
}
}
},
{ data: "due_date",
defaultContent: ""
}
],
"order": [ 1, 'desc' ],
paging: false,
fixedHeader: true
});
//on change reload table
$('#selection').change(function(){
table.ajax.reload();
});
});