2

我想知道如何根据这种类型的数组更新我的数据库:

Array
(
    [0] => Array
        (
            [id] => 58
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 62
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 61
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 60
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 63
                        )

                )

        )

    [2] => Array
        (
            [id] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 59
                        )

                )

        )

)

我尝试了不同的方法,但它没有按应有的方式工作。

这是我保存列表新顺序的函数,我使用nestedSortable('toHierarchy')

function order()
{
    $list = $_POST['list'];
    foreach ($list as $key => $value) {
        if(is_array($value['children'])) {
            $i=1;
            foreach($value['children'] as $k=>$v) {
                $this->section->edit(array('order' => $i, 'parent_id'=>$value['id']), 
                                     "WHERE `id`=" . $v['id']);
                echo 'parent '.$value['id'].'child->'.$v['id'].' order->'.$i.'<br/>';
                $i++;
            }
        }
        else {
            $this->section->edit(array('order' => $key, 'parent_id'=>0), 
                                 "WHERE `id`=" . $value['id']);
        }
    }
}

但它不适用于超过 2 个级别的列表。

这是我正在使用的 js

$('ol.sortable').nestedSortable({
         update : function () {
var orderNew = $(this).nestedSortable('serialize', {startDepthCount: 0});
//alert(orderNew);
        $.ajax({
            type: 'post',
            url: '<?php echo (APP_URL); ?>projects/<?php echo $project_id; ?>/sections/order_list',
            data: {list:orderNew}
            });             
},
            disableNesting: 'no-nest',
            forcePlaceholderSize: true,
            handle: 'div',
            helper: 'clone',
            items: 'li',
            maxLevels: 4,
            opacity: .6,
            placeholder: 'placeholder',
            revert: 250,
            tabSize: 25,
            tolerance: 'move',
            toleranceElement: '> div'
        });
4

1 回答 1

1

您可以实现一个递归函数来处理无限的嵌套级别。但我建议作为使用嵌套排序方法的替代serialize方法:

$('your_sortable_selector').nestedSortable('serialize');

这会生成一个平面数组,其中键是项目 id,每个键的值是父 id。然后,您可以实现一个不太复杂的函数,如下所示:

function order()
{
    $list = $_POST['list'];

    // an array to keep the sort order for each level based on the parent id as the key
    $sort = array();
    foreach ($list as $id => $parentId) {
        /* a null value is set for parent id by nested sortable for root level elements
           so you set it to 0 to work in your case (from what I could deduct from your code) */
        $parentId = ($parentId === null) ? 0 : $parentId;

        // init the sort order value to 1 if this element is on a new level
        if (!array_key_exists($parentId, $sort)) {
            $sort[$parentId] = 1;
        }

        $this->section->edit(array('order' => $sort[$parentId], 'parent_id' => $parentId), "WHERE `id`= $id");

        // increment the sort order for this level
        $sort[$parentId]++;
    }
}

这对于任何数量的嵌套级别都应该同样有效。

于 2012-09-22T18:03:29.420 回答