使用敲除js在单击选项上上下移动数组值?

时间:2018-11-30 06:26:16

标签: javascript jquery knockout.js

我是基因敲除的新手,我看到了一个示例,该示例通过在选项中选择索引的下拉值来上下移动数组值。但是它的问题是它们没有正确地移动值。更改阵列位置后,选择框中的选项将被更改。

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);
<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>

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

1 个答案:

答案 0 :(得分:6)

因此,由于您将索引基于Item的属性,因此此操作比通常的操作更为棘手。没错,它只会增加更多的复杂性。

首先,您必须创建“索引”数组,因为您实际上并未更改项目的索引,它们只是从position属性中计算出来的。

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

我删除了Item构造函数中的subscription函数,这在不需要时引发了太多问题。相反,我将事件处理程序附加到可以管理项目的选择框,并通过获取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);
<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>