我正在使用Webforms頁面。在它上面,我有一個KnockoutJS ViewModel,通過調用後端C#代碼來獲取序列化的「Customers」JSON列表。使用淘汰賽將對象從一個陣列複製到另一個陣列
我將該數組綁定到組合框,並且我想在單擊按鈕時將選定的客戶添加到另一個數組。我希望所選客戶的列表出現在一個簡單的無序列表中。
我不太清楚在單擊「添加」按鈕時如何將客戶添加到「SelectedCustomers」屬性。注意:我不想移動它們,只需複製。
使用Javascript /淘汰賽綁定
<script type="text/javascript">
$(document).ready(function() {
function CustomerViewModel() {
var self = this;
self.Customers= <%= getJson() %>;
self.SelectedCustomers = ko.observableArray([]);
//operations
self.addCustomerToList = function() {
//Add selected customer to self.SelectedCustomers
}
}
ko.applyBindings(new CustomerViewModel());
});
</script>
HTML元素
<select data-bind="options: Customers, optionsText: 'CustomerName', value: CustomerID, optionsCaption: 'Select a Customer to Add'"></select>
<button type="submit">Add Customer</button>
Selected Customers:
<ul data-bind="foreach: SelectedCustomers">
<li><span data-bind="text: CustomerName"></span></li>
</ul>
我不明白你爲什麼要複製的項目。 'self.SelectedComponents.push(self.componentToAdd())'不會從'self.Components'數組中移除項目。因此,如果您沒有具體的理由說明該項目應該被複制而不被引用,那麼該代碼行是多餘的。 – nickvane 2013-02-27 12:49:41
是的,它需要複製,因爲您可以有兩個具有不同折扣和數量的項目3的實例。 – PercivalMcGullicuddy 2013-02-27 13:23:51