Php Magento Adminhtml:带有类别选择器的类别属性
我正在尝试实现一个category属性,用于将当前类别与其他类别链接起来,以实现特定于项目的用途 为此,我创建了一个varchar属性,用于存储分类ID的分隔列表,但我希望在显示分类选择器的字段旁边有一个小的选择器图标,就像升级管理屏幕的条件部分中的图标一样 我真的不知道我应该实现什么样的渲染器来实现这一点,我希望你们能给我一个提示。Php Magento Adminhtml:带有类别选择器的类别属性,php,magento,adminhtml,Php,Magento,Adminhtml,我正在尝试实现一个category属性,用于将当前类别与其他类别链接起来,以实现特定于项目的用途 为此,我创建了一个varchar属性,用于存储分类ID的分隔列表,但我希望在显示分类选择器的字段旁边有一个小的选择器图标,就像升级管理屏幕的条件部分中的图标一样 我真的不知道我应该实现什么样的渲染器来实现这一点,我希望你们能给我一个提示。 谢谢你的帮助。我终于做到了,就这样: 仅供参考:我的category属性名为“category\u top\u searches”,是一个单独的类别ID列表。选
谢谢你的帮助。我终于做到了,就这样: 仅供参考:我的category属性名为“category\u top\u searches”,是一个单独的类别ID列表。选取者旨在帮助贡献此列表 1-使用观察者添加选项卡 A。观察员声明
<adminhtml_catalog_category_tabs>
<observers>
<add_topsearches_tab>
<class>mymodule/observer</class>
<method>addTopSearchesTab</method>
</add_topsearches_tab>
</observers>
</adminhtml_catalog_category_tabs>
<catalog_category_save_before>
<observers>
<set_top_searches>
<class>mymodule/observer</class>
<method>setTopSearches</method>
</set_top_searches>
</observers>
</catalog_category_save_before>
2-定义选项卡的块
初始化树时,需要实现选项卡接口方法和获取选中类别的方法
class MyNamespace_Adminhtml_Block_Catalog_Category_Edit_Tab_Topsearches
extends Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Categories
implements Mage_Adminhtml_Block_Widget_Tab_Interface
{
/**
* Specify template to use
*/
public function __construct()
{
parent::__construct();
$this->setTemplate('catalog/category/edit/categorypicker.phtml');
}
/**
* Checks when this block is readonly
*
* @return bool
*/
public function isReadonly()
{
return false;
}
/**
* getCategoryIds method
* @return array
*/
protected function getCategoryIds()
{
$category = Mage::registry('current_category');
$ids = explode(',', $category->getCategoryTopSearches());
return $ids;
}
/**
* getIdsString
*
* @return string
*/
public function getIdsString()
{
$category = Mage::registry('current_category');
return $category->getCategoryTopSearches();
}
/**
* Return Tab label
*
* @return string
*/
public function getTabLabel()
{
return $this->__('Top Searches');
}
/**
* Return Tab title
*
* @return string
*/
public function getTabTitle()
{
return $this->__('Top Searches');
}
/**
* Can show tab in tabs
*
* @return boolean
*/
public function canShowTab()
{
return true;
}
/**
* Tab is hidden
*
* @return boolean
*/
public function isHidden()
{
return false;
}
}
3-定义树模板
我将产品编辑页面app/design/adminhtml/default/default/template/catalog/product/edit/categories.phtml的类别树的核心模板复制粘贴到app/design/adminhtml/default/mytheme/template/catalog/category/edit/categorypicker.phtml中,并做了一些细微的更改:
添加了一个文本字段以显示我的ID:
<div class="entry-edit">
<div class="entry-edit-head">
<h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('catalog')->__('Linked Categories') ?></h4>
</div>
<fieldset id="linked_cat">
<input type="text" name="linked_categories_list" id="linked_categories_list" value="<?php echo $this->getIdsString() ?>">
</fieldset>
</div>
我添加了一个函数来实现数组的独特特性
Array.prototype.unique = function(){
'use strict';
var im = {}, uniq = [];
for (var i=0;i<this.length;i++){
var type = (this[i]).constructor.name,
// ^note: for IE use this[i].constructor!
val = type + (!/num|str|regex|bool/i.test(type)
? JSON.stringify(this[i])
: this[i]);
if (!(val in im)){uniq.push(this[i]);}
im[val] = 1;
}
return uniq;
}
4-对category save事件实现一个观察者,将新值注入到real属性中
A.声明
<adminhtml_catalog_category_tabs>
<observers>
<add_topsearches_tab>
<class>mymodule/observer</class>
<method>addTopSearchesTab</method>
</add_topsearches_tab>
</observers>
</adminhtml_catalog_category_tabs>
<catalog_category_save_before>
<observers>
<set_top_searches>
<class>mymodule/observer</class>
<method>setTopSearches</method>
</set_top_searches>
</observers>
</catalog_category_save_before>
5-最后,我通过在安装脚本中更新来隐藏我的原始属性:
(我更改了其组,否则其专用选项卡将为空,但仍会显示)
不知道是否有人会使用它,因为我在这个帖子上很孤独,但我很想在尝试实现这个功能时找到这样的帖子…:D
如果有人感兴趣,我也在配置字段中实现了这个类别选择器,作为一个新的前端类型,它非常棒 我尝试将自定义输入\ U渲染器设置为我的属性。在这个渲染器中,我使用getAfterElementHtml()方法注入HTML以显示选择器触发器按钮链接,但这几乎就是我所做的:'(向上!有人设法使用Mage\u Adminhtml\u Block\u Catalog\u Category\u checkbox\u Tree Block吗?
function categoryAdd(id) {
var ids = $('linked_categories').value.split(',');
ids.push(id);
var idList = ids.unique().join(',').replace(/^,/, '');
document.getElementById("linked_categories_list").value = idList;
$('linked_categories').value = ids.join(',');
}
function categoryRemove(id) {
var ids = $('linked_categories').value.split(',');
// bug #7654 fixed
while (-1 != ids.indexOf(id)) {
ids.splice(ids.indexOf(id), 1);
}
var idList = ids.unique().join(',').replace(/^,/, '');
document.getElementById("linked_categories_list").value = idList;
$('linked_categories').value = ids.join(',');
}
<catalog_category_save_before>
<observers>
<set_top_searches>
<class>mymodule/observer</class>
<method>setTopSearches</method>
</set_top_searches>
</observers>
</catalog_category_save_before>
/**
* takes the fake attribute value and insert it into the real category_top_searches attribute
*
* @param Varien_Event_Observer $observer Observer
*
* @return void
*/
public function setTopSearches(Varien_Event_Observer $observer)
{
/** @var $category Mage_Catalog_Model_Category */
$category = $observer->getEvent()->getCategory();
$params = Mage::app()->getRequest()->getParams();
$ids = $params['linked_categories_list'];
$category->setData('category_top_searches', $ids);
}
$installer->updateAttribute("catalog_category", "category_top_searches", 'group', "General");
$installer->updateAttribute("catalog_category", "category_top_searches", 'is_visible', false);