使用knockout js在点击选项上上下移动数组值?

Ste*_*007 4 javascript jquery knockout.js

我是knockoutjs中的新手,我看到了一个示例,它通过选择选项中索引的下拉值来上下移动数组值.但它的问题是它们不能正确地移动值.更改选择框中的数组位置选项后,将更改..

var viewModel = function() {
  var self = this;

  var Item = function(name, pos) {
    this.name = ko.observable(name);
    this.position = ko.observable(pos);
    var oldPosition = pos;
    this.position.subscribe(function(newValue) {
      self.reposition(this, oldPosition, newValue);
      oldPosition = newValue;
    }, this);
  };

  this.items = [
    new Item("item Three", "3"),
    new Item("item One", "1"),
    new Item("item Two", "2"),
    new Item("item Five", "5"),
    new Item("item Four", "4"),
    new Item("item Six", "6")
  ];


  self.orderedItems = ko.computed(function() {
    return ko.utils.arrayFilter(this.items, function(item) {
      return true;
    }).sort(function(a, b) {
      return a.position() - b.position();
    });
  });

  self.curName = ko.observable();

  self.reposition = function(item, oldPosition, newPosition) {
    console.debug("Reposition", item, oldPosition, newPosition);
  };

};

ko.applyBindings(viewModel);
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div class='liveExample'>
  <ul data-bind="foreach: orderedItems">
    <li>
      <div> <span data-bind="text: name"> </span> has Position:
        <select id="pos" data-bind=" options: orderedItems,
    						optionsText: 'position',
    						optionsValue: 'position',
    						value: position "></select>
      </div>
    </li>
  </ul>
</div>
Run Code Online (Sandbox Code Playgroud)

这是我的示例代码,我想显示数组索引位置应该在下拉列表中显示.我想在下拉列表中选择索引值,数组值的位置应该改变而不是选择.怎么可能与淘汰赛js.

Bud*_*004 6

所以这个比平常更棘手,因为你的索引基于Item的属性.这没有错,它只会增加更多的复杂性.

首先,您必须创建"索引"数组,因为您实际上并未更改项的索引,它们只是根据position属性计算的.

this.items()已更改为observableArray以将项目的更改传播/冒泡到其他功能.现在,您可以包含"添加项"功能,将其添加到items数组中,一切都将正确更新.

我删除了Item构造函数中的subscribe函数,它导致在不需要时触发太多问题.相反,我将一个事件处理程序附加到可以管理项目的选择框,并通过获取value()来删除值的双向绑定.

希望这有帮助,祝你好运!

var viewModel = function() {
  var self = this;

  // UPDATED: Removed the subscribe function
  var Item = function(name, pos) {
    this.name = ko.observable(name);
    this.position = ko.observable(pos);
  };

  // UPDATED: Changed to observable so you can change items here and it will propogate down to the computed functions
  this.items = ko.observable([
    new Item("item Three", "3"),
    new Item("item One", "1"),
    new Item("item Two", "2"),
    new Item("item Five", "5"),
    new Item("item Four", "4"),
    new Item("item Six", "6")
  ]);
  
  // ADDED: Create array of index options based on length
  this.positions = ko.computed(function(){
    var numArray = [];
    for(i = 0; i < self.items().length; i++) {
      numArray.push(i + 1)
    }
    return numArray;
  })


  self.orderedItems = ko.computed(function() {
    return ko.utils.arrayFilter(self.items(), function(item) {
      return true;
    }).sort(function(a, b) {
      return a.position() - b.position();
    });
  });

  self.curName = ko.observable();

  /**
  * UPDATED: Get item at selected position, change it to the current 
  * items position, then update current items position to the selected position;
  */
  self.reposition = function(item, event) {
    var selectedPosition = event.target.value;
    var itemAtPosition = ko.utils.arrayFirst(self.items(), function(i){
      return i.position() === selectedPosition;
    })
    itemAtPosition.position(item.position());
    item.position(event.target.value)
  };

};

ko.applyBindings(viewModel);
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div class='liveExample'>
  <ul data-bind="foreach: orderedItems">
    <li>
      <div> <span data-bind="text: name"> </span> has Position:
        <select id="pos" data-bind=" options: positions(),
    						value: position(), event: { change: reposition} "></select>
      </div>
    </li>
  </ul>
</div>
Run Code Online (Sandbox Code Playgroud)