Laravel实现异步修改分类排序
后台Model方法:
public function getCategories(){
$categories = $this->orderBy('cate_order','asc')->get();
return $this->getTree($categories,'cate_name','cate_id','cate_pid',0);
}
Controller方法:
// change order
public function changeOrder(){
$input = Input::all();
$cate = Category::find($input['cate_id']);
$cate->cate_order = $input['cate_order'];
$res = $cate->update();
if ($res) {
$data=[
'status'=>0,
'msg'=>'分类排序更新成功!',
];
} else {
$data=[
'status'=>1,
'msg'=>'分类排序更新失败,请稍后重试!',
];
}
return $data;
}
前端使用了layerUI框架
<input type="text" onchange="changeOrder(this,{{ $category->cate_id }})" class="order" value="{{ $category->cate_order }}">
<script type="text/javascript">
function changeOrder(obj,cate_id){
var cate_order=$(obj).val();
$.post("{{ url('admin/category/changeorder') }}",{'_token':"{{ csrf_token() }}",'cate_id':cate_id,'cate_order':cate_order},function(data){
if (data.status==0) {
layer.msg(data.msg, {icon: 6});
} else {
layer.msg(data.msg, {icon: 5});
}
});
}
</script>
<script async defer src="{{ asset('admin/assets/layer/layer.js') }}"></script>