Javascript JQuery DataTable:如何隐藏多变量列
我有一个数组(colNumArray),它保存我想要隐藏的列的索引,但是我无法找到一种方法来循环这个数组并将这些索引的可见性设置为false。这是我的密码:Javascript JQuery DataTable:如何隐藏多变量列,javascript,jquery,datatables,Javascript,Jquery,Datatables,我有一个数组(colNumArray),它保存我想要隐藏的列的索引,但是我无法找到一种方法来循环这个数组并将这些索引的可见性设置为false。这是我的密码: function runQuery(ProfileName) //this uses bootstraps data tables plugin { var columnsArray = []; var toHideArray = []; var co
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是datatable的变量名