Javascript 使用jquerydatatables和绑定插件的KnockoutJS,行单击传递整个数组而不是单个模型
我正在使用这个插件 处理datatables/ko绑定。 以下是JS代码:Javascript 使用jquerydatatables和绑定插件的KnockoutJS,行单击传递整个数组而不是单个模型,javascript,knockout.js,jquery-datatables,Javascript,Knockout.js,Jquery Datatables,我正在使用这个插件 处理datatables/ko绑定。 以下是JS代码: function ProductViewModel() { // Init. var self = this; self.products = ko.observableArray(); self.singleProduct = ko.observable(); var mappedProducts; // At first load i'm loading data. $.getJSON(
function ProductViewModel() {
// Init.
var self = this;
self.products = ko.observableArray();
self.singleProduct = ko.observable();
var mappedProducts;
// At first load i'm loading data.
$.getJSON("/admin/test", function(allData) {
mappedProducts = $.map(allData, function(item) {
var p = new Product(item);
// I'm adding a new property to my model, to handle row level actions.
// I'm not sure this is a good practice.
p.edit = "<button data-bind='click: $root.edit'><i class='icon-pencil'></i></button>";
return p;
});
self.products(mappedProducts);
});
// Here i'm using the basic switch pattern, as from KO tutorials.
self.edit = function(product) {
console.log(product); // <--- Prints the whole self.products() array
self.singleProduct(product);
self.products(null);
}
self.list = function() {
self.products(mappedProducts);
self.singleProduct(null);
}
}
// My model.
function Product(item) {
this.name = ko.observable(item.name);
this.dealer = ko.observable(item.dealer);
this.cost = ko.observable(item.cost);
this.price = ko.observable(item.price);
this.picture = ko.observable();
}
函数ProductViewModel(){
//初始化。
var self=这个;
self.products=ko.observearray();
self.singleProduct=ko.observable();
产品的var;
//在第一次加载时,我正在加载数据。
$.getJSON(“/admin/test”,函数(allData){
mappedProducts=$.map(所有数据,函数(项){
var p=新产品(项目);
//我正在向模型中添加一个新属性,以处理行级操作。
//我不确定这是不是一个好的做法。
p、 编辑=”;
返回p;
});
自我产品(mappedProducts);
});
//这里我使用的是基本的切换模式,正如KO教程中的一样。
self.edit=功能(产品){
console.log(product);//好吧,我想我自己得到了它,在浏览了插件源代码之后,原因很清楚
来自插件源:
(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);
基本上,对于每个更新操作,都会清理表并使用可观察数组重新构建,该数组应该提供绑定特性
KO在每一次本机点击:绑定中试图做的是将上下文数据(即整个数组)传递给适当的处理程序。好吧,我想我自己得到了,在浏览了插件源代码后,原因很清楚
来自插件源:
(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);
基本上,对于每个更新操作,都会清理表并使用可观察数组重新构建,该数组应该提供绑定特性
在每次本机click:binding中,KO试图做的是将上下文数据(即整个数组)传递给适当的处理程序