俩个方法:
actomicOrderSort:主要提取出需要操作的俩个对象:orderState为操作状态(1,表示前移一位。2,表示后移一位),atomicorder需要操作的对象的当前排序。
changeAtomicOrder:将actomicOrderSort()方法中提取出的俩个需要操作的对象,将其从list中删除,然后重新push进去。
/*原子排序*/
function actomicOrderSort(orderState, atomicorder) {
var nowAtomic = {};
var willAtomic = {};
for (var i = 0; i < atomicList.length; i++) {
var atomic = atomicList[i];
if (orderState == 1) {
/*表示原子前移*/
if (atomicorder - 1 < 1) {
layer.msg("已经是最前面的原子了!", {time: 1500});
return;
}
if (atomic.order == atomicorder - 1) {
willAtomic.order = atomic.order + 1;
willAtomic.name = atomic.name;
willAtomic.id = atomic.id;
willAtomic.content = atomic.content;
willAtomic.keyword = atomic.keyword;
}
if (atomic.order == atomicorder) {
nowAtomic.order = atomic.order - 1;
nowAtomic.name = atomic.name;
nowAtomic.id = atomic.id;
nowAtomic.content = atomic.content;
nowAtomic.keyword = atomic.keyword;
}
} else if (orderState == 2) {
/*表示原子后移*/
if (atomicorder + 1 > atomicList.length) {
layer.msg("已经是最后的原子了!", {time: 1500});
return;
}
if (atomic.order == atomicorder) {
nowAtomic.order = atomic.order + 1;
nowAtomic.name = atomic.name;
nowAtomic.id = atomic.id;
nowAtomic.content = atomic.content;
nowAtomic.keyword = atomic.keyword;
}
if (atomic.order == Number(atomicorder) + Number(1)) {
willAtomic.order = atomic.order - 1;
willAtomic.name = atomic.name;
willAtomic.id = atomic.id;
willAtomic.content = atomic.content;
willAtomic.keyword = atomic.keyword;
}
}
}
changeAtomicOrder(atomicList, nowAtomic, willAtomic);
type = 1;
actomicStr = JSON.stringify(atomicList);
showAtomic();
}
function changeAtomicOrder(atomicList, nowAtomic, willAtomic) {
for (var i = 0; i < atomicList.length; i++) {
var atomicListElement = atomicList[i];
if (atomicListElement.id == nowAtomic.id) {
atomicList.splice(i, 1);
}
}
atomicList.push(nowAtomic);
for (var i = 0; i < atomicList.length; i++) {
var atomicListElement = atomicList[i];
if (atomicListElement.id == willAtomic.id) {
atomicList.splice(i, 1);
}
}
atomicList.push(willAtomic);
}