Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按属性对列表进行分组_Javascript_C#_Html_Knockout.js_Knockout 2.0 - Fatal编程技术网

Javascript 按属性对列表进行分组

Javascript 按属性对列表进行分组,javascript,c#,html,knockout.js,knockout-2.0,Javascript,C#,Html,Knockout.js,Knockout 2.0,嗨,我正在尝试对类列表进行表分组。我在这一点上遵循代码,我的问题是这一行 <ul data-bind="foreach: $root.people.index.type()[$data]"> .distinct“courseCode”位于错误的可观察数组上。它应该位于this.courses=ko.observableArlay上,因为这是您要分组的数组 ko.observearray.fn.distinct=函数属性{ var目标=此; target.ind

嗨,我正在尝试对类列表进行表分组。我在这一点上遵循代码,我的问题是这一行

<ul data-bind="foreach: $root.people.index.type()[$data]">
.distinct“courseCode”位于错误的可观察数组上。它应该位于this.courses=ko.observableArlay上,因为这是您要分组的数组

ko.observearray.fn.distinct=函数属性{ var目标=此; target.index={}; target.index[prop]=ko.observable{}; 计算函数{ //重建索引 var-propIndex={}; ko.utils.arrayForEachtarget,功能项{ var key=ko.utils.unwrapobbservable项[prop]; 如果关键{ ProIndex[键]=ProIndex[键]| |[]; ProIndex[key].pushitem; } }; 目标指数[prop]propIndex; }; 回报目标; }; 函数课程\u id、\u课程代码、\u课程主题、\u课程计算机{ var self=这个; this.id=ko.observable\u id; this.courseCode=ko.observable\u courseCode; this.courseTitle=ko.observable\u courseTitle; this.coursecampus=ko.observable\u coursecampus; } 函数视图模型{ var self=这个; this.courses=ko.observearray.distinct'courseCode'; this.gpCourseCode=ko.observearray; self.courses.pushnew course1,数学1030,微积分,城市2; 如果self.gpCourseCode.indexOfMATH1030==-1{ self.gpCourseCode.pushMATH1030; } self.courses.pushnew course2,数学1030,微积分,城市1; 如果self.gpCourseCode.indexOfMATH1030==-1{ self.gpCourseCode.pushMATH1030; } 新课程3,数学1030,微积分,城市3; 如果self.gpCourseCode.indexOfMATH1030==-1{ self.gpCourseCode.pushMATH1030; } 新课程4,数学1006,线性代数,城市2; 如果self.gpCourseCode.indexOfMATH1006==-1{ self.gpCourseCode.pushMATH1006; } self.courses.pushnew course5,数学1046,离散数学,城市2; 如果self.gpCourseCode.indexOfMATH1046==-1{ self.gpCourseCode.pushMATH1046; } self.courses.pushnew course6,数学1006,线性代数,城市1; 如果self.gpCourseCode.indexOfMATH1006==-1{ self.gpCourseCode.pushMATH1006; } self.courses.pushnew course7,数学1046,离散数学,城市1; 如果self.gpCourseCode.indexOfMATH1046==-1{ self.gpCourseCode.pushMATH1046; } } ko.applybindingsnewviewmodel; 身份证件 课程代码 课程名称 课程校园
<table class="table table-striped">
    <thead>
        <tr>
            <th>ID</th>
            <th>Course Code</th>
            <th>Course Title</th>
            <th>Course Campus</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: gpCourseCode">
        <tr>
            <td data-bind="text: $data"></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr data-bind="foreach: $root.courses.index.courseCode()[$data]">
            <td><span data-bind="text: ID"></span></td>
            <td><span data-bind="text: courseCode"></span></td>
            <td><span data-bind="text: courseTitle"></span></td>
            <td><span data-bind="text: coursecampus"></span></td>
        </tr>
    </tbody>
</table>
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;
};    

function 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();
    this.gpCourseCode = ko.observableArray().distinct('courseCode');

    self.courses.push(new course("1", "MATH1030", "Calculus", "City2"));
    if (self.gpCourseCode().indexOf("MATH1030") == -1) {
        self.gpCourseCode.push("MATH1030");
    }
    self.courses.push(new course("2", "MATH1030", "Calculus", "City1"));
    if (self.gpCourseCode().indexOf("MATH1030") == -1) {
        self.gpCourseCode.push("MATH1030");
    }
    self.courses.push(new course("3", "MATH1030", "Calculus", "City3"));
    if (self.gpCourseCode().indexOf("MATH1030") == -1) {
        self.gpCourseCode.push("MATH1030");
    }
    self.courses.push(new course("4", "MATH1006", "Linear algebra", "City2"));
    if (self.gpCourseCode().indexOf("MATH1006") == -1) {
        self.gpCourseCode.push("MATH1006");
    }
    self.courses.push(new course("5", "MATH1046", "Discrete Math", "City2"));
    if (self.gpCourseCode().indexOf("MATH1046") == -1) {
        self.gpCourseCode.push("MATH1046");
    }
    self.courses.push(new course("6", "MATH1006", "Linear algebra", "City1"));
    if (self.gpCourseCode().indexOf("MATH1006") == -1) {
        self.gpCourseCode.push("MATH1006");
    }
    self.courses.push(new course("7", "MATH1046", "Discrete Math", "City1"));
    if (self.gpCourseCode().indexOf("MATH1046") == -1) {
        self.gpCourseCode.push("MATH1046");
    }
}