如何根据foreach observablearray属性筛选选择列表选项

时间:2013-07-30 15:14:28

标签: knockout.js knockout-2.0

我有foreach使用名为CertificateDetailsToShow的observablearray。 CertificateDetailsToShow有一个名为LoanNum的列。嵌套在foreach中,我有一个选择列表,它使用一个名为DDABankNum的observablearray(填充选择列表选项)。它有一个名为mLoanNum的属性。我想根据observablearrays(LoanNum)中的DDABankNums.mLoanNum = CertificateDetailsToShow.LoanNum过滤选择列表中显示的内容。我怎样才能做到这一点?

我的观看代码 -

<tbody data-bind="foreach: CertificateDetailsToShow">
  <tr id="Tr1" style="cursor: pointer">
    <td>
      <ul style="width: 100%">
        <h5 class="muted"><b>Loan:&nbsp;<span class="muted" data-bind="text: LoanNum"></span>&nbsp;(<span data-bind="    text: CurrType"></span>)</b>
          <select id="Select1" style="margin-top: 0px; height: 24px; width: 160px !important"
            data-bind="options: $parent.DDABankNums, optionsText: 'AccountName', value: $parent.mLoanNum, optionsCaption: $data.LoanNum">
          </select>
        </h5>
      </ul>
    </td>
  </tr>
</tbody>

1 个答案:

答案 0 :(得分:5)

<tbody data-bind="foreach: CertificateDetailsToShow">
  <tr>
    <td>
      Loan: <span data-bind="text: LoanNum"></span> (<span data-bind="text: CurrType"></span>)

      <select
        data-bind="options: $parent.filter($parent.DDABankNums, $data.LoanNum), optionsText: 'AccountName', optionsValue: 'myDDABankNumValueAttributeName'">
      </select>
    </td>
  </tr>
</tbody>

ViewModel包含此方法:

self.filter = function(ddaBankNums, loanNum) {
    return ddaBankNums().filter(function(ddaBankNum) {
         return ddaBankNum.mLoanNum == loanNum;
    });
}