简体   繁体   English

下拉式嵌套类别具有无限子类别-> Ajax / Laravel

[英]Dropdown nested categories with unlimited subcategories -> Ajax / Laravel

Im trying to create Nested Dropdown categories using the following code: 我正在尝试使用以下代码创建嵌套下拉列表类别:

My DB-> Table product_categories with fields: -id -categoryName -parentId-level -is_active 我的DB->表product_categories具有字段:-id -categoryName -parentId-level -is_active

My Product Entity to retrieve categories: 我的产品实体检索类别:

    public function getAjaxGetCategoriesDropdown() {
        $categories = DB::table('product_categories')
            ->where('is_active', 1)
            ->orderBy('path', 'asc')
            ->select('categoryName','level','id','parentId')
            ->get();

    $first = array();
    $children = array();
    foreach ($categories as $cat) {
        if ($cat->level == 2) {
            $first[$cat->id] = $cat;
        } else if ($cat->parentId) {
            $children[$cat->parentId][] = $cat;
        }
    }
    return array('first' => $first, 'children' => $children);
}

Controller: 控制器:

public function create()
{
    $cats = $this->product->getAjaxGetCategoriesDropdown();
    return View('products/create_product')->with(compact('products','cats'));
}

In View: (create.blade.php) 在视图中:(create.blade.php)

<div class="section">
  <label class="field select">
        <select id="first_cat" class="required-entry validate-select" onchange="showCat(this, 2)">
      <option value=""> Select a Category </option>
        <?php foreach ($tree['first'] as $cat): ?>
      <option value="<?php echo $cat->id ?>"><?php echo $cat->categoryName ?></option>
<?php endforeach ?>
</select>
<i class="arrow double"></i>
</label>
<div id="sub_cat"></div>
</div>

My Script: 我的剧本:

<script type="text/javascript">
    var children = $H(<?php echo json_encode($tree['children']) ?>);

    function showCat(obj, level) {
        var catId = obj.value;
        level += 1;
        if ($('cat_container_' + level)) {
            $('cat_container_' + level).remove();
        }
        if (children.get(catId)) {
            var options = children.get(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].entity_id + '">' + options[i].name + '</option>';
            }
            html += '</select>' + '<i class="arrow double"></i>';
            html = '<label class="field select ' + 'cat_container_' + level + '">' + html + '</label>';

            $('sub_cat').insert(html);
        }
    }
</script>

The current error that the Log displays says: 日志显示的当前错误为:

Undefined Variable Tree in create.blade.php create.blade.php中的未定义变量树

.. code <?php foreach ($tree['first'] as $cat): ?> ..代码<?php foreach ($tree['first'] as $cat): ?>

I've been using this code with success on a Magento app, but now porting the same approach to Laravel is not showing results. 我已经在Magento应用程序上成功使用了此代码,但是现在将相同的方法移植到Laravel并没有显示结果。 Any help appreciated. 任何帮助表示赞赏。

Change 更改

foreach($tree ... 

To

foreach($cats ...

In your view file. 在您的视图文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM