gpt4 book ai didi

javascript - 如何将 observableArray 中的 observable 复制到另一个 observableArray?

转载 作者:行者123 更新时间:2023-12-03 12:29:53 27 4
gpt4 key购买 nike

我是 JS 和 Knockout 的新手。我想将 observableArray 中的特定 observable 复制到另一个 observableArray。我怎样才能做到这一点?

HTML

<table>
<thead>
<tr>
<th>Passenger Name</th>
<th>Meal</th>
<th>Amount ($)</th>
<th></th>
</tr>
</thead>
@*render a copy of seats child elements for each entry in the seats array*@
<tbody data-bind="foreach: seats">
<tr>
<td data-bind="text: name"></td>
@*update the view to make use of the formatted Price*@
<td>
<select data-bind="options: $root.availableMeals,
value: meal, optionsText: 'mealName'">
</select>
</td>
<td data-bind="text: formattedPrice"></td>
<td><a href="#" data-bind="click: $root.removeSeat">Remove</a></td>
</tr>
</tbody>
</table>

要求

requirements

最佳答案

我创建了一个 JS fiddle 显示了如何使用以下代码在可观察数组之间移动项目:

HTML

<p>first list:</p>
<table>
<tbody data-bind="foreach: contents">
<td> <strong>
<span id="textKey" data-bind="text: displayKey" />
</strong>
</td>
<td>
<input id="textValue" type="text" data-bind="value: displayValue" />
</td>
<td>
<input type="button" data-bind="click: $root.copyItem" value="select" />
</td>
</tbody>
</table>

<p>Second list:</p>

<table>
<tbody data-bind="foreach: selectedContents">
<td> <strong>
<span id="textKey" data-bind="text: displayKey" />
</strong>
</td>
<td>
<input id="textValue" type="text" data-bind="value: displayValue" />
</td>
</tbody>
</table>

查看模型:

function ViewModel() {
var self = this;

self.contents = ko.observableArray([{
"displayKey": "Fruit",
"displayValue": "Bananas"
}, {
"displayKey": "Colour",
"displayValue": "Red"
}, {
"displayKey": "Car",
"displayValue": "Ferrari"
}]);
self.selectedContents = ko.observableArray([]);

self.copyItem = function(item) {
self.selectedContents.removeAll();
self.selectedContents.push(item);
}
}
ko.applyBindings(new ViewModel());

关于javascript - 如何将 observableArray 中的 observable 复制到另一个 observableArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23987807/

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