i add a search button on select change but its wor

2019-08-09 08:57发布

问题:

reference: nested categories dropdown in magento

In Helper class

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 File

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


<?php

$catalogSearchHelper =  $this->helper('catalogsearch');
?>


<form id="search_mini_form" action="<?php echo $catalogSearchHelper->getResultUrl() ?>" method="get">

  <select id="first_cat" onchange="showCat(this, 2)" name="<?php echo $catalogSearchHelper->getQueryParamName() ?>" value="<?php echo $catalogSearchHelper->getEscapedQueryText() ?>"  >
    <?php foreach ($tree['first'] as $cat): ?>
        <option value="<?php echo $cat->getId() ?>"><?php echo $cat->getName() ?></option>
    <?php endforeach ?>
</select>

<button type="submit" title="<?php echo $this->__('Search') ?>" class="button"><span><span><?php echo $this->__('Search') ?></span></span></button>

i m using by default catalogSearchHelper but its working for only first level if i select child then it will always shows only parent result u can see below image

标签: magento