【问题标题】:Tablesorter doesn't do anything in jQueryUI dialogTablesorter 在 jQueryUI 对话框中没有做任何事情
【发布时间】:2016-10-04 14:38:39
【问题描述】:

我正在尝试在 jquery ui 对话框中动态创建的表上使用 tablesorter,但 tablesorter 似乎不起作用。这意味着结果是一个简单的 html 表格,没有像 tablesorter 的“斑马”那样排序或布局。 我的代码:

var content = '<table id="searchtable" class="tablesorter"><thead><tr><th>Date</th><th>Subject</th></th></tr>';
content += '</thead><tbody><tr><td>04.10.2016 09:00</td><td>some Text</td></tr>';
content += '<tr><td>04.10.2016 12:00</td><td>another Text</td></tr></tbody></table>';


$("#searchButton").button().click(function() {
  $('#searchdialog').html(content).dialog({
    width: "auto",
    open: function() {
      $('#searchtable').tablesorter();
    }
  });
});
.tablesorter {
  width: auto;
  cursor: pointer;
  widgets: ['zebra'];
}
<link href="https://mottie.github.com/tablesorter/css/theme.blue.css" rel="stylesheet"/>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script src="http://mottie.github.com/tablesorter/js/jquery.tablesorter.js"></script>

<div id='searchButton'>Search</div>
<div id="searchdialog" title="Search Dialog"></div>

这是我在 stackoverflow 上的第一个问题,我对插入代码等不太熟悉,我从我的 jsfiddle (https://jsfiddle.net/JMKivi/kumrgyaw/12/) 复制/粘贴了代码。因此,请让我知道使用 stackoverflow 是否有错误或似乎缺少重要的东西。 编辑:我现在将&lt;button id='searchButton'&gt;Search&lt;/button&gt; 更改为&lt;div id='searchButton'&gt;Search&lt;/div&gt;。它在这里仍然有效,但在 jsfiddle 和我的生产环境中无效。无论如何感谢您的帮助。

【问题讨论】:

    标签: jquery jquery-ui jquery-ui-dialog tablesorter


    【解决方案1】:

    我收到此错误:

    Error: {
      "message": "TypeError: $(...).button is not a function",
      "filename": "http://stacksnippets.net/js",
      "lineno": 24,
      "colno": 1
    }
    

    这意味着您必须将选择器修改为:(删除按钮())

    $("#searchButton").click(function()
    

    var content = '<table id="searchtable" class="tablesorter"><thead><tr><th>Date</th><th>Subject</th></th></tr>';
    content += '</thead><tbody><tr><td>04.10.2016 09:00</td><td>some Text</td></tr><tr><td>01.10.2016 09:00</td><td>some Text</td></tr>';
    content += '<tr><td>04.10.2016 12:00</td><td>another Text</td></tr></tbody></table>';
    
    
    $("#searchButton").click(function() {
      $('#searchdialog').html(content).dialog({
        width: "auto",
        open: function() {
          $('#searchtable').tablesorter();
        }
      });
    });
    .tablesorter {
      width: auto;
      cursor: pointer;
      widgets: ['zebra'];
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.27.8/js/jquery.tablesorter.js"></script> 
    <button id='searchButton'>Search</button>
    <div id="searchdialog" title="Search Dialog"></div>

    是的,这似乎可行,只需单击标题;)

    【讨论】:

    • "$("#searchButton").click()" 和 "$("#searchButton")button().click()" 都在 jsfiddle 中工作。但是tablesorter无论如何都不排序:(
    • 对不起,我忽略了它现在排序。谢谢!编辑:好吧,它在这里排序,但不在 Jsfiddle 和我的生产环境中。
    • 它在 jsFiddle 中也适用于我。 - jsFiddle 我提到过,您指向 jquery.js 的链接的 url 有误:ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js。 - 删除最后的点:)
    • 谢谢。 “复制和粘贴”的情况 - 错误;)现在在 jsfiddle 中可以正常工作,但在我的生产环境中仍然不行。我认为必须重新编写该页面。
    猜你喜欢
    • 1970-01-01
    • 2012-09-07
    • 2016-10-02
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多