yajra数据表重新排序时想要更新数据库中的位置

问题描述 投票:0回答:0

我正在尝试使用 sortable 方法重新排序,拖放操作正常但位置没有更新

我在其中使用 yajara 数据表的 listUser.blade.php 文件

<div class="container mt-5">
    <h2 class="mb-4">User List</h2>
    <table id ="userTable"class="table table-bordered   yajra-datatables">
        <thead>
            <tr>
                <th>Sr.No</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
                <th>Country Code</th>
                <th>Mobile Number</th>
                <th>Action</th>

            </tr>
        </thead>
        <tbody></tbody>
    </table>
    
</div>
<script type="text/javascript"> 
    $(function(){
        var table =$('#userTable').DataTable({
            processing:true,
            rowReorder: true,
            serverSide:true, 
            ajax:"{{ route('user.list') }}",
            columns:[
                {data:'DT_RowIndex',name:'DT_RowIndex'},
                {data:'first_name',name:'first_name'},
                {data:'last_name',name:'last_name'},
                {data:'email',name:'email',searchable:false},
                {data:'country_code',name:'country_code'},
                {data:'mobile_number',name:'mobile_number'},
                {data:'action',name:'action',searchable:false},
                {data: 'id', name: 'id' , visible: false },
                {data: 'position', name: 'position', visible: false,searchable:true},

            ],
            order: [[ 8, 'asc' ]],
            rowReorder: {
                selector: 'tr',
                
            }
        });

       $('tbody').sortable({
            update: function (event, ui) {
                 var data = $(this).sortable('toArray', {attribute: 'id'});
                alert(data);
                var token = $('meta[name="csrf-token"]').attr('content');
                $.ajax({
                
                  url: "{{ route('user.reorder') }}",
                  type: 'POST',
                  data: {data: data,_token:token},
                  dataType:'json',
                  success: function (response) {
                     console.log(response);
                  },
                  error: function (xhr, status, error) {
                     console.log(xhr.responseText);
                  }   
            
                });
          }
      });

       
    })


</script>

我的 Usercontorller 文件有重新排序的方法来保存数据库中的位置

public function reorder(Request $request)
{
   try{
    
     foreach($request->data as $row)
   {
      User::find($row['id'])->update(['position' => $row['position']]);
   }
  return response();
}
catch(Exception $e){
    print_r($e->getMessage());
}

}

这是特定路线

  Route::post('user/reorder', 'reorder')->name('user.reorder');
php mysql ajax laravel yajra-datatable
© www.soinside.com 2019 - 2024. All rights reserved.