Knockout.js 未选择通过knockoutjs绑定的单击表行
我想通过点击一个简单的表格行来选择它,如下所示: 我应用了上述逻辑,但仍然没有选择任何内容,我错了什么 数据显示正确,只是选择不起作用Knockout.js 未选择通过knockoutjs绑定的单击表行,knockout.js,Knockout.js,我想通过点击一个简单的表格行来选择它,如下所示: 我应用了上述逻辑,但仍然没有选择任何内容,我错了什么 数据显示正确,只是选择不起作用 define(['services/dataservice'], function (dataservice) { var self = this; this.Selected = ko.observable(); var schoolyears = ko.observableArray(); this.SelectScho
define(['services/dataservice'], function (dataservice) {
var self = this;
this.Selected = ko.observable();
var schoolyears = ko.observableArray();
this.SelectSchoolyear = function (config) {
self.Selected(config);
};
this.Selected(schoolyears()[0]);
var vm = {
activate: activate,
schoolyears: schoolyears,
title: 'Schoolyears'
};
return vm;
function activate(){
var schoolyearModels = dataservice.getSchoolyears();
var schoolyearViewModels = [];
for (var i = 0; i < schoolyearModels.length; i++){
var e = schoolyearModels[i];
var schoolyearViewModel = new SchoolyearViewModel(e.schoolyearId, e.schoolyearName, e.from, e.to, e.lastEdited, self.Selected);
schoolyearViewModels.push(schoolyearViewModel);
}
return schoolyears(schoolyearViewModels);
}
function SchoolyearViewModel(id, schoolyearName, from, to, lastEdited, selected){
var me = this;
this.schoolyearId = id;
this.schoolyearName = ko.observable(schoolyearName);
this.from = ko.observable(from);
this.to = ko.observable(to);
this.lastEdited = ko.observable(lastEdited);
this.AmISelected = ko.computed(function (){
debugger;
return selected() === me;
});
}
});
<section id="schoolyears-view" class="view">
<a class="btn btn-info btn-force-refresh pull-right" data-bind="click: remove" href="#"><i class="icon-remove"></i>Delete</a>
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th style="width: 25%">Schoolyear name</th>
<th style="width: 25%">From</th>
<th style="width: 25%">To</th>
<th style="width: 250%">Last edited</th>
</tr>
</thead>
<tbody data-bind="foreach: schoolyears">
<tr data-bind="click: $parent.SelectSchoolyear, css: { selected: AmISelected }, attr: { 'data-id': schoolyearId }" >
<td data-bind="text: schoolyearName()"></td>
<td data-bind="text: from()"></td>
<td data-bind="text: to()"></td>
<td data-bind="text: lastEdited()"></td>
</tr>
</tbody>
</table>
</section>
define(['services/dataservice'],函数(dataservice){
var self=这个;
this.Selected=ko.observable();
var学年=ko.observearray();
this.SelectSchoolyear=函数(配置){
自我选择(配置);
};
此。已选择(学年()[0]);
var vm={
激活:激活,
学年:学年,
标题:“学年”
};
返回虚拟机;
函数激活(){
var schoolyearModels=dataservice.getSchoolyears();
var schoolyearViewModels=[];
对于(变量i=0;i
问题似乎是Knockout正在vm
对象上查找remove
和SelectSchoolyear
方法,但它们不存在。它们只在这个对象上
这里有一个解决方案(请注意,remove
)仍然需要一个实现:
这假设在某个地方调用了activate
vm.activate();
我已经做了一个工作
注意:要查看绑定错误(如我提到的),只需使用浏览器的开发人员控制台(敲除将抛出异常)。argh。。。。由于缺少诸如删除之类的内容,这是一个复制/粘贴问题。。。我只是想把重要的东西贴出来。就在我读你的解决方案前5分钟我想嗯。。。学年已绑定,但缺少一些内容:P SelectSchoolyear属性。。。谢谢你,伙计,这让它起作用了。真的谢谢。有趣的是,你的JSFIDLE工作得很好,我可以选择每一行,但在我的代码中,我只能选择每第二行???当我从表class=“table table striped table bordered table condensed”>(简而言之,我没有交替行)中删除这个引导类时,我的示例也正常工作,这是另一个问题。。。
vm.activate();