ajax – 在laravel中的null id上可排序
|
我尝试为我的类别制作可排序的函数,我正在使用
JQuery UI
逻辑 >如果category_id为null,则表示该类别为parent. 我尝试做什么 当我通过Ajax删除和删除我的一个类别时更新category_id列 问题 >我在网络上收到404错误 代码 @foreach($Categorys as $cat)
//parent (deep 0)
<li class="parent" id="{{$cat->id}}">
{{$cat->title}}
//first child (deep 1)
@if(count($cat->childs))
@foreach($cat->childs as $child)
<li style="margin-left:20px !important;" class="firstchild" id="{{$child->id}}">
{{$child->title}}
//first child child (deep 2)
@if(count($child->childs))
@foreach($child->childs as $childdd)
<li style="margin-left:40px !important;" class="secondchild" id="{{$childdd->id}}">
{{$childdd->title}}
</li>
@endforeach
@endif
</li>
@endforeach
@endif
</li>
@endforeach
阿贾克斯 $(function() {
$('.mytest').sortable({
stop: function() {
$.map($(this).find('li'),function(el) {
var itemID = el.id;
var itemIndex = $(el).index();
if (itemID) {
$.ajax({
url: '{{ url('
categorysort ') }}/' + encodeURI(itemID),type: 'POST',dataType: 'json',data: {
itemID: itemID,itemIndex: itemIndex
},});
} else {
console.log(data);
}
});
}
});
});
路线 Route::post('/categorysort/{id}','CategoryController@UpdatecategoryparentByAjax')->name('categorysort');
调节器 public function UpdatecategoryparentByAjax(Request $request,$id)
{
$categories = Category::orderby('id','desc')->get();
$itemID = $request->input('itemID');
$itemIndex = $request->input('itemIndex');
foreach ($categories as $category) {
$category->where('id',$itemId)->update([
'category_id' => $itemIndex,]);
}
}
截图
这可能是因为你没有保护agaisnt csrf,这在执行post请求时是强制性的,因为
laravel docs解释你可以通过将它添加到你的html来解决:
<meta name="csrf-token" content="{{ csrf_token() }}">
然后在你的ajax中你只需要将它作为标题添加. $.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},url: '/categorysort/'+ itemID,data: {itemID: itemID,itemIndex: itemIndex},});
另外你只收到1个id,所以我想你要做的是根据父母排序,因此你应该只选择父类的li,这样你的控制器就会收到1个id而不是2个因为你是什么现在做的是/ categorysort / {parentId} / {childId},你需要的是/ categorysort / {id},所以不要选择所有类别,只需选择顶级父类别: $.map($(this).find('.parent'),function(el)
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
