加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax – 在laravel中的null id上可排序

发布时间:2020-12-16 01:36:18 所属栏目:百科 来源:网络整理
导读:我尝试为我的类别制作可排序的函数,我正在使用 JQuery UI 逻辑 如果category_id为null,则表示该类别为parent. (就我而言,我只使用了category_id而不是parent_id 不同的命名) 类别最大2深,如父母 – 儿童1-儿童1 孩子的 我尝试做什么 当我通过Ajax删除和删除
我尝试为我的类别制作可排序的函数,我正在使用 JQuery UI

逻辑

>如果category_id为null,则表示该类别为parent.
(就我而言,我只使用了category_id而不是parent_id
不同的命名)
>类别最大2深,如父母 – >儿童1->儿童1
孩子的

我尝试做什么

当我通过Ajax删除和删除我的一个类别时更新category_id列

问题

>我在网络上收到404错误
>我的函数不支持null category_id

代码

@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,]);
      }
    }

PS: I’m aware that there is missing category_id data in my li'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来解决:
<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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读