您好我试图用KnockoutJs在表上删除多行。我不知道为什么我的代码在我尝试在桌面上使用时不起作用。我在ul上试过这个并且工作得很好。
下面是我的代码。需要帮助提前感谢:(
HTML:
<table class="pure-table" id="tbl_ordered_products">
<thead>
<tr>
<th><input type="checkbox" id="chkAllProducts" /></th>
<th>Product Code</th>
<th>Product Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Discount Rate</th>
<th>Stock Balance</th>
<th>Total Discount</th>
<th>Orig. Price</th>
<th>Total</th>
</tr>
</thead>
<tbody data-bind="foreach: orderedProducts">
<tr>
<td><input type="checkbox" id="chkAllProducts" data-bind="value: $data, checked: $parent.selectedOrderedProducts"/></td>
<td data-bind="text: product_code"></td>
<td data-bind="text: name"></td>
<td data-bind="text: price"></td>
<td><input data-bind="value: quantity" /></td>
<td><input data-bind="value: discount" /></td>
<td data-bind="text: balance"></td>
<td data-bind="text: total_discount"></td>
<td data-bind="text: original_price"></td>
<td data-bind="text: total_price"></td>
</tr>
</tbody>
</table>
<input type="button" value="Remove Selected" data-bind="click: deleteSelectedProducts" />
我的JS:
function Product(id, product_number, name, price, quantity, discount, balance) {
var self = this;
self.id = ko.observable(id);
self.product_code = ko.observable(product_number);
self.name = ko.observable(name);
self.price = ko.observable(price.toFixed(2));
self.quantity = ko.observable(quantity);
self.discount = ko.observable(discount);
self.balance = ko.observable(balance);
}
function OrdersViewModel() {
var self = this;
self.customerCode = ko.observable();
self.full_name = ko.observable();
self.complete_address = ko.observable();
self.birthday = ko.observable();
self.email = ko.observable();
self.contact = ko.observable();
self.orderedProducts = ko.observableArray();
self.selectedOrderedProducts = ko.observableArray();
self.deleteSelectedProducts = function () {
alert(self.selectedOrderedProducts.length);
self.orderedProducts.removeAll(self.selectedOrderedProducts());
self.selectedOrderedProducts.removeAll();
}
答案 0 :(得分:2)
看起来你对Knockout如何在这里工作有些困惑。
比较一下:
<table class="pure-table" id="tbl_ordered_products">
<thead>
<tr>
<th>
<input type="checkbox" id="chkAllProducts" data-bind="click: toggleAllProducts" />
</th>
<th><label for="chkAllProducts">Product Name</label></th>
</tr>
</thead>
<tbody data-bind="foreach: orderedProducts">
<tr>
<td>
<input type="checkbox" data-bind="checked: isSelected, attr: {id: htmlId}" />
</td>
<td><label data-bind="text: name, attr: {for: htmlId}"></label></td>
</tr>
</tbody>
</table>
<input type="button" value="Remove Selected" data-bind="click: deleteSelectedProducts" />
和
function Product(id, name) {
var self = this;
self.htmlId = "product_" + id;
self.id = ko.observable(id);
self.name = ko.observable(name);
self.isSelected = ko.observable(false);
}
function OrdersViewModel(products) {
var self = this,
productObjects = ko.utils.arrayMap(products, function (product) {
return new Product(product.id, product.name);
});
self.orderedProducts = ko.observableArray(productObjects);
self.selectedOrderedProducts = ko.computed(function () {
return ko.utils.arrayFilter(self.orderedProducts(), function (product) {
return product.isSelected();
});
});
self.toggleAllProducts = function (vm, e) {
var checkboxState = e.target.checked;
ko.utils.arrayForEach(self.orderedProducts(), function (product) {
product.isSelected(checkboxState);
});
return true;
};
self.deleteSelectedProducts = function () {
self.orderedProducts.removeAll(self.selectedOrderedProducts());
};
}
isSelected
观察点,但这是拥有工作视图模型的关键。ko.utils.arrayFilter()
在这里非常有帮助。答案 1 :(得分:2)
checked
为value
时, Knockout object
绑定不起作用。
在isChecked
对象
Product
function Product(id, product_number, name, price, quantity, discount, balance) {
var self = this;
self.isChecked = ko.observable(false);
self.id = ko.observable(id);
self.product_code = ko.observable(product_number);
// ...
}
删除行
self.deleteSelectedProducts = function () {
var productToRemove = [];
ko.utils.arrayForEach(self.orderedProducts(), function(product){
if (product.isChecked())
{
productToRemove.push(product)
}
})
self.orderedProducts.removeAll(productToRemove)
}