1

使用 knockout.js 时,我遇到了嵌套 foreach 的问题。下面是说明

MyViewModel(){
   filteredRecords();
   columns();
}

RecordModel(){
   A,
   B,
   C
}

ColumnModel{
   Visible,
   ColumnName
}

要求是,如果 column.Visible == 1,让在 ReportModel 中显示相应的 ColumnName。在 data-bind="value:records.column.Column_Name() 中,我尝试获取正确的路径但还没有成功,请给我一些建议,非常感谢。


<div>
<!-- ko foreach: {data: filteredRecords(), as: 'records'}  -->
   <ul class="tableBody">
      <li class="firstLevel">
          <ul>
              <li class="col col01"># : <span data-bind="text: records.ID"></span></li>                            
                 <!-- ko foreach: {data: $parent.columns(), as: 'column'} -->
                     <!-- ko if: column.Visible() == 1 -->

                          <li class="col col0"><input class="input0" **data-bind="value: records.column.Column_Name()"** /></li>  

                  <!-- /ko -->
                 <!-- /ko -->              
           </ul>
       </li>
    </ul>
<!-- /ko --> 
</div>     enter code here
4

1 回答 1

3
<ul data-bind="foreach: { data: filteredRecords, as: 'record' }">
    <li><strong>#</strong> : <span data-bind="text: ID"></span>
        <ul data-bind="foreach: { data: $root.columns, as: 'column' }">
            <!-- ko if: column.Visible -->
            <li>
                <strong data-bind="text: column.ColumnName"></strong>:
                <span data-bind="text: record[column.ColumnName()]"></span>
            </li>
            <!-- /ko -->
        </ul>
    </li>
</ul>​

如果您愿意,可以将内部换成<ul>a <!-- ko -->

http://jsfiddle.net/MizardX/K7Lzs/

于 2012-12-10T23:43:26.140 回答