dpp34603 2015-10-05 11:45
浏览 36

Symfony和jQuery Sortable集成无法正常工作

I have implemented jquery sortable in twig and when I drag and drop I need to send the updated order via ajax to the symfony controller.

Inside my twig i am using a loop to display the sortable items

{% for goals in form.goals %}
    <li class="ui-state-default" id="item-{{ goals.id.vars.value }}">
        <div class="row">
            <div class="col-xs-3"></div>
            <div class="col-xs-2" >
                <label class="" for="exampleInputEmail2">Goal</label>

                <div class="form-group input-group">
                    {{ form_widget(goals.target, {'attr': {'class': 'form-control'}}) }}
                </div>

            </div>
            <div class="col-xs-2">
                <label class="" for="exampleInputEmail2">Saved to date</label>

                <div class="form-group input-group">
                    {{ form_widget(goals.reached, {'attr': {'class': 'form-control'}}) }}
                </div>
            </div>
            <div class="col-xs-2">
                <label class="" for="exampleInputEmail2">Goal deadline</label>

                <div class="form-group input-group">
                    {{ form_widget(goals.deadline, {'attr': {'class': 'form-control dp'}}) }}
                </div>
            </div>
            <div class="col-xs-2">
                <label class="" for="exampleInputEmail2">Allocated $</label>

                <div class="form-group input-group">
                    {{ form_widget(goals.allocated, {'attr': {'class': 'form-control'}}) }}
                </div>

            </div>
        </div>

    </li>
{% endfor %}

This is the AJAX call

$( "#sortable" ).sortable({
    axis: 'y',
    stop: function (event, ui) {
        var order = $(this).sortable('serialize');
        $.ajax({
            data: order,
            type: 'POST',
            url: '{{ path('goals_ajax') }}'
        });
    }
});

The problem I am having is how can I get hold of this order array in my controller

I have tried

$data = $request->request->get('order');
$data = $request->get('order');

But these give me null

I am guessing once I have that order array I should be able to use the following foreach loop to update the order

    foreach ($order as $field => $value) {
        $order = $field;
        $id = $value;
        $goal = $this->getDoctrine()->getRepository('MyBundle:Goals')->find($id);
        $goal->setOrderNo($order);
        $em->persist($goal);
    }
    $em->flush();

Any help will really be appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 个人网站被恶意大量访问,怎么办
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制