2013-02-26 27 views
0

我正在使用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> 

回答

0

我能弄明白这一点。点击这里,查看我的解决方案:

http://jsfiddle.net/6vBsm/1/

function ViewModel() { 
    var self = this; 

    self.Components = ko.observableArray([{ 
     "ID": "1", 
      "Name": "Tennis Ball", 
      "Description": "Basic Yellow Tennis Ball 9", 
      "Quantity": 0, 
      "Price": 1.99, 
      "Discount": 0.0, 
      "FreePeriods": 0 
    }, { 
     "ID": "2", 
      "Name": "Hockey Stick", 
      "Description": " Premium Carbon Fiber Construction", 
      "Quantity": 0, 
      "Price": 67.99, 
      "Discount": 0.0, 
      "FreePeriods": 0 
    }, { 
     "ID": "3", 
      "Name": "Cycling Helmet", 
      "Description": " For going fast.", 
      "Quantity": 0, 
      "Price": 226.99, 
      "Discount": 0.0, 
      "FreePeriods": 0 
    }]); 

    self.componentToAdd = ko.observable(); 
    self.SelectedComponents = ko.observableArray([]); 


    // Computed data 
    self.totalSurcharge = ko.computed(function() { 
     var total = 0; 
     for (var i = 0; i < self.SelectedComponents().length; i++) 
     total += self.SelectedComponents()[i].Price; 
     return total; 
    }); 


    //Operations 
    self.addComponent = function() { 
     var mycopy = JSON.parse(ko.toJSON(self.componentToAdd())); 
     self.SelectedComponents.push(mycopy); 
    }; 

} 

ko.applyBindings(new ViewModel()); 
+0

我不明白你为什么要复制的项目。 'self.SelectedComponents.push(self.componentToAdd())'不会从'self.Components'数组中移除项目。因此,如果您没有具体的理由说明该项目应该被复制而不被引用,那么该代码行是多余的。 – nickvane 2013-02-27 12:49:41

+0

是的,它需要复制,因为您可以有两个具有不同折扣和数量的项目3的实例。 – PercivalMcGullicuddy 2013-02-27 13:23:51

-1

比方说,你要复制self.SelectedCustomers = ko.observableArray([]);

然后使用如下

self.newselectedCustomers(self.SelectedCustomers().slice(0)); 
+0

请把原因时,有人downvote答案,所以,即使我可以学习 – DevelopmentIsMyPassion 2013-02-26 19:13:51

2

可以从列表中所选择的数据绑定的客户到另一阵列(ChosenCustomers)敲除的切片功能。 见http://knockoutjs.com/documentation/selectedOptions-binding.html

<select data-bind="selectedOptions: ChosenCustomers, options: Customers, optionsText: 'CustomerName', value: CustomerID, optionsCaption: 'Select a Customer to Add'"></select> 

在ChosenCustomers数组定义的JavaScript类:

self.Customers= <%= getJson() %>; 
self.SelectedCustomers = ko.observableArray([]); 
self.ChosenCustomers = ko.observableArray([]); 

在该方法中,我们检查,如果它不是已经存在,如果不把它添加到SelectedCustomers阵列。

self.addCustomerToList = function() { 
    self.ChosenCustomers.each(function(index, item){ 
     if(self.SelectedCustomers.indexOf(item) < 0){ 
      self.SelectedCustomers.push(item); 
     } 
    }); 
}; 

注意:虽然你的组合框可能只允许1个客户在同一时间被选中,selectedOptions结合永远是一个数组,而只会在它1项。