大家。我是 KnockoutJS 的新手。
我不想做学生表。可以在表中添加或删除新学生。
这是功能
function Friend(a, b){
}
会观察学生的细节。 applyBinding 的另一个函数
function functionViewModel()
如果它将被删除,则代码工作正常,但使用此功能代码将无法在
this.deleteRow=function(){
fn.friends.remove(this);
};
如何从函数“functionViewModel”调用“fn”变量到函数“Friend”。
如果有更好的主意,请建议我。
<table border="1">
<thead>
<th>Full Name</th>
<th>Address</th>
<th>Graduate ?</th>
<th>Subject</th>
<th>Remove User</th>
</thead>
<tbody data-bind="foreach:friends">
<tr>
<td data-bind="text:fullName"></td>
<td data-bind="text:address"></td>
<td><input type ="checkbox" data-bind="checked:graduate"></input></td>
<td><input type ="text" data-bind="value:subjects, visible:graduate"></input></td>
<td><input type= "button" data-bind="click:deleteRow" value="X"></input></td>
</tr>
</tbody>
</table>
<button data-bind="click:addUser">Add User</button>
<script src="D:\KnockoutJS\knockout-3.2.0.js"></script>
<script>
function Friend(a, b){
this.fullName=a;
this.address=b;
this.graduate=ko.observable(false);
this.subjects=ko.observable('');
//Remove Row from Table
this.deleteRow=function(){
fn.friends.remove(this);
};
}
function functionViewModel(){
var fn={friends:ko.observableArray([new Friend("Sofia Smith", "London"), new Friend("Liam Taylor","New York")])};
fn.addUser=function(){fn.friends.push(new Friend("Thomas Miller", "California"));};
return fn;
};
ko.applyBindings(functionViewModel());
</script>
最佳答案
我认为您必须执行以下任一操作。
http://jsfiddle.net/chLa93du/2/
在 Html 中(查看)
<table border="1">
<thead>
<th>Full Name</th>
<th>Address</th>
<th>Graduate ?</th>
<th>Subject</th>
<th>Remove User</th>
</thead>
<tbody data-bind="foreach:friends">
<tr>
<td data-bind="text:fullName"></td>
<td data-bind="text:address"></td>
<td><input type ="checkbox" data-bind="checked:graduate"></input></td>
<td><input type ="text" data-bind="value:subjects, visible:graduate"></input></td>
<td><input type= "button" data-bind="click:$parent.removeUser" value="X"></input></td>
</tr>
</tbody>
</table>
<button data-bind="click:addUser">Add User</button>
你的脚本:
function Friend(a, b){
this.fullName=a;
this.address=b;
this.graduate=ko.observable(false);
this.subjects=ko.observable('');
}
function functionViewModel(){
var fn={friends:ko.observableArray([new Friend("Sofia Smith", "London"), new Friend("Liam Taylor","New York")])};
fn.addUser=function(){fn.friends.push(new Friend("Thomas Miller", "California"));};
fn.removeUser = function(item){
fn.friends.remove(item);
};
return fn;
};
ko.applyBindings(functionViewModel());
http://jsfiddle.net/chLa93du/
var viewModel;
function Friend(a, b){
this.fullName=a;
this.address=b;
this.graduate=ko.observable(false);
this.subjects=ko.observable('');
this.deleteRow=function(){
viewModel.friends.remove(this);
};
}
function functionViewModel(){
var fn={friends:ko.observableArray([new Friend("Sofia Smith", "London"), new Friend("Liam Taylor","New York")])};
fn.addUser=function(){fn.friends.push(new Friend("Thomas Miller", "California"));};
return fn;
};
viewModel = new functionViewModel();ko.applyBindings(viewModel);
关于knockout.js - 在knockoutJS中使用按钮删除表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27684868/