首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将淘汰制模型数据绑定到多个表

如何将淘汰制模型数据绑定到多个表
EN

Stack Overflow用户
提问于 2015-08-05 06:34:52
回答 1查看 877关注 0票数 1

我使用敲除绑定将一些数据绑定到html表中。我的淘汰赛视图模型有多个产品,每个产品将有多个字符。我想在一个表中显示产品,当我选择“显示字符”链接时,它应该在下表中显示相应的字符。

这是我的视图模型

代码语言:javascript
复制
var ProductViewModel = function(items) {
    this.items = ko.observableArray(items);
    this.itemToAdd = ko.observable("");
    this.addItem = function() {
        if (this.itemToAdd() != "") {
            this.items.push(this.itemToAdd()); 
            this.itemToAdd(""); 
        }
    }.bind(this);
};

这是我的html表

代码语言:javascript
复制
<div id="productTable">
        <table  class="ui-responsive table">
            <thead>
                <tr>
                    <th >Product Name</th>
                    <th >Description</th>
                    <th >Parent?</th>
                </tr>
            </thead>
            <tbody id="pBody" data-bind="foreach: items">
                <tr class="success" >
                    <td><span data-bind="text: name"></span>

                    </td>
                    <td><span data-bind="text: desc"></span>

                    </td>
                    <td><a href="#" onclick="showChars();return false;">show chars</a></td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
<div id="productChars">
    <div id="productCharTable">
        <table  class="ui-responsive table">
            <thead>
                <tr>
                    <th >Char Name</th>
                    <th >Description</th>
                    <th >Length</th>
                    <th >Type</th>
                </tr>
            </thead>
            <tbody id="pBody" data-bind="foreach: $data['chars']">
                <tr class="success">
                    <td><span data-bind="text: name"></span>
                    </td>
                    <td>asdf asdfasdf</td>
                    <td>10</td>
                    <td>String</td>
                </tr>
            </tbody>
        </table>
    </div>

我能把产品装到第一张桌子上。但对于我的特点,我不知道如何实现同样。

有谁能帮我弄清楚如何实现同样的目标吗?

这里是jsfiddle k/0Ln7h2bo/7/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-05 08:17:03

正如@Super酷所指出的,您可以使用“data -bind=‘’”来填充第二个表中的chars数据。为此,您需要在模型中再添加一个名为selectedItem的项,每次选择或添加一行时,都会将selectedItem指向该元素数据。并对第二个表使用"data-bind='with selecteItem'“。

代码语言:javascript
复制
var ProductViewModel = function(items) {
this.items = ko.observableArray(items);
this.selectedItem = ko.observableArray();
this.itemToAdd = ko.observable("");
this.addItem = function() {
    if (this.itemToAdd() != "") {
        this.items.push(this.itemToAdd()); 
        this.itemToAdd(""); 
    }
}.bind(this);

};

并在行选择调用某些函数selectedItem( $data ),其中$data引用当前项。

然后在模型中将数据设置为selectedItem。

代码语言:javascript
复制
function selectedItem(prod){
    ProductViewModel.selectedItem(prod);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31825018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档