gpt4 book ai didi

javascript - 数据表:如何在 Ajax 重新加载后保持子行打开

转载 作者:行者123 更新时间:2023-11-30 16:42:17 26 4
gpt4 key购买 nike

我正在使用 Ajax 源代码生成表格。这个每 5000 毫秒刷新一次,当一个子行被打开时,它会被表格的重绘关闭。我怎样才能让这些打开?

我的代码:

    /* Formatting function for row details - modify as you need */
function format ( d ) {
// `d` is the original data object for the row
return '<p>Text in child row</p>';
}

$(document).ready(function() {
$('#table').DataTable( {
...
} );

var tr;
var row;

$('#table tbody').on('click', 'td.details-control', function () {

if (tr !== null && tr !== undefined && row !== null && tr !== undefined) {
row.child.hide();
tr.removeClass('shown');
}

tr = $(this).closest('tr');
row = table.row( tr );

if ( row.child.isShown() ) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
}
else {
// Open this row
row.child( format(row.data()) ).show();
tr.addClass('shown');
}

} );

$.fn.dataTable.ext.errMode = 'none';
var table = $('#table').DataTable();
setInterval( function () {
table.ajax.reload( function () {
if ( row.child.isShown() ) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
}
else {
if (tr.hasClass('shown')) {
// Open this row
row.child( format(row.data()) ).show();
tr.addClass('shown');
}
}
} );
}, 5000 );

$('table td .details-control').html('<button><i class="fa fa-plus"></i></button>');

} );

参见 Child rows exampleajax.reload()方法供引用。

  • 经过一些研究,我发现人们建议在 jQuery 中使用 cookie

最佳答案

要回答最初的问题(如何在 DataTable AJAX 重新加载时保持子行打开),请参阅以下实现。

我使用 cookie 来保持子行打开,我正在使用插件 js-cookie found here .

最好有一个唯一标识符作为表格的一列,以便重新打开的行是正确的。

$(function(){

var dt = $('#my_table').DataTable(...);
var reloadInterval = 10000; // milliseconds

// Add extra-info row
dt_add_details('my_table', 'details-control', formatCallback, 'id');
var reloadCallback = function(json){
dt_reopen_rows('my_table', formatCallback, 'id')
};

// Reload AJAX source every X seconds
setInterval(function(){
dt.ajax.reload(reloadCallback, false);
}, reloadInterval)
});

/**
* Format child row data.
*/
function formatCallback(d){
...
}

/**
* Show / Hide extra-info when clicking on the column identified by className.
* @param {String} selector - The HTML selector for the table.
* @param {String} className - The column class name that holds the extra-info.
* @param {Function} formatCallback - Function used to format the data of the
* child row.
* @param {String} cookieDataIndex - The data index to keep in cookie.
*/

function dt_add_details(selector, className, formatCallback, cookieDataIndex){
$(selector + ' tbody').on('click', 'td.' + className, function () {
var ckey = 'openRows_' + selector;
var openRows = Cookies.getJSON(ckey);

// Create cookie if never created
if (typeof openRows == 'undefined'){
Cookies.set(ckey, [], {'path': ''});
var openRows = Cookies.getJSON(ckey);
};

// Get current info
var tr = $(this).closest('tr');
var row = $(selector).DataTable().row(tr);
var id = row.data()[cookieDataIndex];

if (row.child.isShown()){
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');

// Remove opened row from cookie
var idx = openRows.indexOf(id);
openRows.splice(idx, 1);
Cookies.set(ckey, openRows, {path: ''});
}
else{
// Open this row
row.child(formatCallback(row.data())).show();
tr.addClass('shown');

// Add row 'id' field to cookie
if (openRows.indexOf(id) < 0){
openRows.push(id);
}
Cookies.set(ckey, openRows, {path: ''});
}
// console.log("Opened rows: " + Cookies.getJSON('openRows_' + selector))
});
}

/**
* Show / Hide extra-info when clicking on the column identified by className.
* @param {String} selector - The HTML selector for the table.
* @param {Function} formatCallback - Function used to format the data of the
* the child row.
* @param {String} cookieDataIndex - The data index to keep in cookie.
*/
function dt_reopen_rows(selector, formatCallback, cookieDataIndex) {
var ckey = 'openRows_' + selector;
var openRows = Cookies.getJSON(ckey);
if (!openRows)
return;
var table = $(selector).DataTable(); // existing DataTable
$(table.rows().nodes()).each(function (idx, tr) {
var row = table.row(tr);
var id = row.data()[cookieDataIndex]
if (openRows.indexOf(id) >= 0) {
// console.log("Id " + id + " found in previously opened row. Re-opening.")
$(tr).addClass("shown");
row.child(formatCallback(row.data())).show();
}
});
}

关于javascript - 数据表:如何在 Ajax 重新加载后保持子行打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31771733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com