Tablesorter n[0] undefined

匿名 (未验证) 提交于 2019-12-03 09:14:57

问题:

I am having issues with the tablesorter. The error is: n[0] is undefined

function appendToTable(table,cache) {              if(table.config.debug) {var appendTime = new Date()}              var c = cache,                  r = c.row,                  n= c.normalized,                  totalRows = n.length,                  checkCell = (n[0].length-1),                  tableBody = $(table.tBodies[0]),                 rows = [];              for (var i=0;i < totalRows; i++) {                 rows.push(r[n[i][checkCell]]);                   if(!table.config.appender) {                      var o = r[n[i][checkCell]];                     var l = o.length;                     for(var j=0; j < l; j++) {                          tableBody[0].appendChild(o[j]);                      }                      //tableBody.append(r[n[i][checkCell]]);                 }             }    

Above is a code snippet from the jquery.tablesorter.js The line where it says checkCell = (n[0].length - 1) is where the error is occuring.

I do not understand how an undefined object is being passed into the function. The tablesorter was working fine in one of my project but not working fine on this project.

[EDIT] This is the code snippet used to call the tablesorter. If I comment out the tablesorter and tablesorterPager nothing else in my code will break.

$reviewTable = $("#reviewTable"); $reviewTable.tablesorter(); $reviewTable.tablesorterPager({ container: $("#pager"), size: 100, widgets: ['zebra'] }); 

Below is the client side

<asp:Repeater ID="reviewRepeater" runat="server">     <HeaderTemplate>         <table cellpadding="0" cellspacing="0" class="MarkAsReceived" id="reviewTable">             <thead>                 <tr class="even">                     <th align="left">Review ID</th>                     <th align="left">Date Submitted</th>                     <th align="left">Rating</th>                     <th align="left">Image</th>                       <th align="left">ProductId</th>                     <th align="left">Status</th>                     <th align="left">Name</th>                 </tr>             </thead>             <tbody>     </HeaderTemplate>     <ItemTemplate>         <tr>             <td align="left"><%#DataBinder.Eval(Container.DataItem, "ReviewId")%></td>             <td align="left"><%#DataBinder.Eval(Container.DataItem, "SubmissionDate")%></td>             <td align="left"><%#DataBinder.Eval(Container.DataItem, "Rating")%></td>             <td align="left"><%#hasImage(DataBinder.Eval(Container.DataItem, "ReviewerImages"))%></td>             <td align="left"><%#DataBinder.Eval(Container.DataItem, "ProductId")%></td>             <td align="left"><%#DataBinder.Eval(Container.DataItem, "Status")%></td>             <td align="left"><%#DataBinder.Eval(Container.DataItem, "Nickname")%></td>         </tr>     </ItemTemplate>     <AlternatingItemTemplate>         <tr>             <td align="left"><%#DataBinder.Eval(Container.DataItem, "ReviewId")%></td>             <td align="left"><%#DataBinder.Eval(Container.DataItem, "SubmissionDate")%></td>             <td align="left"><%#DataBinder.Eval(Container.DataItem, "Rating")%></td>             <td align="left"><%#hasImage(DataBinder.Eval(Container.DataItem, "ReviewerImages"))%></td>             <td align="left"><%#DataBinder.Eval(Container.DataItem, "ProductId")%></td>             <td align="left"><%#DataBinder.Eval(Container.DataItem, "Status")%></td>             <td align="left"><%#DataBinder.Eval(Container.DataItem, "Nickname")%></td>         </tr>     </AlternatingItemTemplate>     <FooterTemplate>             </tbody>         </table>         <asp:Label ID="lblEmpty" Text="No Items." runat="server" Visible='<%# reviewRepeater.Items.Count == 0 %>'></asp:Label>     </FooterTemplate> </asp:Repeater> 

回答1:

If I remember, you are getting that error because the table is initially empty ( the tbody ). So modify that line of code from this:

checkCell = (n[0].length-1), 

to this:

checkCell = totalRows ? (n[0].length - 1) : 0, 

If you are interested, I've forked and added lots of enhancements (including the change above) to the original tablesorter plugin on github.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!