0
我有一個數據表來自ajax的數據。我有一些輸入字段,當點擊一個按鈕時,我想讓數據表創建一個新的ajax請求並重新加載數據。datatable ajax過濾器重新加載數據
我:
<script>
var domain = [];
var subdomain = [];
var job_role = [];
var month = [];
var meta_activity = [];
$.fn.dataTableExt.oApi.fnFilterAll = function (oSettings, sInput, iColumn, bRegex, bSmart) {
var settings = $.fn.dataTableSettings;
for (var i = 0; i < settings.length; i++) {
settings[i].oInstance.fnFilter(sInput, iColumn, bRegex, bSmart);
}
};
$(document).ready(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
//Init select2 boxes
$("#domain").select2({
allowClear: true,
ajax: {
url: "{!! route('ajaxlistdomain') !!}",
dataType: 'json',
type: "GET",
processResults: function (data) {
return {
results: $.map(data, function (item) {
return {
text: item.text,
id: item.text
}
})
};
}
}
});
//Init select2 boxes
$("#subdomain").select2({
allowClear: true,
ajax: {
url: "{!! route('ajaxlistsubdomain') !!}",
dataType: 'json',
type: "GET",
processResults: function (data) {
return {
results: $.map(data, function (item) {
return {
text: item.text,
id: item.text
}
})
};
}
}
});
//Init select2 boxes
$("#jobrole").select2({
allowClear: true,
ajax: {
url: "{!! route('ajaxlistjobrole') !!}",
dataType: 'json',
type: "GET",
processResults: function (data) {
return {
results: $.map(data, function (item) {
return {
text: item.text,
id: item.text
}
})
};
}
}
});
//Init select2 boxes
$("#month").select2({
allowClear: true,
data:[
{ id: 'Jan', text: 'Jan' },
{ id: 'Feb', text: 'Feb' },
{ id: 'Mar', text: 'Mar' },
{ id: 'Apr', text: 'Apr' },
{ id: 'May', text: 'May' },
{ id: 'Jun', text: 'Jun' },
{ id: 'Jul', text: 'Jul' },
{ id: 'Aug', text: 'Aug' },
{ id: 'Sep', text: 'Sep' },
{ id: 'Oct', text: 'Oct' },
{ id: 'Nov', text: 'Nov' },
{ id: 'Dec', text: 'Dec' }
]
});
//Init select2 boxes
$("#metaactivity").select2({
allowClear: true,
ajax: {
url: "{!! route('ajaxlistmetaactivity') !!}",
dataType: 'json',
type: "GET",
processResults: function (data) {
return {
results: $.map(data, function (item) {
return {
text: item.text,
id: item.text
}
})
};
}
}
});
$("#update").click(function()
{
domain = [];
subdomain = [];
job_role = [];
month = [];
meta_activity = [];
$("#domain option:selected").each(function()
{
// log the value and text of each option
domain.push($(this).val());
});
$("#subdomain option:selected").each(function()
{
// log the value and text of each option
subdomain.push($(this).val());
});
$("#jobrole option:selected").each(function()
{
// log the value and text of each option
job_role.push($(this).val());
});
$("#month option:selected").each(function()
{
// log the value and text of each option
month.push($(this).val());
});
$("#metaactivity option:selected").each(function()
{
// log the value and text of each option
meta_activity.push($(this).val());
});
employeeActivityTable.ajax.reload();
projectActivityTable.ajax.reload();
}
);
var employeeActivityTable = $('#employeeActivityTable').DataTable({
ajax: {
url: "{!! route('ajaxactivityperemployee') !!}",
type: "POST",
data: {
'domain[]': domain,
'subdomain[]': subdomain,
'job_role[]': job_role,
'month[]': month,
'meta_activity[]': meta_activity
},
dataType: "JSON"
},
columns: [
{ data: 'employee_id', name: 'employee_id' },
{ data: 'employee_name', name: 'employee_name' },
{ data: 'month', name: 'month' },
{ data: 'sum_task_hour', name: 'sum_task_hour' }
],
columnDefs: [
{
"targets": [ 0 ],
"visible": false,
"searchable": false
}
]
});
var oTable0 = $("#table1").dataTable();
$("#Search_All").keyup(function() {
// Filter on the column (the index) of this element
oTable0.fnFilterAll(this.value);
});
var projectActivityTable = $('#projectActivityTable').DataTable({
ajax: {
"url": "{!! route('ajaxactivityperproject') !!}",
"type": "POST",
data: {
'domain[]': domain,
'subdomain[]': subdomain,
'job_role[]': job_role,
'month[]': month,
'meta_activity[]': meta_activity
},
dataType: "JSON"
},
columns: [
{ data: 'project_id', name: 'project_id' },
{ data: 'customer_name', name: 'customer_name' },
{ data: 'project_name', name: 'project_name' },
{ data: 'meta_activity', name: 'meta_activity' },
{ data: 'employee_id', name: 'employee_id' },
{ data: 'employee_name', name: 'employee_name' },
{ data: 'month', name: 'month' },
{ data: 'sum_task_hour', name: 'sum_task_hour' }
],
columnDefs: [
{
"targets": [ 0 ],
"visible": false,
"searchable": false
},
{
"targets": [ 4 ],
"visible": false,
"searchable": false
}
]
});
var oTable1 = $("#table1").dataTable();
$("#Search_All").keyup(function() {
// Filter on the column (the index) of this element
oTable1.fnFilterAll(this.value);
});
});
</script>
我曾嘗試也明確清晰的(),但什麼也沒有發生。另外,我沒有在控制檯中看到任何錯誤,它只是沒有做任何事情。
我也試着關閉MySQL數據庫,然後當我點擊按鈕時,從控制檯中的數據表中得到一個錯誤。這向我顯示它正試圖獲取新的數據,但它不會在屏幕上更新它們。
但是,這正是我所做的。如果你看看單擊#button時調用的函數,那麼最後確實有這樣的命令:employeeActivityTable.ajax.reload(); – Richard
我認爲我的問題來自我無法更改調用重新加載之前發送的ajax數據的事實...... – Richard
但是你已經超出了範圍。它們在一個函數中,它需要有一個全局變量。 – Derek