2012-04-10 111 views
1

我有2个数组。一个是一个地区所有学校的阵列,另一个是这些学校所有学生的名单。学生对象包含他们所属的学校。我可以列出所有学校的名单,我可以列出所有学生的名单。我希望能够选择一所学校(或几所学校),并让学生名单只显示该学校的学生。链接2可观察阵列与淘汰赛js

这里是我有(在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_teams只包含那些在学校的学生记录selected_schools数组?

有没有办法链接observableArrays,使observableArrays一个函数,或者可以捕获一个observableArray回调?

回答

2

我建议将selected_players_to_teams作为ko.computed实施,在selected_schools更新时运行,并返回selected_schools的players_to_teams。

看到这个的jsfiddle一个唯一的代码,例如:http://jsfiddle.net/MqNPm/

+2

我开始回答时@Tuan回答。这是我的小提琴,如果它有帮助:http://jsfiddle.net/rniemeyer/Getfx/ – 2012-04-10 18:40:24