【问题标题】:How to toggle css class on a table head which also has an existing click binding?如何在也具有现有点击绑定的表头上切换 css 类?
【发布时间】:2015-12-14 03:42:04
【问题描述】:

我正在尝试toggle css class table head。但是表头已经有了click binding:我引用了docs here

默认情况下,我需要在表头(thead) 上有一个类,然后在单击表头时添加/删除(切换)该类。目标是使用该类向可排序的表头添加视觉描述:

下面是进行排序的代码:NOTE: 这个代码和想法是从Ryan Rahlf blog 复制而来的。 Article here:

我认为可以从排序函数调用 css 绑定,以便在单击列头时应用或删除它:

    self.sort = function(header, event){

        // This is the existing click binding (sort)
        // Place css logic in here so that individual thead class is toggled when clicked.

        if(self.activeSort === header) {

            header.asc = !header.asc;

        } else {
            self.activeSort = header;
        }

        var prop = self.activeSort.sortPropertyName;
        var ascSort = function(a,b){ return a[prop] < b[prop] ? -1 : a[prop] > b[prop] ? 1 : a[prop] == b[prop] ? 0 : 0; };
        var descSort = function(a,b){ return a[prop] > b[prop] ? -1 : a[prop] < b[prop] ? 1 : a[prop] == b[prop] ? 0 : 0; };
        var sortFunc = self.activeSort.asc ? ascSort : descSort;
        self.Artist.sort(sortFunc);
    };

Example on JSfiddle:

【问题讨论】:

  • 连头部的点击也不能切换吗?
  • 不是我尝试过的,我需要将 css 绑定到头部,但按照 Knockout 文档中的说明添加 css 绑定不起作用。

标签: javascript jquery knockout.js knockout-2.0


【解决方案1】:

有很多方法可以做到这一点,但为了不改变你在这里所做的很多事情,请参阅 sn-p 方法之一:

function viewModel() {

        var self = this;
        
        self.orderedBy = ko.observableArray([]);
        
        self.Artist = ko.observableArray([
            {
                'LastName': 'Simon',
                'FirstName': 'Paul'
            }
            ,
            {
                'LastName': 'McCartney',
                'FirstName': 'Paul'
            },
            {
                'LastName': 'McKnight',
                'FirstName': 'Brian'
            },
            {
                'LastName': 'Morrison',
                'FirstName': 'Marc'
            }]);

        self.headers = [
            {title: 'First Name', sortPropertyName: 'FirstName', asc: true},
            {title: 'Last Name', sortPropertyName:  'LastName', asc:true}
        ];

        self.sort = function(header, event){          
          
          self.activeSort = header;  
          
          if(self.orderedBy.indexOf(header.title)>=0)
            self.orderedBy.remove(header.title);          
          else
            self.orderedBy.push(header.title);
          
          self.activeSort.asc = !self.activeSort.asc;
          var prop = self.activeSort.sortPropertyName;
          var ascSort = function(a,b){ return a[prop] < b[prop] ? -1 : a[prop] > b[prop] ? 1 : a[prop] == b[prop] ? 0 : 0; };
          var descSort = function(a,b){ return a[prop] > b[prop] ? -1 : a[prop] < b[prop] ? 1 : a[prop] == b[prop] ? 0 : 0; };
          var sortFunc = self.activeSort.asc ? ascSort : descSort;
          self.Artist.sort(sortFunc);
        };
    }

    ko.applyBindings(viewModel());
.ordered{
  background-color: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
    <table>
        <thead>
        <tr data-bind="foreach: headers">
            <th data-bind="click: sort, text: title, css:{ordered: orderedBy.indexOf(title)>=0}"></th>
        </tr>
        </thead>

        <tbody data-bind="foreach: Artist">
        <tr>
            <td data-bind="text: FirstName"></td>
            <td data-bind="text: LastName"></td>
        </tr>
        </tbody>
    </table>

希望有用。

【讨论】:

  • 您好乔尔,谢谢!:使用您的方法,我如何将样式保留在两者上,但仅在再次单击同一个头部时才删除?例如:点击FirstName,它保持突出显示,点击LastName,它也保持突出显示,然后如果你再次点击名字,它会删除类。基本上,点击不同的头部不会切换样式,只有点击同一个头部。
  • 请设置一个有用的投票,所以我会编辑我的答案来帮助你。基本上我只是将orderedBy 属性更改为observableArray 并验证标题是否在。
  • @Asynchronous 所以答案已编辑,如果它设置为正确请:)
  • 仅供参考:标记为答案后,我意识到排序仅在第一次单击该列时有效。如果我第一次单击该列,它会排序,如果我再次单击 CSS 将被删除,但排序不起作用。问题在于这一行:self.activeSort = null。排序应该回到它的方式。
  • 我知道,但片段只是为了给你指路。因此,这不能证明撤消有用的投票是合理的。
猜你喜欢
  • 2013-01-27
  • 1970-01-01
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
相关资源
最近更新 更多