我目前正在为 Knockout 使用 Josh Buckley 的数据表绑定。我正在尝试在数据表中创建一个“删除行”按钮。我目前在每个按钮上都有一个点击数据绑定。数据表源连接到一个可观察的数组。当用户点击按钮时,对象会从 observable 数组中移除,然后触发更新绑定,表格视图随后会被更新。这一切都很好,至少在我第一次单击按钮时。
但是,一旦表格更新,“删除行”按钮上的所有单击绑定都将停止工作。我认为这是因为每次调用更新绑定时都会清除并重建整个表,因此绑定不会应用于新元素。
如何重新绑定每个数据表更新生成的新元素?或者,有没有更好的方法来处理这个问题?
自定义绑定:
(function($){
ko.bindingHandlers.dataTable = {
init: function(element, valueAccessor){
var binding = ko.utils.unwrapObservable(valueAccessor());
// If the binding is an object with an options field,
// initialise the dataTable with those options.
if(binding.options){
$(element).dataTable(binding.options);
}
},
update: function(element, valueAccessor){
var binding = ko.utils.unwrapObservable(valueAccessor());
// If the binding isn't an object, turn it into one.
if(!binding.data){
binding = { data: valueAccessor() }
}
// Clear table
$(element).dataTable().fnClearTable();
// Rebuild table from data source specified in binding
$(element).dataTable().fnAddData(binding.data());
}
};
})(jQuery);
HTML 代码:
<table id="membersTable" class="table table-striped table-bordered bootstrap-datatable datatable" data-bind="dataTable: membersTable">
<thead>
<tr>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
</table>
查看型号:
var groupViewModel = {
groupMembers: ko.observableArray([
new GroupMember("1", "Abe", true),
new GroupMember("2", "Bob", false),
new GroupMember("3", "Bill", false)
])
};
groupViewModel.membersTable = ko.computed(function() {
var self = this;
var final_array = new Array();
for(var i = 0; i < self.groupMembers().length; i++)
{
var row_array = new Array();
row_array[0] = self.groupMembers()[i].nameWithLink();
row_array[1] = self.groupMembers()[i].actions();
final_array.push(row_array);
}
return final_array;
}, groupViewModel);
groupViewModel.removeMember = function(id) {
var self = this;
self.groupMembers.remove(function(groupMember) {
return groupMember.id == id;
});
};
组成员对象:
function GroupMember(id, name, isGroupLeader)
{
var self = this;
self.id = id;
self.name = name;
self.isGroupLeader = ko.observable(isGroupLeader);
self.link = ko.computed(function() {
return "/#user/" + self.id;
});
self.nameWithLink = ko.computed(function() {
return '<a href="' + self.link() + '">' + self.name + '</a>';
});
self.actions = ko.computed(function() {
return '<a class="btn btn-danger" data-bind="click: function() {removeMember(' + self.id + ')}">' + '<i class="icon-minus-sign"></i>' + '</a>';
});
}
编辑:
jsfiddle 链接:http: //jsfiddle.net/zongweil/PLUKv/1/