Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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
Jquery 在Knockout.js中获取强类型可观察项目数组中的项目总数_Jquery_Model View Controller_Knockout.js - Fatal编程技术网

Jquery 在Knockout.js中获取强类型可观察项目数组中的项目总数

Jquery 在Knockout.js中获取强类型可观察项目数组中的项目总数,jquery,model-view-controller,knockout.js,Jquery,Model View Controller,Knockout.js,请告诉我如何在我的视图中显示可观察数组集合的总长度。每当我尝试时,结果总是零。下面是示例。 MyVM.js文件 //Model var Course = function (data) { var self = this; self.title = ko.observable(data.Title); self.creditHour = ko.observable(data.CreditHour); self.result = ko.observableArray(dat

请告诉我如何在我的视图中显示可观察数组集合的总长度。每当我尝试时,结果总是零。下面是示例。 MyVM.js文件

//Model
var Course = function (data) {
    var self = this;
    self.title = ko.observable(data.Title);
    self.creditHour = ko.observable(data.CreditHour);
self.result = ko.observableArray(data.result);
    return self;
}
//View Model
var CourseList = function () {
    var self = this;

    self.courses = ko.observableArray([]);

    $.ajax({
        type: "GET",
        url: '/Student/Register_For_Courses',
        contentType: "application/json; charset=utf-8",
        datatype: 'json',
        traditional: true,
        success: function (data) {
            ko.utils.arrayForEach(data, function (data) {
                self.courses.push(new Course(data))          
            });

        },
        error: function (err) {
            alert(err.status + " : " + err.statusText);
        }

    });

    //Get total of array collection
    tHour = self.courses().length;
    // button functions
    removeCourse = function (course) { self.courses.remove(course); }


}

$(document).ready(function () {
    ko.applyBindings(new CourseList());
});
这是我的看法

<tr>
   Total CH<td data-bind="text:tHour"></td>
</tr>

总CH
问题是tHour返回的是0,而不是数组的预期长度。

两个选项:

  • 使用
    课程()

    <td data-bind="text: courses().length"></td>
    

  • 旁注:您的模板无效。不能将
    总CH
    直接放在
    tr
    内(例如,不在
    td
    th
    内)

    旁注2:你的
    tHour
    是一个;你的意思是
    self.tHour=…

    这是我贫血小博客上的一篇文章。

    有两种选择:

  • 使用
    课程()

    <td data-bind="text: courses().length"></td>
    

  • 旁注:您的模板无效。不能将
    总CH
    直接放在
    tr
    内(例如,不在
    td
    th
    内)

    旁注2:你的
    tHour
    是一个;你的意思是
    self.tHour=…

    1这是我贫血小博客上的一篇文章。

    *“获取knockoujs中强类型可观察项目数组中的项目总数”不确定这里的“强类型”是什么意思。这是JavaScript。没有什么是强类型的。*“获取knockoujs中强类型可观察项目数组中的项目总数”不确定这里的“强类型”是什么意思。这是JavaScript。没有强类型。