[英]JQuery DataTable: How to hide multple variable columns
我有一个数组 (colNumArray),其中包含我希望隐藏的列的索引,但我无法找到一种方法来循环遍历此数组并将这些索引的可见性设置为 false。 这是我的代码:
function runQuery(ProfileName) //this uses bootstraps data tables plugin
{
var columnsArray = [];
var toHideArray = [];
var colNumArray = [];
$.ajax({
type: "POST",
url: "sharedComponents.cfc",
data: { method: "checkHeaders",
default_sql_query_name: ProfileName
},
datatype: "json"
}).done(function(returnresult) {
var filteredResult = returnresult;
filteredResult = filteredResult.replace(/(\r\n|\n|\r)/gm,"");
filteredResult = filteredResult.trim();
var headersArray = filteredResult.split(','); //headersArray holds each of the values in the needed headers table that will be used as headers in the data table
// for(i of headersArray) //For every column in the return result add that column to the headers table
// {
// alert(i);
// }
$.ajax({
type: "POST",
url: "sharedcomponents.cfc",
data: { method: "runSelectedQuery",
sql_query: btoa($('#textareaQuery').val())
},
datatype: "json",
success: function(data) {
$.each(data.COLUMNS, function(index, item) { //index is the column array position, item is the value of the column array at [index] position
columnsArray.push(item);
});
for(i of columnsArray)
{
if(headersArray.includes(i)){
toHideArray.push(i);
}
}
$.each(data.COLUMNS, function(index, item) { //index is the column array position, item is the value of the column array at [index] position
if(toHideArray.includes(item)){
colNumArray.push(index);
}
});
}
}).done(function(returnresult) {
var tableLogData = $('#TripLogTable').DataTable({
columnDefs: [
{ targets: '-all', visible: true}/*,
{ targets: [colNumArray[0], colNumArray[1]], visible: false } */
]
});
tableLogData.clear();
console.log(colNumArray[0], colNumArray[4]);
console.log([colNumArray[0], colNumArray[1]]);
returnresult['DATA'].forEach( function (row) {
const rowData = {};
for (let i in row) {
rowData[i] = row[i];
}
tableLogData.row.add(rowData).draw( false );
})
});
});
}
我想你可能想看看大局,但更具体地说,问题就在这里:
var tableLogData = $('#TripLogTable').DataTable({
columnDefs: [
{ targets: '-all', visible: true},
{ targets: [colNumArray[0], colNumArray[1]], visible: false }
]
});
如您所见,我确实知道如何将一组索引的可见性设置为 false,但是如何将 colNumArray 数组中的每个数字的可见性设置为 false?
我想到了。 我不知道您可以在 columnDefs 之外执行此操作。 columnDefs 括号下方和 tableLogData.clear(); 这就是我所做的:
for(i of colNumArray){
tableLogData.column(i).visible(false);
}
正如您在上面看到的那样,tableLogData 是数据表的变量名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.