VUE、快应用 实现多个DIV上下移动随意交换位置

虚拟数据

 SchoolObj: ['a','b','c'],

template模板渲染数据(快应用和vue语法差不多)
$idx:快应用的数据循环索引;
也可以写成for="{{(index,item) in SchoolObj}}"

<div for="{{item in SchoolObj}}" class="colleges-list">
    <text>{{item}}text>
  <text onclick="UpBtn($idx)">上移text>
  <text onclick="DownBtn($idx)">下移text>
div>

事件以及方法

//上移
  UpBtn(idx){
    const that = this;
    //获取当前索引数据
    const linValue = that.SchoolObj[idx];
    //获取当前索引的前一个
    const index = idx - 1;
    //获取数组
    const arr = that.SchoolObj;
    //判断索引
    if ( 0 <= index ) {
          that.swapArray(arr, index, index + 1);
      }

  },
  //下移
  DownBtn(idx){
    const that = this;
    const linValue = that.SchoolObj[idx];
    const index = idx + 1;
    const arr = that.SchoolObj;
    if( index < that.SchoolObj.length) {
        that.swapArray(arr, index, index - 1);
    }
  },
  //数组移动
  //arr:数组、对象;
  //index1:当前索引;
  //index2:当前索引的前或后的索引
  swapArray(arr, index1, index2) {
      arr[index1] = arr.splice(index2, 1, arr[index1])[0];
      return arr;
  },

你可能感兴趣的:(Vue,快应用,JavaScript,javascript,前端,vue)