Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 向数据表添加额外列_Javascript_Datatables - Fatal编程技术网

Javascript 向数据表添加额外列

Javascript 向数据表添加额外列,javascript,datatables,Javascript,Datatables,我需要向datatable添加列(标题)。在这个例子中,我有一个filter on period,如果我选择period从2016年1月到2016年5月,那么我需要像这样添加列 Buyer|January 2016 | February 2016 | April 2016 | May 2016 | 但当我选择从2016年1月到2016年6月的期限时 Buyer|January 2016 | February 2016 | April 2016 | May 2016 | June 2016

我需要向datatable添加列(标题)。在这个例子中,我有一个filter on period,如果我选择period从2016年1月到2016年5月,那么我需要像这样添加列

Buyer|January 2016 | February 2016 | April 2016 | May 2016 | 
但当我选择从2016年1月到2016年6月的期限时

Buyer|January 2016 | February 2016 | April 2016 | May 2016 | June 2016

有什么想法吗???我使用的是DataTables。

您可以使用类似于drawDataTable的函数,将月份包装在数组中,清除标题行,最后添加标题

(函数(){
var月数=[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’、‘七月’、‘八月’、‘九月’、‘十月’、‘十一月’、‘十二月’];
var sel1=document.querySelector('#sel1');
var sel2=document.querySelector('#sel2');
风险值年份=2016年;
var table=document.querySelector('table');
函数fillMonths(月,选择){
var-element=document.createElement('option');
element.textContent=月份;
选择.appendChild(元素);
}
月份。forEach(函数(月){
填写月份(月,sel1);
});
月份。forEach(函数(月){
填写月份(月,sel2);
});
函数drawDataTable(已选择){
var rest=months.slice(months.indexOf(选择[0]),months.indexOf(选择[1])+1);
var节点=table.rows[0];
while(nodes.children.length>1){
var cell=nodes.children[1];
节点。removeChild(单元);
}
rest.forEach(函数(月){
var header=document.createElement('th');
header.textContent=月份+“”+年份;
table.rows[0].appendChild(表头);
});
}
var monthSelected=[‘一月’、‘一月’];
drawDataTable(选定的月份);
sel1.addEventListener('change',函数(事件){
monthSelected[0]=event.target.value;
drawDataTable(选定的月份);
});
sel2.addEventListener('change',函数(事件){
monthSelected[1]=event.target.value;
drawDataTable(选定的月份);
});
})();
正文{
字体系列:“开放式Sans”,无衬线;
}
桌子{
边界塌陷:塌陷;
}
th{
边框:2件纯黑;
}

用顶针做的
选择日期
月
到
买主

您可以使用类似的函数,如drawDataTable,将月份包装在数组中,清除标题行,最后添加标题

(函数(){
var月数=[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’、‘七月’、‘八月’、‘九月’、‘十月’、‘十一月’、‘十二月’];
var sel1=document.querySelector('#sel1');
var sel2=document.querySelector('#sel2');
风险值年份=2016年;
var table=document.querySelector('table');
函数fillMonths(月,选择){
var-element=document.createElement('option');
element.textContent=月份;
选择.appendChild(元素);
}
月份。forEach(函数(月){
填写月份(月,sel1);
});
月份。forEach(函数(月){
填写月份(月,sel2);
});
函数drawDataTable(已选择){
var rest=months.slice(months.indexOf(选择[0]),months.indexOf(选择[1])+1);
var节点=table.rows[0];
while(nodes.children.length>1){
var cell=nodes.children[1];
节点。removeChild(单元);
}
rest.forEach(函数(月){
var header=document.createElement('th');
header.textContent=月份+“”+年份;
table.rows[0].appendChild(表头);
});
}
var monthSelected=[‘一月’、‘一月’];
drawDataTable(选定的月份);
sel1.addEventListener('change',函数(事件){
monthSelected[0]=event.target.value;
drawDataTable(选定的月份);
});
sel2.addEventListener('change',函数(事件){
monthSelected[1]=event.target.value;
drawDataTable(选定的月份);
});
})();
正文{
字体系列:“开放式Sans”,无衬线;
}
桌子{
边界塌陷:塌陷;
}
th{
边框:2件纯黑;
}

用顶针做的
选择日期
月
到
买主

如果您指的是dataTables:标题和数据紧密地联系在一起,您不能只动态添加新列。我建议您将数据集组织为包含所有月份,然后根据日期选择器或您使用的任何工具显示**/**隐藏相关列。如果您指的是数据表:标题和数据紧密联系在一起,您不能只动态添加新列。我建议您将数据集组织为包含所有月份,然后根据日期选择器或您正在使用的任何内容显示**/**隐藏相关列。