简体   繁体   English

如何使用淘汰赛选择列中的所有复选框?

[英]How can I select all checkboxes in a column using knockout?

Disclaimer: The environment I'm working in has to be completely inline. 免责声明:我正在工作的环境必须完全内联。 The HTML calls to a JS file I am not allowed to edit. HTML调用了不允许编辑的JS文件。 That being said, I'm trying to select/deselect all checkboxes in a column when I click the header row. 就是说,当我单击标题行时,我试图选择/取消选择列中的所有复选框。 I want to be able to select/deselect any individual row below the header as usual but when I check the header I want EVERY row underneath to select/deselect. 我希望能够像往常一样选择/取消选择标题下的任何单独的行,但是当我检查标题时,我希望下面的每一行都可以选择/取消选择。

Right now my problem is that selecting the header row only selects or deselects once. 现在,我的问题是选择标题行只能选择或取消选择一次。 So clicking it once unchecks every row but then the functionality stops. 因此单击一次取消选中每一行,然后功能停止。 For what it's worth the check box doesn't appear in the header row either. 值得的是,该复选框也不会出现在标题行中。 That's a different problem though. 不过,这是一个不同的问题。

The problem resides in the first table row --> label class tag. 问题出在表的第一行->标签类标签。 Any suggestions? 有什么建议么?

<tbody>
    <tr>
        <td class="feature-name">All</td>
        <!-- ko foreach: $parent.featureHeadings[$index()] -->
        <td data-bind="css:{alt: !($index() % 2)}">
            <label class="multiple-checkboxes">
                <input type="checkbox" data-bind="checked: $parent.dataItems.every(function (acct) { 
                return !acct.features[$index()].isVisible() ||
                acct.features[$index()].value(); }), 
                click: function (data, index, model, event) 
                { 
                var newValue = event.srcElement.checked; 
                data.forEach(function (acct) { 
                if (acct.features[index].isVisible()) 
                    acct.features[index].value(newValue); 
                    }
                    ); 
                    }.bind($data, $parent.dataItems, $index())" />
            </label>
        </td>
        <!-- /ko -->
    </tr>
    <!--  ko foreach: dataItems -->
    <tr>
        <td class="feature-name" data-bind="text: name"></td>
        <!-- ko foreach: features -->
        <td class="setting" data-bind="highlightOverride: isOverridden(), css: { alt: !($index() % 2) }, highlightHelpHint: isHintActive">
        <!-- ko if: type === 'Boolean' -->
        <label class="checkbox" data-bind="css: { checked: value }, fadeVisible: isVisible()"><input type="checkbox" data-bind="checked: value" /></label>
        <!-- /ko -->
        </td>
        <!-- /ko -->
    </tr>
    <!-- /ko -->
</tbody>

Edit: Thank you all for reading. 编辑:谢谢大家阅读。 Sorry I could not offer more details to allow you to help me. 抱歉,我无法提供更多详细信息,以帮助您。 Here is the final solution. 这是最终的解决方案。

<label class="multiple-checkboxes" data-bind="css: { checked: $parent.dataItems.every(function (acct) { return !acct.features[$index()].isVisible() || acct.features[$index()].value(); }) }, click: function (data, index, model, event) { var newValue = !event.srcElement.classList.contains('checked'); data.forEach(function (acct) { if (acct.features[index].isVisible()) acct.features[index].value(newValue); }); }.bind($data, $parent.dataItems, $index())">

This works: 这有效:

<label class="multiple-checkboxes" data-bind="css: { checked: $parent.dataItems.every(function (acct) { return !acct.features[$index()].isVisible() || acct.features[$index()].value(); }) }, click: function (data, index, model, event) { var newValue = !event.srcElement.classList.contains('checked'); data.forEach(function (acct) { if (acct.features[index].isVisible()) acct.features[index].value(newValue); }); }.bind($data, $parent.dataItems, $index())">

I'd like to mark the question as answered but you can see I am new here. 我想将问题标记为已回答,但是您可以在这里看到我是新来的。

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

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