splice()不更新心跳.js中数组的项目顺序 [英] splice() not updating items order of array within knockout.js

查看:180
本文介绍了splice()不更新心跳.js中数组的项目顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前的帖子中更新数组的顺序.我遵循了迈克尔·贝斯特(Michael Best)的建议,并使用splice()修改了按钮单击时我的数组的顺序

Following a prior post on how to update the order of an array. I followed the suggestion of Michael Best and used splice() to modify the ordering of my array on button click

self.moveup = function (itemIndex) {
    var i = self.itemList.indexOf(itemIndex); 
    if(i >= 1){
     var array = self.itemList();
        self.itemList.splice(i-1, 2, array[i], array[i-1]);
    } 

我遇到麻烦的地方是增加数组中的项目. 通过阅读Array Splice的用法第一个参数指示向上移动应发生的更改,我认为应为i+1,值2指示数组中将更改多少个项目,因此在那里没有更改,然后我认为范围将是所选项目array [i],随着我增加位置,结尾将是[i + 1].

Where I am having trouble is in incrementing the items in the array. From reading the usage of Array Splice The first param indicates where the change should occur for moving up I would think that would be i+1 , the value 2 indicates how many items in the array would change so no change there and then the range I thought would be the selected item array[i] and the end would be [i+1] as I am increasing the position.

self.itemList.splice(i+1, 2, array[i], array[i+1]);

在所附的提琴手中,您可以看到值增加了,但是项实际上并没有改变顺序,只是当您按下向下"按钮时它们才被复制.我希望结果与调用moveUp时的结果相同.

In the attached fiddler you can see the values increase but the items do not actually change order they are only replicating when you hit the down button. I expected the result to be the same as when calling moveUp.

对于任何我在这里缺少的内容,我将不胜感激. http://jsfiddle.net/rlcrews/SCWmk/5/

I'd appreciate any pointers on what I am missing here. http://jsfiddle.net/rlcrews/SCWmk/5/

欢呼声

推荐答案

快到了. 这是我的操作方式.

向上移动项目时,您需要将其与上一个项目交换.因此,您需要分别用array[i]array[i-1]替换索引i-1i处的元素.您的moveup方法正是这样做的,所以一切都很好.

When moving an item up, you need to swap it with the previous item. Thus, you need to replace the elements at indices i-1 and i with array[i] and array[i-1], respectively. Your moveup method does exactly that, so all is good.

现在,当向下移动项目时,您需要将其与 next 项目互换.因此,分别用array[i+1]array[i]替换索引ii+1处的元素.但是,您的代码更改了元素i+1i+2,这是不好的.相反,您应该这样做:

Now, when moving an item down, you need to swap it with the next item. Thus, you replace the elements at indices i and i+1 with array[i+1] and array[i], respectively. Your code however changes the elements i+1 and i+2, which is no good. Instead, you should be doing:

self.itemList.splice(i, 2, array[i+1], array[i]);

您从i开始拼接(因为要删除ii+1上的元素),然后将它们(在该索引处插入)替换为array[i+1]array[i].

You start splicing at i (as you're removing the elements at i and i+1) and you replace them (insert at that index) with array[i+1] and array[i].

另一方面,您是否可以向下移动该项目的检查不正确.您唯一不能向下移动的项目是最后一项,即索引为self.itemList().length-1的元素.因此,支票的外观应类似于if (i < array.length - 1) { ... }(请参见小提琴).

On another note, your check for whether you can move the item down is incorrect. The only item you should not move down is the last item, i.e. the element at index self.itemList().length-1. Thus, the check should look like if (i < array.length - 1) { ... } (see the fiddle).

这篇关于splice()不更新心跳.js中数组的项目顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆