我尝试为我的类别制作可排序的函数,我正在使用
JQuery UI
逻辑
>如果category_id为null,则表示该类别为parent.
(就我而言,我只使用了category_id而不是parent_id
不同的命名)
>类别最大2深,如父母 – >儿童1->儿童1
孩子的
我尝试做什么
当我通过Ajax删除和删除我的一个类别时更新category_id列
问题
>我在网络上收到404错误
>我的函数不支持null category_id
阿贾克斯
@H_403_20@$(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); } }); } }); });路线
@H_403_20@Route::post('/categorysort/{id}','CategoryController@UpdatecategoryparentByAjax')->name('categorysort');调节器
@H_403_20@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,]); } }PS: I’m aware that there is missing
category_id
data in myli's
the
reason i didn’t put that is because I didn’t know how to should I use
it exactly,as I mentioned before in my issues my function doesn’t
support that yet (so I need your helps please).
截图
这可能是因为你没有保护agaisnt csrf,这在执行post请求时是强制性的,因为
laravel docs解释你可以通过将它添加到你的html来解决:
@H_403_20@<Meta name="csrf-token" content="{{ csrf_token() }}">
@H_403_20@$.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},所以不要选择所有类别,只需选择顶级父类别:
@H_403_20@$.map($(this).find('.parent'),function(el)