【问题标题】:Dropdown nested categories with unlimited subcategories -> Ajax / Laravel具有无限子类别的下拉嵌套类别 -> Ajax / Laravel
【发布时间】:2019-02-26 06:06:02
【问题描述】:

我正在尝试使用以下代码创建嵌套下拉类别:

我的数据库-> 表 product_categories 的字段: -ID -分类名称 -parentId 级别 -is_active

我的产品实体来检索类别:

    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);
}

控制器:

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

在视图中:(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>

我的脚本:

<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>

日志显示的当前错误说:

create.blade.php 中的未定义变量树

..代码&lt;?php foreach ($tree['first'] as $cat): ?&gt;

我一直在 Magento 应用程序上成功使用此代码,但现在将相同的方法移植到 Laravel 并没有显示结果。任何帮助表示赞赏。

【问题讨论】:

标签: php ajax laravel cascadingdropdown


【解决方案1】:

改变

foreach($tree ... 

foreach($cats ...

在您的视图文件中。

【讨论】:

  • 是的,完美!我会尽快接受答案。谢谢!
  • 另外,您正试图从控制器方法将“产品”传递给您的视图,但您没有在那里定义它。
  • 是的,但那部分在模型中。我的问题在于您的建议。
  • 出于某种原因 $('sub_cat').insert(html);没有插入任何错误的东西。很抱歉打扰您,但是您能在脚本中看到任何可能出错的内容吗?谢谢
  • 尝试在该行之前控制台记录您的 html var,以确保它进入该 if 语句并按照您想要的方式进行设置
猜你喜欢
  • 2018-09-10
  • 1970-01-01
  • 2021-04-29
  • 2020-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多