2013-06-26 7 views
0

Если нет никаких данных в моих данных я получаю эту ошибку:Как отключить jquery tablesorter, если в таблице нет данных?

Uncaught TypeError: Cannot read property 'rows' of undefined 

Как отключить таблицу сортировщика, если нет никаких данных в таблице:

Я использую tablesorter так:

$(document).ready(function() { 
     $("table").tablesorter(); 
      // set sorting column and direction, this will sort on the first and third column the column index starts at zero 
      var sorting = [[0,0],[2,0]]; 
      // sort on the first column 
      $("table").trigger("sorton",[sorting]); 
      // return false to stop default link action 
      return false; 
    }); 

Таблица выглядит следующим образом:

<table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table" class="tablesorter"> 
      <thead> 
      <tr> 
       <th width="5%" class="table-header-left"><a href=""><span></span></a></th> 
       <th width="25%" class="table-header-repeat line-left"><a href=""><span>File Name</span></a></th> 
       <th width="15%" class="table-header-repeat line-left"><a href=""><span>Type</span></a></th> 
       <th width="15%" class="table-header-repeat line-left"><a href=""><span>Size</span></a></th> 
       <th width="20%" class="table-header-repeat line-left"><a href=""><span>Date Updated</span></a></th> 
       <th width="20%" class="table-header-options line-left"><a href=""><span>Source</span></a></th> 
      </tr> 
      </thead> 
      {% csrf_token %} 
       {% load endless %} 
       {% paginate limit files %} 
       {{ endless_pagination.utils.get_elastic_page_numbers }} 

       {% for choice in files %} 
       <tr> 
       <td><input type="checkbox" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" /></td> 
       <td><label for="choice{{ forloop.counter }}">{{ choice.file_name }}</label></td> 
       <td>{{ choice.type }}</td> 
       <td>{{ choice.size }}</td> 
       <td>{{ choice.end_date }}</td> 
       <td>{{ choice.source }}</td> 
       </tr> 
       {% endfor %} 

      </table> 

ответ

2

Попробуйте это,

$(function(){ 
    if ($("table").find("tr").length > 1) 
    { 
     $("table").tablesorter(); 
     // set sorting column and direction, this will sort on the first and third column the column index starts at zero 
     var sorting = [[0,0],[2,0]]; 
     // sort on the first column 
     $("table").trigger("sorton",[sorting]); 
     // return false to stop default link action 
     return false; 
    } 
}); 
0

Если вы используете this fork of tablesorter, то попробуйте код с this demo (ref).

var minRows = 3, // Minimum number of rows needed before tablesorter allows sorting 

    $t = $('table'), 

    // check number of rows; enable or disable sorting 
    checkRows = function() { 
    // check number of rows 
    var min = $t.find('tbody tr').length < minRows; 
    // go through each header and enable/disable as needed 
    $t[0].config.$headers.each(function (i) { 
     // only enable/disable certain columns (ignore 5th column, it's always disabled) 
     if (i < 4) { 
     // disable sort per column 
     this.sortDisabled = min; 
     // add sorter-false class to hide controls 
     $(this).toggleClass('sorter-false', min); 
     } 
    }); 
    }; 

$t 
    // check number of rows after initialization & updates 
    .on('tablesorter-initialized updateComplete', checkRows) 
    // initialize tablesorter 
    .tablesorter({ 
    theme: 'blue', 
    widgets: ['zebra', 'columns'] 
    });