【发布时间】:2016-10-27 11:55:08
【问题描述】:
我正在使用 Knockout js 进行绑定,我正在使用复选框绑定数据列表,我正在获取并绑定到列表项的数据,包括所有项目的复选框,现在我需要创建新的按钮名称“全选”和当我单击该按钮时,我需要选中所有复选框,并且我需要从选中的复选框中获取所有数据如何实现这一点需要帮助..
<div>
<input type="button" value="Select-All" data-bind="click:SelectAll" />
</div>
<div id="List" data-bind="foreach:items">
<ul>
<li>
<span data-bind="text:userId" style="display:none"></span>
<span style="margin-top: 10px;font-size: 22px;font-weight: bold;padding-left:0px;" data-bind="text:username"></span>
<input type="checkbox" data-bind="value:userId(),click:$root.toggle" />
</li>
</ul>
</div>
//ViewModel
function userViewModel()
var self=this;
{
function userList(userId, userName)
{
var self=this;
self.userID=ko.observable(userId);
self.userName=ko.observable(userName);
self.Selected = ko.observable(false);
}
self.toggleAssociation = function (item) {
//here i am getting the individual selected checkbox Item
}
//This part is not working
self.SelectAll = function() {
console.log("in")
self.items().forEach(function(item) {
item.Selected(true);
});
};
self.items = ko.observableArray();
self.add = function (userId, userName,){
self.items.push(new FriendsContact(userId, userName)
}
};
//Page Load
$(function () {
var viewModel = new userViewModel();
ko.applyBindings(viewModel);
//Get the data from database//
$.ajax({
type: "GET",
dataType: "json",
contentType: 'application/json; charset=utf-8',
url: baseUrl + 'xxx/xxx/xxxxx',
success: function (data) {
$.each(data, function (key, value) {
viewModel.add(value.userId, value.userName)
});
}
})
});
【问题讨论】:
-
您确定您的代码在上述问题之外还能正常工作吗?我试图创建一个小提琴并发现了很多问题
-
@yes 它与所有其他功能一起工作正常
标签: javascript jquery knockout.js knockout-2.0