我在获取列表编号时遇到问题( sortIndex )。获得类似“示例列表”中的sortIndex之后,我想将sortIndex赋予toArray函数。所以当这个函数确实创建一个数组时,有一个sortIndex init。请参阅“阵列示例”
使用的jQuery插件: mjsarfatti jQuery nestedSortable
以HTML格式列出:
<ol class="sortable ui-sortable">
<li id="category_1"><div>Car</div>
<ol>
<li id="category_2"><div>Color</div>
<ol>
<li id="category_3"><div>Red</div></li>
<li id="category_4"><div>Black</div></li>
</ol>
</li>
</ol>
</li>
<li id="category_5"><div>Motor</div></li>
<li id="category_6"><div>Truck</div></li>
</ol>
列出示例,为您提供上述列表的视图。
[sortIndex] [Name]
1. Car
1. Color
1. Red
2. Black
2. Motor
3. Truck
使用nestedSortable toArray,将sortIndex添加到array()会很不错,如下所示:
["storage":"ArrayObject":private]=>
array(1) {
["newList"]=>
array(7) {
[0]=> //This is the OL
array(5) {
["item_id"]=>
string(0) ""
["parent_id"]=>
string(4) "none"
["depth"]=>
string(1) "0"
["left"]=>
string(1) "1"
["right"]=>
string(2) "38"
}
[1]=> // Car
array(6) {
["item_id"]=>
string(1) "1"
["parent_id"]=>
string(0) ""
["sort_index"]=>
string(1) "1"
["depth"]=>
string(1) "1"
["left"]=>
string(1) "2"
["right"]=>
string(1) "9"
}
[2]=> // Color
array(6) {
["item_id"]=>
string(1) "2"
["parent_id"]=>
string(1) "1"
["sort_index"]=>
string(1) "1"
["depth"]=>
string(1) "2"
["left"]=>
string(1) "3"
["right"]=>
string(1) "8"
}
[3]=> // Red
array(6) {
["item_id"]=>
string(1) "3"
["parent_id"]=>
string(1) "2"
["sort_index"]=>
string(1) "1"
["depth"]=>
string(1) "3"
["left"]=>
string(1) "4"
["right"]=>
string(1) "5"
}
[4]=> // Black
array(6) {
["item_id"]=>
string(1) "4"
["parent_id"]=>
string(1) "2"
["sort_index"]=>
string(1) "2"
["depth"]=>
string(1) "3"
["left"]=>
string(1) "6"
["right"]=>
string(1) "7"
}
[5]=> // Motor
array(6) {
["item_id"]=>
string(2) "5"
["parent_id"]=>
string(0) ""
["sort_index"]=>
string(1) "1"
["depth"]=>
string(1) "1"
["left"]=>
string(2) "10"
["right"]=>
string(2) "11"
}
[6]=> // Truck
array(6) {
["item_id"]=>
string(2) "6"
["parent_id"]=>
string(0) ""
["sort_index"]=>
string(1) "1"
["depth"]=>
string(1) "1"
["left"]=>
string(2) "10"
["right"]=>
string(2) "11"
}
}
}
这是NestedSortable插件的toArray函数。我确实插入了
“sort_index”:,
在
ret.push()
toArray功能:
toArray: function(options) {
var o = $.extend({}, this.options, options),
sDepth = o.startDepthCount || 0,
ret = [],
left = 2;
ret.push({
"item_id": o.rootID,
"parent_id": 'none',
"depth": sDepth,
"left": '1',
"right": ($(o.items, this.element).length + 1) * 2
});
$(this.element).children(o.items).each(function () {
left = _recursiveArray(this, sDepth + 1, left);
});
ret = ret.sort(function(a,b){ return (a.left - b.left); });
return ret;
function _recursiveArray(item, depth, left) {
var right = left + 1,
id,
pid;
if ($(item).children(o.listType).children(o.items).length > 0) {
depth ++;
$(item).children(o.listType).children(o.items).each(function () {
right = _recursiveArray($(this), depth, right);
});
depth --;
}
id = ($(item).attr(o.attribute || 'id')).match(o.expression || (/(.+)[-=_](.+)/));
if (depth === sDepth + 1) {
pid = o.rootID;
} else {
var parentItem = ($(item).parent(o.listType)
.parent(o.items)
.attr(o.attribute || 'id'))
.match(o.expression || (/(.+)[-=_](.+)/));
pid = parentItem[2];
}
if (id) {
ret.push({"item_id": id[2], "parent_id": pid, "sort_index": , "depth": depth, "left": left, "right": right});
}
left = right + 1;
return left;
}
},
就像我说的那样,给toArray函数提供一个sort_Index会很好,但我现在已经找了好几天,而且我对如何解决这个问题一无所知。
答案 0 :(得分:0)
编辑jQuery插件。
将此行添加到_recursiveArray(项目,深度,左侧)函数:
si = ($(item).index(o.item));
定义var si:
var right = left + 1,
id,
pid,
si;
在ret.push中添加你的变量:
ret.push({"item_id": id[2], "parent_id": pid, "sort_index": si+1, "depth": depth, "left": left, "right": right});
所以它看起来像这样:
toArray: function(options) {
var o = $.extend({}, this.options, options),
sDepth = o.startDepthCount || 0,
ret = [],
left = 2;
ret.push({
"item_id": o.rootID,
"parent_id": 'none',
"sort_index" : this.element.index(this.element),
"depth": sDepth,
"left": '1',
"right": ($(o.items, this.element).length + 1) * 2
});
$(this.element).children(o.items).each(function () {
left = _recursiveArray(this, sDepth + 1, left);
});
ret = ret.sort(function(a,b){ return (a.left - b.left); });
return ret;
function _recursiveArray(item, depth, left) {
var right = left + 1,
id,
pid,
si;
if ($(item).children(o.listType).children(o.items).length > 0) {
depth ++;
$(item).children(o.listType).children(o.items).each(function () {
right = _recursiveArray($(this), depth, right);
});
depth --;
}
id = ($(item).attr(o.attribute || 'id')).match(o.expression || (/(.+)[-=_](.+)/));
if (depth === sDepth + 1) {
pid = o.rootID;
} else {
var parentItem = ($(item).parent(o.listType)
.parent(o.items)
.attr(o.attribute || 'id'))
.match(o.expression || (/(.+)[-=_](.+)/));
pid = parentItem[2];
}
si = ($(item).index(o.item));
if (id) {
ret.push({"item_id": id[2], "parent_id": pid, "sort_index": si+1, "depth": depth, "left": left, "right": right});
}
left = right + 1;
return left;
}
},