0
点赞
收藏
分享

微信扫一扫

Bootstrap datatable 更换数据源(data json 形式)

海牙秋天 2023-03-04 阅读 51


$(document).ready(function () {
var dataTable = $('.ajax-sourced').DataTable({
"dom": '<"toolbar">rtBp',
"autoWidth": false,
"pageLength": 50,
"processing": true,
"data":dataSet,
"buttons": [
{ text: 'Copy to Clipboard', extend: 'copy', className: 'btn btn-outline-secondary' },
{ text: 'Download as CSV / Excel', extend: 'csv', className: 'btn btn-outline-secondary', exportOptions: { orthogonal: 'export' } }
],
"columns": [
{
data: null,
render: function (data, type, row) {
return "";
}
},
{
data: 'date', render: function (data, type, row) {
if (type == "filter") {
return row.date;
} else {
return moment(row.date).format('MM/DD/YYYY');
}
}
},
{ data: 'name' },
{ data: 'name' },
{
data: 'category', render: function (data, type, row) {
return row && row.category ? row.category.join(',') : ''
}
},
{ data: 'amount', render: $.fn.dataTable.render.number( ',', '.', 2, '$' )},
{ data: 'amount', render: $.fn.dataTable.render.number( ',', '.', 2, '$' )},

],
responsive: {
details: {
type: 'column',
target: 'tr'
}
},
columnDefs: [{
className: 'control',
orderable: false,
targets: 0
}, {
type: "date",
targets: 1
}, {
className: "text-xs-right",
"targets": [5]
}],
initComplete: function () {
dataTableCustomInit();
}
});
$(document).ajaxComplete(function() { $('#spin').hide(); });

$('.card-content').on('click', function(e) {
const accountId = $(this).attr('data-accountId');
$(this).addClass('cardActive').siblings().removeClass('cardActive');
let currentDataSet = dataSet.filter((x) => {
return x.account_id == accountId;
});
$('.ajax-sourced').dataTable().fnClearTable();
if(currentDataSet && currentDataSet.length > 0){
$('.ajax-sourced').dataTable().fnAddData(currentDataSet);
}

});
});

datatable的class 名字ajax-sourced

举报

相关推荐

0 条评论