如何使用 Knockout.js 将项目添加/插入到 ObservableArray 的某个位置
我发现的所有淘汰示例似乎都使用类似以下内容将新项目添加到 ObservableArray 的末尾:
viewModel.SomeItems.push(someNewItem);
这当然会将项目放在数组的末尾。
如何将项目添加到 ObservableArray 的某个位置?
例如。像这样的东西:
viewModel.SomeItems.push(someNewItem, indexToInsertItAt);
All the knockout examples I have found seem to add a new item to the end of an ObservableArray using something like:
viewModel.SomeItems.push(someNewItem);
This of course places the item at the end of the array.
How to I add an item to the ObservableArray at a certain position?
eg. something like:
viewModel.SomeItems.push(someNewItem, indexToInsertItAt);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够使用本机 JavaScript
splice
方法 -此处的文档 - https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice
此处示例(并非针对淘汰赛) - 如何将项目插入到数组中的特定索引处?
来自淘汰赛文档 -
You should be able to use the native JavaScript
splice
method -Docs here - https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice
Example here (not Knockout specific) - How to insert an item into an array at a specific index?
From the Knockout docs -
对于敲除使用
另请参阅:http://knockoutjs.com/documentation/observableArrays.html
For knockout use
See also: http://knockoutjs.com/documentation/observableArrays.html
我制作了这个扩展功能,它对我来说效果很好。如果我添加到稀疏数组的末尾,则 Splice 对我不起作用。
这甚至适用于:
I made this extension function which worked nicely for me. Splice wasn't working for me if I was adding to the end of a sparse array.
This even works with: