订阅以从foreach循环中选择项目 [英] Subscribe to select item from foreach loop

查看:70
本文介绍了订阅以从foreach循环中选择项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用foreach绑定来生成一个表,并且在每一行中都有一个下拉列表.根据他们的选择,我需要显示其他列.我遇到的问题是我需要订阅此下拉列表,以使我的表标题知道它需要显示其他列标题.

I am using the foreach binding to generate a table and in each row there is a drop down list. Based on their selection I need to display additional columns. The issue I am having is I need to subscribe to this drop down list to let me table header know that it needs to display the additional column headers.

我如何订阅从对象的foreach循环生成的选择列表?

How can I subscribe to the select list that is generated from a foreach loop of objects?

推荐答案

这是我拍过的jsfiddle,向您展示了它如何工作:

Here's a jsfiddle i slapped together to show you how it could work:

http://jsfiddle.net/vzkaY/5/

您可以使用ko.computeds确定该行中其他单元格所处的状态,如果它们有效,则让下一个单元格可见

You can use ko.computeds to determine what state the other cells in that row are in, and if they are valid, let the next cell be visible

self.col3data.active = ko.computed(function () {
    if (self.col2data() === "item B")
        return true;
    return false;
}, self);

并在标记中

<td><select data-bind="options: col2items, value: col2data"></select></td>
<td><select data-bind="visible: col3data.active, options: col3items, value: col3data"></select></td>

我认为您可能希望显示所有标题,而不管是否有任何行应显示该列的单元格数据.原因是,如果一行中所有列均可见,而您更改另一行中的某些值以隐藏"列标题,则可能会影响其他行.如果所有行均未处于正确状态以显示该列,则有可能折叠标题.

I think that you might want to show all of the headers regardless of whether any row should show its cell data for that column. the reason being, if one row has all columns visible, and you change some values in another row to "hide" the column header, then you would be impacting the other rows. its possible to collapse the header if all rows are not in the right state to show that column.

这篇关于订阅以从foreach循环中选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆