Javascript 自动展开产品类别树
我陷入了这个困境,基本上我需要的是一种方法来自动展开类别树节点,其中包含一个子类别节点 代码中的入口点是Javascript 自动展开产品类别树,javascript,magento,extjs,Javascript,Magento,Extjs,我陷入了这个困境,基本上我需要的是一种方法来自动展开类别树节点,其中包含一个子类别节点 代码中的入口点是js/extjs/exttree checkbox.js和'catalog/category/tree.phtml' 可以使用expand()js方法在中展开节点,展开所有节点并不困难,但这会大大降低页面速度 更新: 我已经测试了以下解决方案: 更改js/extjs/ext-tree checkbox.jsrender方法添加以下内容: var tree = n.getOwnerTree();
js/extjs/exttree checkbox.js
和'catalog/category/tree.phtml'
可以使用expand()
js方法在中展开节点,展开所有节点并不困难,但这会大大降低页面速度
更新:
我已经测试了以下解决方案:
js/extjs/ext-tree checkbox.js
render方法添加以下内容:
var tree = n.getOwnerTree();
if (tree){
expandNodeIds = tree.expandNodeIds.split(',');
for (i in expandNodeIds)
{
if (n.id == expandNodeIds[i])
n.expand();
}
}
此解决方案可以工作,但它会破坏(不再显示)权限角色的树此解决方案解决了所有问题
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();
}
}
在js/extjs/ext-tree checkbox.js的render
方法中添加上述代码,因为:
- 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;
}
现在将在重写类中使用上述代码
谢谢你我做了一点bug修复和重构,并将@Obscles answer打包成一个小扩展。在GitHub上试一试:到目前为止,您尝试了什么?介绍似乎有点不合适您测试过这个吗?在render
方法中将其添加到何处?此外,您似乎在结尾缺少一个分号。我在CE1.7.0.2中测试过这个,但它似乎不起作用。当时,我成功地使用了它。。无论如何,这可能是解决方案的一个很好的切入点,如果您发现任何问题,您可以改进答案或添加新的答案