Jquery 如果存在';表中没有数据吗?

Jquery 如果存在';表中没有数据吗?,jquery,tablesorter,Jquery,Tablesorter,如果我的数据中没有数据,我会得到以下错误: Uncaught TypeError: Cannot read property 'rows' of undefined 如果表格中没有数据,如何禁用表格分拣机: 我使用的tablesorter如下: $(document).ready(function() { $("table").tablesorter(); // set sorting column and direction, this

如果我的数据中没有数据,我会得到以下错误:

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>

{%csrf_令牌%}
{%load HANDING%}
{%paginate limit files%}
{{{无止境分页.utils.get_弹性_页码}
{%用于文件%]中的选择
{{choice.file_name}
{{choice.type}
{{choice.size}}
{{choice.end_date}
{{choice.source}
{%endfor%}
试试这个

$(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;
    } 
});
如果正在使用,请尝试()中的代码

var minRows=3,//表排序器允许排序之前所需的最小行数
$t=$(“表”),
//检查行数;启用或禁用排序
checkRows=函数(){
//检查行数
var min=$t.find('tbody tr')。长度
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']
  });