2014-12-10 48 views
-2

我想組織一個有2個布爾值和一個價格的observablearray。我需要通過淘汰賽和2個複選框,通過這兩個值過濾元素。也按價格(升序和降序)排序顯示的值。我沒有提供任何代碼,因爲我是全新的淘汰賽,我無法看到採取這些行動的方式。 欣賞指導我的人。使用敲除複選框的多個排序

+1

那麼你有什麼嘗試? – 2014-12-10 21:37:09

+0

我試過[this](http://knockoutjs.com/examples/animatedTransitions.html),並且我知道它是複選框的問題,但使用值進行排序比互聯網示例複雜得多。 Greetings – ArlanG 2014-12-10 22:41:31

+0

http://jsfiddle.net/e0tr5ooa/非常簡單的例子。 – 2014-12-11 13:24:26

回答

0

簡單的答案,我嘗試了this,但在我的個人觀點模型上做了一些改變以滿足我的需求。所以,我做了這樣的事情:

self.elementsToShow = ko.pureComputed(function() { 
      // Represents a filtered and ordered list of elements 
      var recomend = self.showRecommended(); //chekbox 1 
      var special = self.showSpecial(); // checkbox2 
      var sorting = self.currentSortDirection(); //sort direction: price or rating //ascending or descending, represented by an observableArray with that conditions and the //selectedSortDirection 

      if (!recomend && !special) return self.myOservableArray().sort(function (a, b) { 
       //in case that no one of the checkboxes where selected but the sort direction was't by default 
       if (sorting.price != null) { 
        var fp = sorting.price ? -1 : 1; 
        ap = parseInt(a.price); 
        bp = parseInt(b.price); 
        return ap == bp ? 0 : (fp * (ap < bp ? -1 : 1)); 
       } 
       else if (sorting.rate != null) { 
        var f = sorting.rate ? -1 : 1; 
        ar = parseFloat(a.rating); 
        br = parseFloat(b.rating); 
        return ar == br ? 0 : (f * (ar < br ? -1 : 1)); 
       } 
      }); 

      return ko.utils.arrayFilter(self.myOservableArray(), function (element) { 
       return (element.recommended != "0" && recomend) || (element.offer != "" && special); //some other conditions for the relection of the checkboxes in the observableArray 
      }).sort(function (a, b) { 
       if (sorting.price != null) { 
        var fs = sorting.price ? -1 : 1; 
        ap = a.price; 
        bp = b.price; 
        return ap == bp ? 0 : (fs * (ap < bp ? -1 : 1)); 
       } 
       if (sorting.rate != null) { 
        var fu = sorting.rate ? -1 : 1; 
        ar = a.rating; 
        br = b.rating; 
        return ar == br ? 0 : (fu * (ar < br ? -1 : 1)); 
       } 
      }); 
     }, self);