Adding Columns Data in a loop
Adding Columns Data in a loop
Hi,
My table column number will be different from case to case (depending on the month amount per case). And I am not sure how to include those months data with data tables. So I created a table and populated it from the model.
table id="MyTable"class="display row-border" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Gender</th>
@foreach (var M in Model.MonthsCounter)
{
<th>@M.Month-@M.Year</th>
}
</tr>
</thead>
<tbody>
@foreach (var item in Model.Data)
{
<tr>
<td>@item.Name</td>
<td>@item.Gender</td>
@foreach (var record in item.values)
{
<td>@record.Value</td>
}
</tr>
}
</tbody>
</table>
I was wondering if its possible to add columns data from a loop... somethig in the idea of this
{ "data": "Name", "autoWidth": true, "visible": false },
{ "data": "Gender", "autoWidth": true, "visible": false },
// someway to loop over data
foreach (var item in data)
{
{"data": item,"autoWidth": true, "visible": false }
}
I understand its possible with "Editor" by using .Add, but for few more weeks i have to postpone purchesing Editor, so i was wandering if it can be done with datatables.
BR,
This question has an accepted answers - jump to answer
Answers
Hi @tarudian ,
You can add rows in DataTables with
rows().add()
- would that do the trick?Cheers,
Colin
Not sure the Javascript syntax allows for adding object elements this way. While not exactly what you are asking for here is an example of dynamically creating the
columns
option then using it during Datatatables initialization.http://live.datatables.net/huyexejo/1/edit
Kevin
@colin i am looking on how to work with Columns not rows, but thanks
@kthorngren Thanks, that is somewhat what i was looking for. I think i might be able to continue based on that. My only concern here is that with this approach i don't have similar render options as i would have with
but i will make do. Thank you
One option may be to define your
columns.render
function incolumnDefs
then usecolumnDefs.targets
to apply the render function. You could setcolumns.className
in yourcolumns
settings to define which will use the render function.columnDefs.targets
can be set using the class name.Kevin