简体   繁体   English

在html表上的foreach绑定内,是否可以使用剔除将数据绑定到选择下拉列表?

[英]Is it possible to data-bind to a select dropdown with knockout while inside a foreach binding on a html table?

I am trying to show the same select with the same values and functions on a table. 我试图在表上显示具有相同值和功能的相同select I am using the foreach binding to bind to my table objects. 我正在使用foreach绑定来绑定到我的表对象。 Here is what my code looks like: 这是我的代码:

<table class="table table-bordered" >
    <thead>
        <tr>
            <th>Nextable Name</th>
            <th>POS Name</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: tables">
        <tr>
            <td data-bind="text: name"></td>
            <td><select  class="select2 span8 dropdown"  data-placeholder="Select Pos Table" data-bind="options: $parent.omnivoreTables, optionsText: 'name', optionsValue:'id', value: $parent.selectedOmnivoreTableId"></select></td>
        </tr>
    </tbody>
</table>

For some reason, the dropdowns all say undefined and are not selectable at all. 出于某种原因,下拉菜单都显示未定义并且根本无法选择。 I inspect the page and the dropdown elements and they all have the correct options and values inside as if this worked, but it doesn't. 我检查了页面和下拉菜单元素,并且它们内部都有正确的选项和值,好像可以正常工作,但是没有。 Any help much appreciated! 任何帮助,不胜感激!

Is this what you want it to do? 这是您想要的吗?

 var vm = { omnivoreTables: [ {name: 'One', id: '1'}, {name: 'Two', id: '2'} ], selectedOmnivoreTableId: ko.observable(1), tables: ko.observableArray([ {name: 'First'}, {name: 'Second'}, {name: 'Third'} ]) }; ko.applyBindings(vm); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <table class="table table-bordered"> <thead> <tr> <th>Nextable Name</th> <th>POS Name</th> </tr> </thead> <tbody data-bind="foreach: tables"> <tr> <td data-bind="text: name"></td> <td> <select class="span8 dropdown" data-placeholder="Select Pos Table" data-bind="options: $parent.omnivoreTables, optionsText: 'name', optionsValue:'id', value: $parent.selectedOmnivoreTableId"></select> </td> </tr> </tbody> </table> 

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

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