Type -6ren">
gpt4 book ai didi

knockout.js - knockout : Selectable table rows without extending the model?

转载 作者:行者123 更新时间:2023-12-04 10:01:27 24 4
gpt4 key购买 nike

我有通过 knockout 呈现的下表模板:

            <table class="gv" data-bind="visible: products().length > 0">
<thead>
<th>Type</th>
<th>Name</th>
</thead>
<tbody data-bind="foreach: products">
<tr data-bind="click: $root.selectProduct">
<td data-bind="text: type"></td>
<td data-bind="text: name"></td>
</tr>
</tbody>
</table>

现在我想使行可点击,并希望在选择一行时分配一个 css 类。
一次只能选择 1 (!) 行,因此必须取消选中其他行。

最简单的方法是使用选定的属性扩展我的模型(产品类),但这会破坏我与服务器端的 1:1 映射。

我应该如何解决这个问题?
你会如何处理这件事?

最佳答案

这是我现在的最终解决方案,没有额外的隐藏单选按钮:

<tr data-bind="click: $root.selectProduct, css: { 'active-row': $root.selectedProduct() === $data }">

ViewModel 中的 selectedProduct 实现:
function AppViewModel() {
// Private
var self = this;

// Public Properties
self.selectedProduct = ko.observable();

关于knockout.js - knockout : Selectable table rows without extending the model?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9871477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com