how to shift columns mappings (because of inserted “row header column”) without redefining columns?
how to shift columns mappings (because of inserted “row header column”) without redefining columns?
RPokrovskij
Posts: 8Questions: 2Answers: 0
I have added "row header column" to my table:
HTML:
<table id="main-table" class="table table-hover table-striped mainTable w-100">
<thead>
<tr>
<th class="rowHeader no-sort not-colvis">
<a class="btn-sm" href="..." ><span class="material-icons">note_add</span></a>
</th>
<th>ID</th>
<th>Part</th>
<th>Released</th>
<!-- other columns -->
</tr>
</thead>
<tbody></tbody>
</table>
Data is accessed by ajax.
The problem is that now I should re-arrange mappings for each column "manually":
table = $('#main-table').DataTable({
ajax: {"url": "..."},
columnDefs: [
{
targets: 1,
render: function (data, type, row, meta) {
return row[0]; // table column 1, data column 0
}
},
{
targets: 2,
render: function (data, type, row, meta) {
return row[1]; // table column 2, data column 1
}
},
{
targets: 3,
render: function (data, type, row, meta) {
return row[2]; // table column 3, data column 2
}
},
// and so on
];
});
It is too verbose.
Is it possible to configure the same (data column X to table column X+1 ) in one line?
This discussion has been closed.
Answers
Instead of
columnDefs
I would usecolumns.data
and reference the position in the array, for example:Not sure what you want in column 0 but you can change the defaultContent to display buttons or whatever. Here is an example:
http://live.datatables.net/ketiboru/1/edit
Someone may post a better way of doing this.
Kevin
I want to remove column 0 from mappings (and going to render it by my own code).
this is still too verbose . But I could to add the same in javascript loop - so thank you for the idea.
P.S. Still think that there should be the way to remove "row header" from mappings "in one config options". Row headers is common practice, isn't it?
So far the best option:
But still would be nice just to say: "first table column is row header, exclude it from mapping" with one configuration option.