【问题标题】:Using knockout to copy objects from one array to another使用敲除将对象从一个数组复制到另一个数组
【发布时间】:2013-02-12 06:34:06
【问题描述】:

我正在使用 Webforms 页面。在它上面我有一个 KnockoutJS ViewModel,它通过调用后端 C# 代码来获取“客户”的序列化 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>

【问题讨论】:

    标签: c# .net json mvvm knockout.js


    【解决方案1】:

    假设您要复制 self.SelectedCustomers = ko.observableArray([]);

    然后像下面这样使用敲除的切片功能

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

    【讨论】:

    • 当有人拒绝回答时请说明理由,这样我也可以学习
    【解决方案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>
    

    在 javascript 类中定义了 ChosenCustomers 数组:

    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 绑定将始终是一个数组,但其中只有一项。

    【讨论】:

      【解决方案3】:

      我能够弄清楚这一点。在这里查看我的解决方案:

      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());
      

      【讨论】:

      • 我不明白您为什么要复制该项目。 self.SelectedComponents.push(self.componentToAdd()) 不会从 self.Components 数组中删除该项目。因此,如果您没有具体的理由说明该项目应该被复制而不被引用,那么这行代码就是多余的。
      • 是的,它需要被复制,因为您可以拥有具有不同折扣和数量的项目 3 的两个实例。
      猜你喜欢
      • 2013-05-28
      • 2022-07-05
      • 2018-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 2020-08-23
      • 2023-02-24
      相关资源
      最近更新 更多