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.