Knockout move observableArray item down

不打扰是莪最后的温柔 提交于 2020-01-13 15:03:47

问题


From this question, i know how to move an item up:

moveUp: function(category) {
    var i = categories.indexOf(category);
    if (i >= 1) {
        var array = categories();
        categories.splice(i-1, 2, array[i], array[i-1]);
    }
}

What is the equivalent for moving the item down?


回答1:


In this solution, i delete two items, from index position, and then again insert them (first - the next item, and the second - moving item):

moveDown = function(number) {
    var i = self.numbers().indexOf(number);
    if (i < self.numbers().length - 1) {
        var rawNumbers = self.numbers();
        self.numbers.splice(i, 2, rawNumbers[i + 1], rawNumbers[i]);
    }
}

Demo



来源:https://stackoverflow.com/questions/22347977/knockout-move-observablearray-item-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!