admin管理员组文章数量:1387422
Unlike in the example provided by documentation, I want to make column title span dynamically.
I have generated sample data
var data = [];
for (var i = 0; i < 4; ++i) {
for (var j = 0; j < 4; ++j) {
var dataRow = [];
dataRow.push ("10" + (i + 1));
dataRow.push ("A");
for (var k = 0; k < 8; ++k) {
dataRow.push ("B");
dataRow.push ("test");
}
data.push (dataRow);
}
}
tried to generate header via columnDefs
var columnDefs = [
{
title: "title",
targets: [0, 1]
}
];
for (i = 0; i < 8; ++i) {
columnDefs.push ({
title: "data" + i,
targets: [(i + 1) * 2, (i + 1) * 2 + 1]
});
}
and generated table
$("#table").DataTable({
columnDefs: columnDefs,
data: data,
rowsGroup: [
0
],
responsive: true,
paging: false,
searching: false,
fixedHeader: true,
fixedColumns: {
leftColumns: 2
},
scrollX: true,
scrollY: "200px",
scrolLCollapse: true,
info: false,
ordering: false
});
but table duplicated title on each column assigned by targets
field. Is there any way I can merge them (effectively making th
s have colspan
of 2)?
Demo
Unlike in the example provided by documentation, I want to make column title span dynamically.
I have generated sample data
var data = [];
for (var i = 0; i < 4; ++i) {
for (var j = 0; j < 4; ++j) {
var dataRow = [];
dataRow.push ("10" + (i + 1));
dataRow.push ("A");
for (var k = 0; k < 8; ++k) {
dataRow.push ("B");
dataRow.push ("test");
}
data.push (dataRow);
}
}
tried to generate header via columnDefs
var columnDefs = [
{
title: "title",
targets: [0, 1]
}
];
for (i = 0; i < 8; ++i) {
columnDefs.push ({
title: "data" + i,
targets: [(i + 1) * 2, (i + 1) * 2 + 1]
});
}
and generated table
$("#table").DataTable({
columnDefs: columnDefs,
data: data,
rowsGroup: [
0
],
responsive: true,
paging: false,
searching: false,
fixedHeader: true,
fixedColumns: {
leftColumns: 2
},
scrollX: true,
scrollY: "200px",
scrolLCollapse: true,
info: false,
ordering: false
});
but table duplicated title on each column assigned by targets
field. Is there any way I can merge them (effectively making th
s have colspan
of 2)?
Demo
Share Improve this question asked Feb 14, 2017 at 11:22 Kudayar PirimbaevKudayar Pirimbaev 1,3202 gold badges24 silver badges43 bronze badges 1-
I've tried using jQuery, but it just weirds out. I was using this:
$("#table thead th:first-child").attr('colspan',2);
– Ryan Commented Feb 14, 2017 at 11:41
1 Answer
Reset to default 5 +50To make the DataTable colspan
work requires a second header row, one that has a single header cell for each column.
Quote from https://datatables/examples/advanced_init/plex_header.html:
Note that each column must have at least one unique cell (i.e. a cell without colspan) so DataTables can use that cell to detect the column and use it to apply ordering.
But to answer your question on how to add headers with colspan
dynamically, you can do the following:
var headers = '<thead><tr><th colspan="2">Title</th><th colspan="2">1</th><th colspan="2">2</th><th colspan="2">3</th><th colspan="2">4</th><th colspan="2">5</th><th colspan="2">6</th><th colspan="2">7</th><th colspan="2">8</th></tr>';
headers += '<tr><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th></tr></thead>';
$("#table").append(headers);
$("#table").DataTable({...});
Updated demo: https://jsfiddle/pn4aLmpb/1/
To give it the correct appearance you may be able to apply a height of 0 to this second row. Note that display:none
won't work as the headers will no longer align with the row data. This is because behind the scenes DataTable cleverly generates several overlapping HTML tables to simulate the effects of frozen rows, columns and headings, so there is a distinct disconnect between the column data and the column headings.
本文标签: javascriptJQuery DataTable Set colspan of column title dynamicallyStack Overflow
版权声明:本文标题:javascript - JQuery DataTable: Set colspan of column title dynamically - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744496611a2609066.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论