Knockout JS:在 foreach 绑定中添加表单
Knockout JS: adding forms in foreach bindings
我想添加一个 'delete comment' 按钮作为一种表单,它会在使用 knockout 的 foreach 绑定生成的每个评论旁边触发 ajax 请求。 comments
是一个可观察的数组,每条评论都是一个包含 username
、text
、timestamp
和 commentID
成员的对象。在没有 <form>
元素的情况下加载以下内容工作正常:
<ul data-bind="foreach: comments">
<li>
<span data-bind="text: username"></span>
<ul>
<li data-bind="text: text"></li>
<li data-bind="text: timestamp"></li>
<form data-bind="if: sameUser" method="post" action="deleteComment.php">
<input data-bind="attr: {id: commentID}, click: deleteComment" type="submit" value="Delete comment">
</form>
</ul>
</li>
</ul>
但是,包含 <form>
元素会中断 foreach 循环,只会加载一条评论。
我想使用 if: sameUser
数据绑定,以便删除按钮仅对发布评论的用户可见,并且
attr: {id: commentID}
数据绑定以在单击按钮时将要删除的正确评论 ID 发送到服务器,但现在我主要关心的是首先加载 form/button。
解决这个问题的正确方法是什么?
Not sure if you are writing delete function on comment level or its list level but if are binding the function use $parent to get out of foreach context.
以下是工作示例
function viewModel() {
var self = this;
self.comments = ko.observableArray([]);
self.isDataLoaded = ko.observable(false);
self.loadData = function(){
setTimeout(function(){
self.comments.push({username:"A", sameUser:true, commentID:1, text:"This is comment from A"});
self.comments.push({username:"B", sameUser:true, commentID:2, text:"This is comment from B"});
self.comments.push({username:"C", sameUser:false, commentID:3, text:"This is comment from C"});
self.comments.push({username:"D", sameUser:true, commentID:4, text:"This is comment from D"});
self.isDataLoaded(true);
}, 2000);
}
self.deleteComment = function(data){
self.comments.remove(data);
}
}
var vm = new viewModel();
vm.loadData();
ko.applyBindings(vm);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul data-bind="foreach: comments">
<li>
<span data-bind="text: username"></span>
<ul>
<li data-bind="text: text"></li>
<!--<li data-bind="text: timestamp"></li> -->
<form data-bind="if: sameUser" method="post">
<input data-bind="attr: {id: commentID}, click: $parent.deleteComment" type="submit" value="Delete comment">
</form>
</ul>
</li>
</ul>
我想添加一个 'delete comment' 按钮作为一种表单,它会在使用 knockout 的 foreach 绑定生成的每个评论旁边触发 ajax 请求。 comments
是一个可观察的数组,每条评论都是一个包含 username
、text
、timestamp
和 commentID
成员的对象。在没有 <form>
元素的情况下加载以下内容工作正常:
<ul data-bind="foreach: comments">
<li>
<span data-bind="text: username"></span>
<ul>
<li data-bind="text: text"></li>
<li data-bind="text: timestamp"></li>
<form data-bind="if: sameUser" method="post" action="deleteComment.php">
<input data-bind="attr: {id: commentID}, click: deleteComment" type="submit" value="Delete comment">
</form>
</ul>
</li>
</ul>
但是,包含 <form>
元素会中断 foreach 循环,只会加载一条评论。
我想使用 if: sameUser
数据绑定,以便删除按钮仅对发布评论的用户可见,并且
attr: {id: commentID}
数据绑定以在单击按钮时将要删除的正确评论 ID 发送到服务器,但现在我主要关心的是首先加载 form/button。
解决这个问题的正确方法是什么?
Not sure if you are writing delete function on comment level or its list level but if are binding the function use $parent to get out of foreach context.
以下是工作示例
function viewModel() {
var self = this;
self.comments = ko.observableArray([]);
self.isDataLoaded = ko.observable(false);
self.loadData = function(){
setTimeout(function(){
self.comments.push({username:"A", sameUser:true, commentID:1, text:"This is comment from A"});
self.comments.push({username:"B", sameUser:true, commentID:2, text:"This is comment from B"});
self.comments.push({username:"C", sameUser:false, commentID:3, text:"This is comment from C"});
self.comments.push({username:"D", sameUser:true, commentID:4, text:"This is comment from D"});
self.isDataLoaded(true);
}, 2000);
}
self.deleteComment = function(data){
self.comments.remove(data);
}
}
var vm = new viewModel();
vm.loadData();
ko.applyBindings(vm);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul data-bind="foreach: comments">
<li>
<span data-bind="text: username"></span>
<ul>
<li data-bind="text: text"></li>
<!--<li data-bind="text: timestamp"></li> -->
<form data-bind="if: sameUser" method="post">
<input data-bind="attr: {id: commentID}, click: $parent.deleteComment" type="submit" value="Delete comment">
</form>
</ul>
</li>
</ul>