laravel - How to arrange <tr> elements of a table using JQuery Sortable -
i new laravel , trying make arrangement of table rows , save position
in database have table cars containing attributes id, name, order(int) attributes.
i have car model , datacontroller containing basic crud functions functions make order:
public function ordercars(request $request){ $caritemorder = json_decode($request->input('order')); $this->orderitems($caritemorder); } private function orderitems(array $caritems) { foreach ($caritems $index => $caritem) { $item = car::findorfail($caritem->id); $item->order = $index + 1; $item->save(); } } i have view
<div class="panel-body"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <script> $(function () { $("tbody").sortable(); $("tbody").disableselection(); }); </script> <table> <thead> <tr> <th>id</th> <th>name</th> </tr> </thead> <tbody> <?php $data = app\car::all();?> @foreach($data $car) <tr class="dd"> <td>{{$car->id}</td> <td>{{ucfirst($car->name)}}</td> </tr> @endforeach </tbody> </table> </div> i have route
route::post('cars/order', ['uses' => 'datacontroller@ordercars', 'as' => 'car.order']); what want order table rows , save order in database don't know how integrate route view , make work
use update of jquery ui sortable , make ajax call save data in database like:
$('#element').sortable({ axis: 'y', update: function (event, ui) { var data = $(this).sortable('serialize'); // post server using $.post or $.ajax $.ajax({ data: data, type: 'post', url: '/your/url/here' }); } }); when use serialize option, create post query string this: item[]=1&item[]=2 etc.
Comments
Post a Comment