[英]jquery Datatable manipulation causing the entire page to reload
I have a jQuery datatable that is initially populated using ajax call, and when i click anywhere on the table like pagination number or display length dropdown list, the whole page is reloaded indefinetely .我有一个 jQuery 数据表,该数据表最初是使用 ajax 调用填充的,当我单击表上的任何位置(如分页号或显示长度下拉列表)时,整个页面将无限重新加载。 Here is how i populate the datatable.这是我填充数据表的方式。
let table = $('#data-table').DataTable();
function populateTable(){
table = $('#data-table').DataTable({
destroy: true,
responsive: true,
serverSide: false,
autoWidth: false,
paging: true,
filter: true,
searching: true,
stateSave: true,
scrollX: true,
lengthMenu: [10, 25, 50, 75, 100],
language: {
"search": "Filtrer: "
},
ajax: {
url: '/Observer/GetActiveClientsByFloor',
type: 'POST',
data: {
FloorId: floorId,
Type: type
},
dataSrc: ''
},
columns: [
{
title: 'Zone',
data: 'LastKnownZone',
},
{
title: 'Hiérarchie Map',
data: 'MapInfo.mapHierarchyString',
},
{
title: 'Addresse MAC',
data: 'macAddress',
},
{
title: 'SSID',
data: 'ssId',
},
],
createdRow: (row, data, dataIndex, cells) => {
const selectedRowProfileId = $('#selectedRowProfileId', window.parent.document).val();
if (selectedRowProfileId !== '') {
if (data['ProfileId'] === selectedRowProfileId) {
$(row).addClass('selectedCustom');
}
}
},
initComplete: function (settings, json)
{
const response = json;
//Show the respone on other part of the page
}
}).order([[1, 'asc']]).draw(false);
}
I would like to know what could be causing page re-load and also know how to make pagination works.我想知道是什么导致页面重新加载,也想知道如何使分页工作。
You don't need to call order([[1, 'asc']]).draw(false)
after table initialization, just add您不需要在表初始化后调用order([[1, 'asc']]).draw(false)
,只需添加
order: [[1, 'asc']]
to your table properties, like this到你的表属性,像这样
$(document).ready(function(){
let table = $('#data-table').DataTable({
order: [[1, 'asc']],
//Other properties
});
As you are not using server-side
DataTables will make pagination automatically when you click pagination buttons, considering that all data has already been loaded in the first Ajax
call, but when serverSide
is set to true
every time you change the pagination a new Ajax
call will be made by datatables sending aditional parameters for pagination
, ordering
etc and you will need to change you backend query
, filters
and pagination
logic based on that params.由于您没有使用server-side
DataTables 将在您单击分页按钮时自动进行分页,考虑到所有数据已经在第一个Ajax
调用中加载,但是当每次更改分页时serverSide
设置为true
时,新的Ajax
调用将由数据表发送用于pagination
、 ordering
等的附加参数,您将需要根据该参数更改后端query
、 filters
和pagination
逻辑。
Edit: Also destroy: true
is not needed in your case, as Documentation says: "Destroy any existing table matching the selector and replace with the new options."编辑:还destroy: true
在您的情况下不需要,因为文档说:“销毁与选择器匹配的任何现有表并替换为新选项。” You are not re-creating or replacing your table, so you can just remove it您不是重新创建或替换您的表,因此您可以删除它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.