微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

GroupBy,在knockout.js 中过滤

如何解决GroupBy,在knockout.js 中过滤

我如何分组并使用knockout.js 在表中过滤。我也尝试展开和折叠行,这是我有点成功的代码)。

这是我的区别函数,请注意,目前它似乎只适用于一个数据元素(我希望将来它可以在对象的属性上与众不同。

ko.observableArray.fn.distinct = function (prop) {
    var target = this;
    target.index = {};
    target.index[prop] = ko.observable({});

    ko.computed(function () {
        //rebuild index
        var propIndex = {};

        ko.utils.arrayForEach(target(),function (item) {
            var key = ko.utils.unwrapObservable(item[prop]);
            if (key) {
                propIndex[key] = propIndex[key] || [];
                propIndex[key].push(item);
            }
        });

        target.index[prop](propIndex);
    });

    return target;
};  

这是我当前的对象函数

course(_id,_courseCode,_courseTitle,_courseCampus) {
    var self = this;
    this.id = ko.observable(_id);
    this.courseCode = ko.observable(_courseCode);
    this.courseTitle = ko.observable(_courseTitle);
    this.coursecampus = ko.observable(_courseCampus);
}

这是我的视图模型

function viewmodel() {
    var self = this;
    this.courses = ko.observableArray().distinct('courseCode');
    this.gpCourseCode = ko.observableArray();
    this.mutated = ko.observableArray();

    this.switchMutated = function (code) {
        if (self.mutated.indexOf(code) > -1) {
            self.mutated.push(code);
        }
        else {
            self.mutated.remove(code);
        }
    };

    this.switchText = function (code) {
        if (self.mutated.indexOf(code) > -1) {
            return "-"
        }
        else {
            return "+"
        }
    };

    self.gpCourseCode.push("MATH1030");
    self.gpCourseCode.push("MATH1006");
    self.gpCourseCode.push("MATH1046");
    self.courses.push(new course("1","MATH1030","Calculus","city1"));
    self.courses.push(new course("2","city2"));
    self.courses.push(new course("3","city3"));
    self.courses.push(new course("4","MATH1006","Linear algebra","city1"));
    self.courses.push(new course("5","MATH1046","discrete Math","city2"));
    self.courses.push(new course("6","city2"));
    self.courses.push(new course("7","city1"));

    this.searchCode = ko.observable("");
    self.filteredRecords = ko.computed(function () {
        return ko.utils.arrayFilter(self.gpCourseCode(),function (rec) {
            return (
                (self.searchCode().length == 0 || rec.toLowerCase().indexOf(self.searchCode().toLowerCase()) >= 0)
            )
        });
    });
}

这是我的html

<table class="table">
    <thead>
        <tr>
            <th>ID</th>
            <th>Course Code</th>
            <th>Course Title</th>
            <th>Course Campus</th>
        </tr>
        <tr>
            <th><input data-bind="value: searchCode" placeholder="Course Code" /></th>
            <th></th>
            <th></th>
            <th></th>
        </tr>

    </thead>
    <tbody data-bind="foreach: filteredRecords">
        <tr class="table-dark">
            <td><span data-bind="text: $data"></span></td>
            <td><span></span></td>
            <td></td>
            <td class="text-right">
                <button class="btn btn-secondary" data-bind="click: $root.switchMutated($data),text: $root.switchText($data)"></button>
            </td>
        </tr>
        <!-- ko foreach: $root.courses.index.courseCode()[$data] -->
        <tr data-bind="css: { mutated: $root.mutated.indexOf($data.courseCode()) > -1 }">
            <td><span data-bind="text: $data.id"></span></td>
            <td><span data-bind="text: $data.courseCode"></span></td>
            <td><span data-bind="text: $data.courseTitle"></span></td>
            <td><span data-bind="text: $data.coursecampus"></span></td>
        </tr>
        <!-- /ko -->
    </tbody>
</table>

这是我的两个问题

  1. 当我过滤搜索时,我的可变数组变为空,因此所有元素都被扩展。
  2. 我很想将我的 gpCourseCode 转换为类似于 course 的类,但我不确定如何对类进行不同的处理,而不仅仅是一个元素。

我有一个 jsfiddle here

解决方法

当我过滤搜索时,我的可变数组变为空,因此所有元素都被扩展。

那是因为您的点击处理程序有错误。它应该采用函数引用而不是函数调用。所以 click: $root.switchMutated($data) 应该变成:click: $root.switchMutated(在 foreach 中,点击处理程序会自动将当前项目作为它的第一个参数传递)。

然而,您现在必须自己初始化 mutated 数组(或者可能颠倒逻辑,因此您不必这样做)。您的点击处理程序执行此操作是偶然的,因为向点击处理程序提供函数调用将导致该函数被执行。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。