gpt4 book ai didi

jquery - 如何在jquery DataTable中打开columnDefs

转载 作者:行者123 更新时间:2023-12-01 05:35:59 28 4
gpt4 key购买 nike

我在此链接中使用数据表来显示网格。 https://datatables.net/examples/basic_init/hidden_columns.html

我使用 (columnDefs.targets) 显示几个默认列,然后我添加了从此链接显示和隐藏该列的功能:

https://datatables.net/examples/api/show_hide.html

首先我加载的页面是正确的并显示默认列,当我尝试显示/隐藏时,它显示所有列而不是默认列,我不知道如何在那里只显示默认列。

这是我的代码:

    $(document).ready(function () {

var table = $('#DataLegal').DataTable({


"columnDefs": [
{
"targets": [ 4,5,6,7,8,9,10,14,15,16,17,18,19,20,21,22,23,24,25,26,27],
"visible": false
// "searchable": false
}

]
} );


//This is show/Hide part

var ms = $('#magicsuggest').magicSuggest({
// Converts our C# object in a JSON string.
data: @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(columns))
});


$(ms).on('selectionchange', function(e,m){


// Turn on columns
$.each(table.columns()[0], function(index) {

table.column(index).visible(true);
//here how I can only turned on the DefColumns?

});




// Turn off each column in the value array... Value = int[0,1, 2, ...]
$.each(this.getValue(), function(index, item) {

table.column(item).visible(false);
});


});

});

最佳答案

您是否尝试过存储该目标列表?

然后只更新每个函数中的列表?像这样的吗?

$(document).ready(function () {
var targetArr = [4,5,6,7,8,9,10,14,15,16,17,18,19,20,21,22,23,24,25,26,27];
var table = $('#DataLegal').DataTable({
"columnDefs": [{
"targets": targetArr,
"visible": false
// "searchable": false
}]
});

$(ms).on('selectionchange', function(e,m){
// Turn on columns
$.each(table.columns()[0], function(index) {
if($.inArray(item, targetArr)){
table.column(item).visible(true); //in case some values were false set all to true
} else {
table.column(item).visible(false);//in case some values were true set all to false
}
});

$.each(this.getValue(), function(index, item) {
table.column(item).visible(false);
});
});
});

关于jquery - 如何在jquery DataTable中打开columnDefs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34006473/

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