【问题标题】:Nested Foreach loop with diffrent list in knockoutjs淘汰赛js中具有不同列表的嵌套Foreach循环
【发布时间】:2017-08-12 21:41:04
【问题描述】:

我首先有两个列表 SelectedServiceSubCategoryList 声明为

self.SelectedServiceSubCategoryList = ko.observableArray([]);

第二个ServiceCategoryList

声明为

self.ServiceCategoryList = ko.observableArray([]);

这些列表的模型如下所示 对于 SelectedServiceSubCategoryList

    function SSCategoryViewModel(obj) 
{
                var x = this;
                x.Id = obj.SCategory.Id;
                x.SC_Name = obj.SCategory.SC_Name;
                x.SSC_Id = obj.SSC_Id;
                x.SSC_Name = obj.SSC_Name;
                x.Value = ko.observable(0);
                x.IsSelected = ko.observable(false);
                x.CompanyID = self.Company.CompanyID();
                x.Value.subscribe(function (newValue) {
                    self.IsEditingServices(true);
                    self.DrawServiceGraph(x);
                    $('#serviceIndustries .editButtonIocn .edit').click();
                });
                x.OnClickSlider = null;
            }

和 对于 ServiceCategoryList

function SCategoryViewModel(obj) 
{
                var x = this;
                x.Id = obj.Id;
                x.SC_Name = obj.SC_Name;
                x.IsSelected = ko.observable(false);
                x.CompanyID = self.Company.CompanyID();
                x.SSC_List = ko.observableArray([]);
                x.IsValueChanged = ko.computed(function () {
                    var selectedList = ko.utils.arrayFilter(x.SSC_List(), function (o) { return o.IsSelected() && o.Value() > 0; });
                    if (selectedList.length > 0) { return true; }
                    else { return false; }
                }, x);
            }

我在 HTML 中使用 data-bind 属性使用 foreach 绑定 SelectedServiceSubCategoryList

<ul class="collapsible popout" data-collapsible="accordion"
                        data-bind="foreach:SelectedServiceSubCategoryList">

我想在 SelectedServiceSubCategoryList foreach 循环中绑定 ServiceCategoryList

如何建立这两个列表之间的关系。

【问题讨论】:

  • 你能把SelectedServiceSubCategoryList中的单项结构和ServiceCategoryList中的单项结构发布出来,然后发布你期望的结果吗?
  • 我想在 ServiceCatSelectedServiceSubCategoryList 中使用 ServiceCategoryList 的 foreach

标签: jquery arrays list knockout.js


【解决方案1】:

如果两者都声明在同一级别,如下所示:

var viewModel = function() {
  ...
  self.SelectedServiceSubCategoryList = ko.observableArray([]);
  ...
  self.ServiceCategoryList = ko.observableArray([]);
  ...
}

然后您调用$parent 以访问ServiceCategoryList。

<ul class="collapsible popout" data-collapsible="accordion" data-bind="foreach: SelectedServiceSubCategoryList">
  <li>
    <ul data-bind="foreach: $parent.ServiceCategoryList ">
      ...
    </ul>
  </li>
  ...
</ul>

【讨论】:

    猜你喜欢
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 2011-08-11
    • 2013-11-25
    • 2013-10-07
    • 2013-04-20
    相关资源
    最近更新 更多