DataTables: TypeError: i is undefined

2020-06-02 05:00发布

问题:

I have a table like the following

the table as rowspans because for some users I need to have 2 lines (Like you see at column 'D')

I am trying to use datatables:

<table class="table table-bordered table-hover table-striped" id="myTable">
(...)
</table>

And I call this at the begining of the code:

 <script>
    $( document ).ready(function() {
         $('#myTable').DataTable();
    });
</script>

But I have this error:

TypeError: i is undefined

And the table is not like a datatable type!

Maybe it doesn't work with rowspans? Any idea??

回答1:

FWIW you can also get this error if you don't have the same number of <td></td> elements in every row. Make sure you aren't adding any rows with nav buttons or links or anything like that that may not be formatted the same way as the other rows.



回答2:

jQuery DataTables plug-in doesn't support ROWSPAN attribute by default. However there is a RowsGroup plugin for jQuery DataTables that groups cells together to make them look like as if ROWSPAN attribute is used.

See this example for code and demonstration.

See jQuery DataTables – ROWSPAN in table body TBODY for more details.



回答3:

I'd say your table is not a data table because you have undefined data and the 'i' referred to is the internal iterator of the DataTable loop, the use of rowspans is the problem - I would redesign your table to have an entire row for each piece of data (in your example 250 would require an entire row with duplicate values for all other columns except D) - it is wholly possible to use css to hide values that are duplicated for the same visual effect, this would allow datatable filtering to still work on those rows (although you may need some hooks to reveal hidden data when these 'extra' rows are filtered).



回答4:

For future referer.

It is because you are using Rowspan or colspan which is not supportable.

If you want to use colspan you can use it outside </tbody>.

Thanks.



回答5:

This problem happens if your table is not well formed, for example you should have <table> <thead> <th> <tbody> <tr> <td>

And then the id of the table should not overlap with id of any thing else on the same page. Other wise you will get errors like i is udefined or c is undefined.



回答6:

I was facing the same issue. The main reason for the error is due to using the colspan & rowspan. Because the jQuery DataTables plug-in does not support them and hence causing the error.

TypeError: i is undefined

So, If you are using any colspan or rowspan within any <tr></tr> inside the <tbody></tbody> then make sure that each <tr></tr> having the same no of <td></td> for each row. If not, then repeat the <td style='display:none'></td> to match the same no e.g

<table border='1' cellspacing='2'>
   <thead>
      <tr>
         <td>A</td>
         <td>B</td>
         <td>C</td>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td rowspan="2">1</td>
         <td rowspan="2">name</td>
         <td>200</td>
         <td style='display:none'></td>
         <td style='display:none'></td>
      </tr>
      <tr>
         <td >300</td>
         <td style='display:none'></td>
         <td style='display:none'></td>
      </tr>
   </tbody>
</table>

I think by following the above suggestion will help you sure.