自动展开产品类别树

时间:2012-10-28 11:40:42

标签: javascript magento extjs

我对此感到困惑,基本上我需要的是一种自动扩展包含子类别节点的类别树节点的方法。

代码中的入口点为js/extjs/ext-tree-checkbox.js'catalog/category/tree.phtml'

可以使用expand() js方法扩展节点,并且扩展所有节点并不困难,但这会使页面放慢太多。

更新

我测试了以下解决方案:

  1. 更改js/extjs/ext-tree-checkbox.js渲染方法,添加以下内容:

    var tree = n.getOwnerTree();
        if (tree){
            expandNodeIds =  tree.expandNodeIds.split(',');
            for (i in expandNodeIds) 
            {
                if (n.id == expandNodeIds[i])
                    n.expand();
            }
        }
    
  2. 此解决方案有效,但它会中断(不再显示)允许角色的树

3 个答案:

答案 0 :(得分:4)

我做了一个小错误修复和重构,并将@obscures的答案打包成一个小扩展。试试GitHub:Kiwigrid_AutoExpandProductCategoryTree

答案 1 :(得分:2)

由于:

 - Google sees this as a solution

我的解决方案是在Php方面,在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;
}

现在将上面的代码放在重写类中。

谢谢

答案 2 :(得分:1)

这解决了所有问题

 var tree = n.getOwnerTree();
    if (tree){
        expandNodeIds =  tree.expandNodeIds;

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

使用render

js/extjs/ext-tree-checkbox.js方法添加上述代码