使用挖空复选框进行多重排序

multiple sorting using knockout checkboxes

本文关键字:排序 复选框      更新时间:2023-09-26

我正在尝试组织一个包含 2 个布尔值和价格的可观察数组。我需要通过挖空和 2 个复选框,通过这两个值过滤元素。还可以按价格(升序和降序)对显示的值进行排序。我没有输入任何代码,因为我是淘汰赛的新手,我看不到进行这些操作的方法。感谢指导我的人。

简单的答案,我尝试过这个,但对我的个人视图模型进行了一些更改以满足我的需求。所以,我做了这样的东西:

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);