2

2つのアレイがあります。1つは地域内のすべての学校の配列であり、もう1つはそれらの学校のすべての生徒のリストです。生徒オブジェクトには、所属する学校が含まれています。すべての学校のリストを作成でき、すべての生徒のリストを作成できます。学校(またはいくつかの学校)を選択して、生徒のリストにその学校の生徒のみを表示できるようにしたいと思います。

これが私が(CoffeeScriptで)持っているものです:

ViewModel = () ->
    @accounts = ko.observableArray([])
    @players_to_teams = ko.observableArray([])
    @selected_players_to_teams = ko.observableArray([])
    @selected_schools = ko.observableArray([])
    null

意見:

<label for="school_selection">School</label>
<select id="school_selection" class="inline" multiple=true size="50" data-bind="options:accounts, optionsText: 'Name', selectedOptions: selected_schools"></select>


<div id="player_list" class="inline">
  <table class="table table-striped">
    <thead>
    <tr>
      <th id="firstName">First Name</th>
      <th id="lastName">Last Name</th>
      <th id="position">Position</th>
      <th id="teamName">Team Name</th>
      <th id="accountId">Account ID</th>
    </tr>
    </thead>
    <tbody data-bind="foreach: selected_players_to_teams">
    <tr>
      <td data-bind="text: FirstName"></td>
      <td data-bind="text: LastName"></td>
    </tr>
    </tbody>
  </table>
</div>

変更する場合、配列に学校がある学生レコードのみを含むselected_schoolsように更新する必要がありますか?selected_players_to_teamsselected_schools

observableArraysをリンクしたり、observableArraysを関数にしたり、observableArrayのコールバックをキャッチしたりする方法はありますか?

4

1 に答える 1

2

selected_schools が更新されたときに実行され、selected_schools のplayers_to_teams を返す ko.computed として selected_players_to_teams を実装することをお勧めします。

コードのみの例については、この jsfiddle を参照してください: http://jsfiddle.net/MqNPm/

トゥアン

于 2012-04-10T18:31:52.257 に答える