简体   繁体   English

在多个列表中选中复选框时,选择所有复选框

[英]Select All checkboxes on check of checkbox in multiple lists

There is a scenario where list of timelsots are listed under days and this data is dynamically coming from server. 在某些情况下,timelsot列表在天下列出,并且此数据动态地来自服务器。 Now user should select all the timeslots for a given day.Something like in this fiddle . 现在,用户应该选择一个给定day.Something所有时隙就像这个小提琴 I am trying to follow the example on knockout documentation but on this example header checkbox is not dynamic. 我正在尝试遵循淘汰赛文档中的示例,但是在此示例中,标题复选框不是动态的。 By same way how can i maintain the reference of multiple lists ? 以同样的方式如何维护多个列表的引用?

<ul class="header" data-bind="foreach: days">
<li>
    <label>
        <input type="checkbox" data-bind="attr: { id: 'chk' + id }"/>
        <span data-bind="text: name"></span>
    </label>
    <ul data-bind="foreach: timeslots">
        <li>
            <label>
                <input type="checkbox" data-bind="attr: { id: 'chk' + id }"/>
                <span data-bind="text: name"></span>
            </label>
        </li>    
    </ul>
</li>    

    function MyViewModel() {

    this.days = ko.observableArray([
        { name:"Sunday", id:1 , timeslots : [{ name:"10:00 - 11:00", id:10},{ name:"10:00 - 11:00", id:11},{ name:"10:00 - 11:00", id:12}] },
        { name:"Monday", id:2 , timeslots : [{ name:"10:00 - 11:00", id:20},{ name:"10:00 - 11:00", id:21},{ name:"10:00 - 11:00", id:22}] },
        { name:"Tuesday", id:3 , timeslots : [{ name:"10:00 - 11:00", id:30},{ name:"10:00 - 11:00", id:31},{ name:"10:00 - 11:00", id:32}] },
        { name:"Wednesday", id:4 , timeslots : [{ name:"10:00 - 11:00", id:40},{ name:"10:00 - 11:00", id:41},{ name:"10:00 - 11:00", id:42}] },
        { name:"Thursday", id:5 , timeslots : [{ name:"10:00 - 11:00", id:50},{ name:"10:00 - 11:00", id:51},{ name:"10:00 - 11:00", id:52}] },
        { name:"Friday", id:6 , timeslots : [{ name:"10:00 - 11:00", id:60},{ name:"10:00 - 11:00", id:61},{ name:"10:00 - 11:00", id:62}] },
        { name:"Saturday", id:7 , timeslots : [{ name:"10:00 - 11:00", id:70},{ name:"10:00 - 11:00", id:71},{ name:"10:00 - 11:00", id:72}] }
    ]);
    this.selectedDays = ko.observableArray([ 
        { id:1 , timeslots : [{ name:"10:00 - 11:00", id:10},{ name:"10:00 - 11:00", id:12}] },
        { id:2 , timeslots : [{ name:"10:00 - 11:00", id:21}] }
    ]);
}
ko.applyBindings(new MyViewModel());

The functionality i am trying to achieve is Example 2: Selecting/deselecting all items in the knockout documentation link but i don't how to implement the pureComputed function in my scenario. 我正在尝试实现的功能是示例2:选择/取消选择敲除文档链接中的所有项目 ,但是我不pureComputed在我的方案中实现pureComputed函数。

It's best to map your plain object to some sort of view model that can have observable or computed properties first. 最好将普通对象映射到可以先具有可观察或计算的属性的某种视图模型。

Then, 然后,

  1. You give each timeslot an observable boolean checked property, 您给每个timeslot一个可观察的布尔值checked属性,
  2. You give each day a writable computed checked property day可写的计算 checked属性

The write function of your days' checked property sets all of the inner timeslots . 您几天的checked属性的write函数设置所有内部timeslots Its read function returns whether at least one of the timeslots is checked. read功能返回是否检查了至少一个时隙。

In the end, you create a pureComputed state that parses the viewmodels back to a plain javascript data structure. 最后,您将创建一个pureComputed状态,该状态pureComputed视图模型解析回普通的javascript数据结构。 To do so, I filter out the unchecked days first. 为此,我首先过滤掉未选中的日期。 Then, for each day that has a checked timeslot, I collect the labels of the checked items. 然后,对于具有已检查时间段的每一天,我都会收集已检查项目的标签。


Here's an example, abstracted to just Group and Item models. 这是一个示例,仅抽象为GroupItem模型。 You should be able to convert it to your own format. 您应该能够将其转换为自己的格式。 Let me know if you need more help with that. 让我知道您是否需要更多帮助。

 function Item(label) { this.label = label; this.checked = ko.observable(false); } Item.forLabel = str => new Item(str); function Group(label, items) { this.label = label; this.items = items.map(Item.forLabel); this.checked = ko.computed({ read: () => this.items.some(i => i.checked()), write: val => { this.items.forEach(i => i.checked(val)); } }); } Group.prototype.toJS = function() { // Define the format of your selected state here: return { label: this.label, items: this.items .filter(i => i.checked()) .map(i => i.label) }; } const groups = [ new Group("colors", [ "red", "green", "blue" ]), new Group("directions", [ "up", "down" ]) ]; const selection = ko.pureComputed( () => groups .filter(g => g.checked()) .map(g => g.toJS()) ) ko.applyBindings({ groups, selection }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <ul data-bind="foreach: groups"> <li> <label> <input type="checkbox" data-bind="checked: checked"> <span data-bind="text: label"></span> </label> <ul data-bind="foreach: items"> <li> <label> <input type="checkbox" data-bind="checked: checked"> <span data-bind="text: label"></span> </label> </li> </ul> </li> </ul> <pre data-bind="text: JSON.stringify(selection(), null, 4)" style="background: #efefef; padding: 1rem;"></pre> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM