【问题标题】:nested categories dropdown in magentomagento中的嵌套类别下拉列表
【发布时间】:2014-04-19 09:33:18
【问题描述】:

我在 magento 前端中有以下工作代码,格式为我正在开发的客户“添加产品”功能:

帮手区:

public function getCategoriesDropdown() {

    $categoriesArray = Mage::getModel('catalog/category')
        ->getCollection()
        ->addAttributeToSelect('name')
        ->addAttributeToSort('path', 'asc')
        ->addFieldToFilter('is_active', array('eq'=>'1'))
        ->load()
        ->toArray();


    foreach ($categoriesArray as $categoryId => $category) {
        if (isset($category['name'])) {
            $categories[] = array(
                'label' => $category['name'],
                'level'  =>$category['level'],
                'value' => $categoryId
            );
        }
    }
    return $categories;
}

PHTML 文件:

<select id="category-changer" name="category-changer" style="width:150px;">
        <option value="">--Select Categories--</option>
            <?php
             $_CategoryHelper = Mage::helper("marketplace")->getCategoriesDropdown();
                        foreach($_CategoryHelper as $value){
                            foreach($value as $key => $val){

                                if($key=='label'){
                                    $catNameIs = $val;
                                }
                                if($key=='value'){
                                    $catIdIs = $val;
                                }
                                if($key=='level'){
                                    $catLevelIs = $val;
                                    $b ='';
                                    for($i=1;$i<$catLevelIs;$i++){
                                        $b = $b."-";
                                    }
                                }
                            }
                            ?>
              <option value="<?php echo $catIdIs; ?>"><?php echo $b.$catNameIs ?></option>
                        <?php
                        }
                        ?>
                    </select>

此代码生成一个包含类别和子类别的下拉列表。像这个:

我的主要想法是为子类别创建 n 级嵌套链式下拉列表,如下例所示:

或者这个布局会更好:

任何修改建议的 php 以包含 ajax 调用或 javascript 以生成那些前端链接前端的指导或代码示例将不胜感激

brgds!

【问题讨论】:

    标签: javascript magento


    【解决方案1】:

    这是我的方式:

    在辅助类中,添加方法:

    public function getCategoriesDropdown() {
        $categories = Mage::getModel('catalog/category')
            ->getCollection()
            ->addAttributeToSelect('name')
            ->addAttributeToSort('path', 'asc')
            ->addFieldToFilter('is_active', array('eq'=>'1'));
    
        $first = array();
        $children = array();
        foreach ($categories->getItems() as $cat) {
            if ($cat->getLevel() == 2) {
                $first[$cat->getId()] = $cat;
            } else if ($cat->getParentId()) {
                $children[$cat->getParentId()][] = $cat->getData();
            }
        }
    
        return array('first' => $first, 'children' => $children);
    }
    

    在 PHTML 文件中:

    <?php $tree = $this->helper('xxx')->getCategoriesDropdown(); ?>
    <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 + ')">';
                for (var i = 0; i < options.length; i++) {
                    html += '<option value="' + options[i].entity_id + '">' + options[i].name + '</option>';
                }
                html += '</select>';
                html = '<div id="cat_container_' + level + '">' + html + '</div>';
    
                $('sub_cat').insert(html);
            }
        }
    </script>
    <select id="first_cat" onchange="showCat(this, 2)">
        <?php foreach ($tree['first'] as $cat): ?>
            <option value="<?php echo $cat->getId() ?>"><?php echo $cat->getName() ?></option>
        <?php endforeach ?>
    </select>
    <div id="sub_cat"></div>
    

    【讨论】:

    • 嗨@ndlinh,它似乎适用于 2 个级别 - onchange=showcat(this,2) -,您知道创建 N 级嵌套下拉列表的方法吗?
    • 抱歉耽搁了。我读过但没有测试。我会测试。谢谢!
    • hi ndlinh 您的代码工作正常,但如果我进入第三级然后再次进入第一级,它将在中间显示上一个第二级,无论我选择不同的父级,它都不会删除第二级还有一件事我想为所有第三级添加提交按钮,因为你可以看到如果我添加提交按钮,它将仅适用于第一级stackoverflow.com/questions/34670243/…
    【解决方案2】:
    $rootCategoryId = Mage::app()->getStore()->getRootCategoryId();
    
    /* You can play with this code */
    echo '<select>';
    echo getChildrenCategoryOptions($rootCategoryId);
    echo '</select>';
    /* You can play with this code */
    
    function getChildrenCategoryOptions($categoryId) {
    $html = '';
    $_categoryCollection = Mage::getModel('catalog/category')->load($categoryId)->getChildrenCategories();
    
    if( $_categoryCollection->count() > 0 ) {
        foreach($_categoryCollection as $_category) {
    
            $html .= '<option value="'.$_category->getId().'">'.str_repeat("-", ($_category->getLevel() - 2)).$_category->getName().'</option>';
            $html .= getChildrenCategoryOptions($_category->getId());
        }
        return $html;
    }
    else {
        return '';
    }
    
    }
    

    【讨论】:

      【解决方案3】:
      $rootCategoryId = Mage::app()->getStore()->getRootCategoryId();
      
      $categoriesHierachy = getChildrenCategoryOptions($rootCategoryId);
      
      
      function getChildrenCategoryOptions($categoryId) {
      $html = '';
      $_categoryCollection = Mage::getModel('catalog/category')->load($categoryId)->getChildrenCategories();
      
      if( $_categoryCollection->count() > 0 ) {
          foreach($_categoryCollection as $_category) {
              $array[$_category->getLevel()][$_category->getId()]['name'] = $_category->getName();
              $array[$_category->getLevel()][$_category->getId()]['subcategories'] = getChildrenCategoryOptions($_category->getId());
          }
          return $array;
      }
      else {
          return array();
      }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2015-12-20
        • 1970-01-01
        • 1970-01-01
        • 2014-06-25
        • 2013-04-07
        • 2019-02-26
        • 1970-01-01
        • 1970-01-01
        • 2014-07-02
        相关资源
        最近更新 更多