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 に答える