我编写了代码来过滤来自可观察数组的数据,但是它总是会抛出一个错误,表示长度未定义。
这是我的功能代码:
self.filterShowsByTheatre=function(teatr)
{
var stageid;
if (teatr != staryteatr)
{
if (teatr == "") {
self.Shows.removeAll();
self.loadShows();
}
else {
for (var x = 0; x < self.stages().lenght; x++) {
console.log(self.stages()[x].TheatreName);
if (self.stages()[x].TheatreName == teatr)
stageid = self.stages()[x].StageId;
}
var temp = [];
for (var i = 0; i < self.Shows().length; i++) {
if(self.Shows()[i].StageId==stageid)
{
temp.push(self.Shows()[i]);
}
}
self.Shows.removeAll();
for (var z = 0; z < temp.length; z++) {
self.Shows.push(temp[z])
}
}
staryteatr = teatr;
}
}
这是我的视图模型:
function viewModel() {
var self = this;
self.stages = ko.observableArray();
self.Shows = ko.observableArray();
self.loadShows = function()
{
Here Shows.push...
}
self.loadStages=function()
{
here stages.push...
}
self.loadShows();
self.loadstages();
}
function stage(id,Tid,Sname,Tname,adress, lat, long) {
var self = this;
self.StageId = id;
self.TheatreId = Tid;
self.Stagename = Sname;
self.TheatreName = Tname;
self.Adress = adress;
self.lat = lat;
self.long = long;
}
function show(showId, stageId, showName, directorName)
{
var self = this;
self.ShowId = showId;
self.StageId = stageId;
self.ShowName = showName;
self.DirectorName = directorName;
}
var model = new viewModel();
ko.applyBindings(model);
当我尝试从例如读取长度时。 loadShows()
它有效,但filterShows
却没有。
答案 0 :(得分:4)
拼写错误:
self.stages().lenght
应该是
self.stages().length