代码之家  ›  专栏  ›  技术社区  ›  s_h

带有无限子类别的Laravel下拉列表嵌套类别级别给定错误

  •  1
  • s_h  · 技术社区  · 6 年前

    老实说,我不知道如何从儿童类别中获得价值观,我相信这是问题的主要根源。但我不知道怎么解决

    TypeError:children不是函数[Learn More]

    这是我的数据库和代码:

    DB:结构

    enter image description here

    public function getAjaxGetCategoriesDropdown() {
        $categories = DB::table('categories')
            ->where('is_active', 1)
            ->orderBy('path', 'asc')
            ->select('categoryName','level','id','parentId')
            ->get();
    
        $first = array();
        $children = array();
        foreach ($categories as $cats) {
            if ($cats->level == 2) {
                $first[$cats->id] = $cats;
            } else if ($cats->parentId) {
                $children[$cats->parentId][] = $cats;
            }
        }
        return array('first' => $first, 'children' => $children);
    }
    

    脚本:

    <script type="text/javascript">
      var children = @json($cats['children'])
    
      function showCat(obj, level) {
          var catId = obj.value;
            level += 1;
              if ($('cat_container_' + level)) {
                 $('cat_container_' + level).remove();}
                 var options = children(catId);
                 var html = '<select id="cat_' + catId + '" onchange="showCat(this, ' + level + ')">' + '<option value="">Select a SubCategory</option>';
             for (var i = 0; i < options.length; i++) {
                html += '<option value="' + options[i].id + '">' + options[i].categoryName + '</option>';}
                html += '</select>' + '<i class="arrow double"></i>';
                html = '<label class="field select ' + 'cat_container_' + level + '">' + html + '</label>';
    $('sub_cat').insert(html);
    

    查看刀片:

    <select id="first_cat" class="required-entry validate-select" onchange="showCat(this, 2)">
            <option value=""> Select a Category </option>
            @foreach($cats['first'] as $cat): <option value="{{ $cat->id }}">{{ $cat->categoryName }}</option> @endforeach
    </select>
    <div id="sub_cat"></div>
    

    谢谢你的帮助!。

    1 回复  |  直到 6 年前
        1
  •  1
  •   whmkr    6 年前

    改变

    var options = children(catId);
    

    var options = children[catId];