Adding Columns Data in a loop

Adding Columns Data in a loop

tarudiantarudian Posts: 13Questions: 5Answers: 1

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 :smile:

 { "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

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @tarudian ,

    You can add rows in DataTables with rows().add() - would that do the trick?

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    Answer ✓

    possible to add columns data from a loop

    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

  • tarudiantarudian Posts: 13Questions: 5Answers: 1
    edited November 2018

    @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

                                { "data": "name", "orderable": false",
                                   render: function (data, type, row) { ....
    

    but i will make do. Thank you :)

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    One option may be to define your columns.render function in columnDefs then use columnDefs.targets to apply the render function. You could set columns.className in your columns settings to define which will use the render function. columnDefs.targets can be set using the class name.

    Kevin

This discussion has been closed.