Datatables - different number of columns.

Datatables - different number of columns.

tarudiantarudian Posts: 13Questions: 5Answers: 1

Hi,

I am wondering if its possible to get a different number of columns, based on the return value using datatables?

For example one case would show data for only 2018 like
Name, Type , 2018, jan, feb, mar ... etc

and the second case would need to display data for 2018 & 2019 like
Name, Type , 2018, jan, feb, mar ... 2019, jan, feb, mar ...

I was able to achive that on html side with data from a model (example below), but i lose most of the datatables functionalites using html here. So i was wandering if its possible to get it using columns property ?

   "columns": [
                        { "data": "Name", "width": "50px" },
//For each (var item in years)
                         { "data": "Jan", "width": "50px" },
                         { "data": "Feb", "width": "50px" },
                         { "data": "Mar", "width": "50px" },
// end loop for all years 
                     ]




<table id="TempTable"class="display row-border" cellspacing="0" width="100%">
                    <thead>
                        <tr>
                            <th>Name</th>
                             <th>type></th>
                            @foreach (var Year in Model.Years)
                            {

                                <th>Jan-@Year</th>
                                <th>Feb-@Year</th>
                                <th>Mar-@Year</th>
                                <th>Apr-@Year</th>
                                <th>May-@Year</th>
                                <th>Jul-@Year</th>
                                <th>Jun-@Year</th>
                                <th>Aug-@Year</th>
                                <th>Sep-@Year</th>
                                <th>Oct-@Year</th>
                                <th>Nov-@Year</th>
                                <th>Dec-@Year</th>
                            }
                        </tr>
                    </thead>
                            <tbody>
                                @foreach (var item in Model.Data)
                                {
                                    <tr>
                                        <td>@item.[0].Values.Name</td>
                                        <td>@item.[0].Values.Type</td>
                                        @foreach (var item2 in item.Values)
                                        {
                                            <td>@item2.Jan</td>
                                            <td>@item2.Feb</td>
                                            <td>@item2.Mar</td>
                                            <td>@item2.Apr</td>
                                            <td>@item2.May</td>
                                            <td>@item2.July</td>
                                            <td>@item2.June</td>
                                            <td>@item2.Aug</td>
                                            <td>@item2.Sep</td>
                                            <td>@item2.Oct</td>
                                            <td>@item2.Nov</td>
                                            <td>@item2.Dec</td>
                                        }
                                    </tr>                         
                                }
                            </tbody>
                        </table>

$(document).ready(function () {
    $('#TempTable').DataTable({
        "dom": 'rti'
    });

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,171Questions: 1Answers: 2,589
    Answer ✓

    This looks like a duplicate of this thread here.

This discussion has been closed.