简体   繁体   English

如何禁用/启用映射到敲除可观察数组的表中嵌入的下拉菜单

[英]How can I disable/enable a drop down embedded in a table mapped to a knockout observable array

I'd like to disable/enable a drop down embedded in a table in a form until a particular value is selected in the predecessor drop down. 我想禁用/启用表格中嵌入的下拉菜单,直到在之前的下拉列表中选择特定值为止。

Here's what I tried that doesn't seem to work: 这是我尝试的似乎无效的方法:

<tbody data-bind="foreach:studentData">
    <tr>
        <td><span data-bind="text:rollNo"></span></td>
        <td><select class="form-control" data-bind="options:$parent.sexDropDown,optionsText:'name',optionValue:'id',value:sex,optionsCaption:'-Select-'"></select></td>
        <td><select class="form-control" data-bind="options:$parent.uniformTypeDropDown,optionsText:'name',optionValue:'id',value:uniform,enable:sex? (sex.id == 2):false,optionsCaption:'-Select-'"></select></td>
    </tr>
</tbody>

Please find the associated fiddle here 请在这里找到相关的小提琴

To give a brief I want the "Uniform Type" drop down in each of the rows to be enabled in case the option "Female" is selected in the "Sex" drop down and it needs to be disabled in all other scenarios. 为了简单起见,我希望在“性别”下拉列表中选择“女性”选项并且在所有其他情况下都需要禁用的情况下,启用每一行中的“统一类型”下拉列表。

First, your sex and uniform properties have to be observable to allow for two way binding: 首先,必须观察到您的sexuniform属性,以便进行两种方式的绑定:

self.studentData  = ko.observableArray([
  { rollNo: 1, sex: ko.observable(null), uniform: ko.observable(null) }
  //                ^^^^^^^^^^^^^^    ^           ^^^^^^^^^^^^^^    ^
  // This allows the `value` binding to write to the property
]);

Then, in your enable binding, you can use the live-updating value: 然后,在enable绑定中,您可以使用实时更新值:

enable: sex() ? (sex().id === 2) : false
//         ^^       ^^
// Because this property is now observable, you need to call it to extract
// its value

In an updated fiddle: https://jsfiddle.net/ocd5qvr8/ 在更新的小提琴中: https : //jsfiddle.net/ocd5qvr8/

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

相关问题 如何在Knockout.js中附加到映射的可观察数组? - How do I append to a mapped observable array in Knockout.js? 淘汰赛js:如何推送到已映射的可观察数组 - Knockout js: How can i push to an observable array that has been mapped 从击倒.js的下拉菜单中过滤可观察数组 - filtering observable array from drop down menu in knockout.js 我可以绑定到计算的可观察数组(淘汰)吗? - Can I bind to a computed observable array (knockout)? 我怎样才能复制这个淘汰赛观察? - How can I copy this knockout observable? 如何在Knockout中更新映射的内部数组而不更新主数组中的数组对象数? - How can I update mapped inner arrays in Knockout without updating the number of array objects in the main array? 如何使用敲除功能禁用/启用html表中的所有元素 - how to disable/enable all elements in an html table using knockout Knockout.js-无法从可观察数组中删除映射的对象 - Knockout.js - cannot remove mapped object from observable array 如何在不使用计算的可观察变量的情况下一次将可观察阵列上的淘汰赛foreach限制为5? - How can i limit a Knockout foreach on an observable array to 5 at a time without using computed observables? 如何用jquery验证插件检测到的验证错误填充一个knockout observable数组 - how can i fill a knockout observable array with the validation errors detected by jquery validation plugin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM