简体   繁体   English

交换observableArray中的2个项目-剔除

[英]Swap 2 items in observableArray - knockout

im tring to Replace between 2 items in observableArray with knockout but something is wrong.. 正在尝试用剔除替换observableArray中的2个项目之间的内容,但是出了点问题。

after the replace of the items ,i will change and send the displayOrder property (in both itmems) to the server (or should i take other approach for this) 替换项目后,我将进行更改并将displayOrder属性(在两个itmems中)发送到服务器(或者我应该采用其他方法)

        ReplaceBetweenTwoitemsInArray: function () {
            console.log("ranking down msg");
            var currentItemindex = viewModel.myobservableArray.indexOf(this); 
            var nextItemIndex = currentItemindex + 1;
            viewModel.myobservableArray .replace(
                 viewModel.myobservableArray ()[nextItemIndex], 
                viewModel.myobservableArray ()[currentItemindex]
             );

        }

only the first item changed to the second item but the second item doesnt become the first one 只有第一项更改为第二项,但第二项没有成为第一项

Similar to Paoli's answer, but will also trigger updates to observables, could be DRYer. 与Paoli的答案类似,但也会触发可观察值的更新,可能是DRYer。

http://jsfiddle.net/marrok/ckMJE/101/ http://jsfiddle.net/marrok/ckMJE/101/

<ul data-bind="foreach: colors">
    <li><span data-bind="text:color"></span>
    </li>
</ul>
<br/>
<span>From:</span><input type="text" data-bind="value:from"/>
<br/>
<span>TO:</span><input type="text" data-bind="value:to"/>
<br/>
<button data-bind="click:swap">Swap It</button>​

Javascript: 使用Javascript:

var ViewModel = function() {
    this.self = this;
    self.from = ko.observable(0); // default
    self.to = ko.observable(1); // default
    self.colors = ko.observableArray([{
        color: 'red'},
    {
        color: 'green'},
    {
        color: 'pink'},
    {
        color: 'blue'},
    {
        color: 'yellow'}]);

    self.swap= function() {
        var iTo = parseInt(self.to());
        var iFrom = parseInt(self.from());
        var from = self.colors()[iFrom];
        var to = self.colors()[iTo];
        console.log("Before", self.colors().map(function(d){return d.color;} ), from, to, iFrom, iTo)
        self.colors()[iTo] = from;
        self.colors()[iFrom] = to;
        console.log("After ", self.colors().map(function(d){return d.color;} ), from, to, iFrom, iTo)
        self.colors.valueHasMutated()

    };
};
model = new ViewModel()
ko.applyBindings(model);​

You can use a temporary variable: 您可以使用一个临时变量:

var arr = ko.observableArray([0, 1])

// Should produce arr() = [0, 1]

var tmp = arr()[0];

arr()[0] = arr()[1];
arr()[1] = tmp;

// At this point, arr() is [1, 0]

You can just use remove and splice functions directly on the observableArray like this: 您可以像这样直接在observableArray上直接使用remove和splice函数:

var arr=ko.observableArray(["x","y"]);
var index=arr.indexOf("y");
var tmp=arr()[index-1];
arr.remove(tmp);
arr.splice(index,0,tmp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM