Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Expand Product Category Tree

I got stuck on this, basically what I need is a way to auto expand category tree node that contains a sub category node checked.

The entry point in the code is js/extjs/ext-tree-checkbox.js and 'catalog/category/tree.phtml'

It is possible to a expand a node in with expand() js method and it is no difficult to expand all node but this slow down the page too much.

Update:

I have tested the following solution:

  1. Change js/extjs/ext-tree-checkbox.js render method adding this:

    var tree = n.getOwnerTree();
        if (tree){
            expandNodeIds =  tree.expandNodeIds.split(',');
            for (i in expandNodeIds) 
            {
                if (n.id == expandNodeIds[i])
                    n.expand();
            }
        }
    

This solution works but It breaks ( it is not displayed anymore ) the tree for permission Role

like image 205
WonderLand Avatar asked Oct 28 '12 11:10

WonderLand


2 Answers

I did a little bug fixing and refactoring and packaged @obscures answer into a small extension. Give it a try on GitHub: Kiwigrid_AutoExpandProductCategoryTree

like image 78
jmk Avatar answered Sep 22 '22 20:09

jmk


Because:

 - Google sees this as a solution

My solution is in Php side, in class Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Categories.

// start Added stuff
protected $_ubProductCategoriesParents;

public function getUbProductCategories()
{
    if (is_null($this->_ubProductCategoriesParents)) {
        $this->_ubProductCategoriesParents = array();
        $this->_ubProductCategoriesParents = array();
        foreach ($this->getCategoryIds() as $id) {
            $storeId = (int) $this->getRequest()->getParam('store');
            $path = Mage::getResourceModel('catalog/category')
                ->getAttributeRawValue($id, 'path', $storeId); // no model->load ever ever ever
            if (empty($path)) {
                $path = "";
            }
            $parentsIds = explode("/", $path);
            if (!(is_array($parentsIds) && count($parentsIds) > 0)) {
                $parentsIds = array();
            }
            $this->_ubProductCategoriesParents = array_unique(array_merge($this->_ubProductCategoriesParents, $parentsIds));
        }

        //Mage::log(print_r($this->_ubProductCategoriesParents, true));
    }

    return $this->_ubProductCategoriesParents;
}
// end Added stuff

// magento core function from class
protected function _getNodeJson($node, $level = 1)
{
    $item = parent::_getNodeJson($node, $level);

    if ($this->_isParentSelectedCategory($node)) {
        $item['expanded'] = true;
    }

    // added new if statement
    if (!(isset($item['expanded']) && $item['expanded'] == true) && array_search($node->getId(), $this->getUbProductCategories()) !== false) {
        $item['expanded'] = true;
    }

    if (in_array($node->getId(), $this->getCategoryIds())) {
        $item['checked'] = true;
    }

    if ($this->isReadonly()) {
        $item['disabled'] = true;
    }

    return $item;
}

Going now to have the above code in a rewrite class.

Thank you

like image 34
Daniel Ifrim Avatar answered Sep 18 '22 20:09

Daniel Ifrim