问题/问题
我在获取列表编号(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"
}
}
}
嵌套排序插件的 toArray 函数
这是 NestedSortable 插件的 toArray 函数。我确实插入了
“排序索引”:,
在
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 会很好,但我已经找了好几天了,我对如何解决这个问题一无所知。