jquery - 如何使用带有 knockout 功能的 Jquery Table Sorter

标签 jquery knockout.js tablesorter

我有一个需要应用排序的表。我正在使用 knockout 和 jquery.tablesorter.js。我也尝试过自定义绑定(bind),但没有帮助。如果没有 knockout ,我的代码可以正常工作。下面是我的表格。

<table class="tbl" id="dash" data-bind="sortTable: true">
      <thead>
        <tr class="tag close">
          <th>Type</th>
          <th>Title</th>
        </tr>
      </thead>
      <tbody class="scrollContent" data-bind="foreach: Course">
        <tr>
          <td><i class="icon"></i></td>
          <td><a href="#" id="qtipselector_01" data-bind="text: Title"></a></td>
          <div id="TooltipContent_01" class="hidden">
            <a> Test Tool Tip</a>                 
          </div>
      </div>
    </tr> 
  </tbody>
 </table>

最佳答案

这是一个示例:http://jsfiddle.net/jearles/RGsEH/

注意:JS 和 CSS 文件依赖项被引入到托管资源下。

HTML

<table data-bind="sortTable: true">
  <thead>
    <tr>
      <th>Type</th>
      <th>Title</th>
    </tr>
  </thead>
  <tbody data-bind="foreach: course">
   <tr>
      <td data-bind="text: type"></td>
      <td data-bind="text: title"></td>
    </tr> 
  </tbody>
 </table>

JS

function Course(type, title) {
    this.type = type;
    this.title = title;
}

var ViewModel = function() {
    this.course = ko.observableArray([
        new Course("type", "course1"),
        new Course("another_type", "course2"),
        new Course("second_type", "course5"),
        new Course("third_type", "course4"),
        new Course("fourth_type", "course3")        
    ]);
}

ko.bindingHandlers.sortTable = {
    init: function(element, valueAccessor) {
        setTimeout( function() {
            $(element).addClass('tablesorter');
            $(element).tablesorter({widgets: ['zebra']});
        }, 0);
    }
};

ko.applyBindings(new ViewModel());

关于jquery - 如何使用带有 knockout 功能的 Jquery Table Sorter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14593367/

相关文章:

javascript - 如何将列大小调整添加到 MooTools 可排序表?

javascript - 在php中点击动态按钮调用参数化函数

JQuery Ajax 调用,类型为 : "POST" is received as "GET" by server

jquery - 在 HTML 中链接内部文件

javascript - Durandal - 防止绑定(bind)导致 subview 的组合问题

javascript - 为什么 JavaScript 中的正则表达式 match 找到结果,而 test 返回 false?

javascript - 事件系统导致Javascript中的堆栈溢出

knockout.js - 跟踪 Knockout 模型中的更改并在输入更改时触发 CSS 中的更改

c# - MVC3 - Razor - 在 View 或下拉列表之间传输数据

javascript - tablesorter (mottie fork)/恢复 tbody 滚动位置 - 正确吗?