我想在 magento 产品管理后端的一些描述和元字段上添加简单的字符计数器功能。就像下面的屏幕截图一样。
我通过将简单的原型脚本添加到在 magento 管理 HTML 上加载的 JS 文件之一来做到这一点。我选择 browser.js (/js/mage/adminhtml/browser.js
)因为这是在我的 magento 安装的后端区域加载的最后一个脚本。这是我的原型脚本代码块:
/* ADMIN CHAR COUNTER SCRIPT */
Event.observe(window, 'load', function() {
Element.insert( $('meta_title').up().next().down('span'), {
'after': "<div id='meta_title_counter'>Char count: <span id='meta_title_counter_num'>"+(69-$('meta_title').getValue().length)+"</span></div>"
});
Element.insert( $('meta_description').up().next().down('span'), {
'after': "<div id='meta_description_counter'>Char count: <span id='meta_description_counter_num'>"+(156-$('meta_description').getValue().length)+"</span></div>"
});
Element.insert( $('short_description').up().next().down('span'), {
'after': "<div id='short_description_counter'>Char count: <span id='short_description_counter_num'>"+$('short_description').getValue().length+"</span></div>"
});
Element.insert( $('description').up().next().down('span'), {
'after': "<div id='description_counter'>Char count: <span id='description_counter_num'>"+$('description').getValue().length+"</span></div>"
});
Event.observe('meta_title', 'keyup', function(event) {
$counter = 69-$('meta_title').getValue().length;
$("meta_title_counter_num").update($counter);
if($counter < 0){ $("meta_title_counter").setStyle({ color: 'red' }); }
else{ $("meta_title_counter").setStyle({ color: '#6F8992' }); }
});
Event.observe('meta_description', 'keyup', function(event) {
$counter = 156-this.getValue().length;
$("meta_description_counter_num").update($counter);
if($counter < 0){ $("meta_description_counter").setStyle({ color: 'red' }); }
else{ $("meta_description_counter").setStyle({ color: '#6F8992' }); }
});
Event.observe('short_description', 'keyup', function(event) { $("short_description_counter_num").update(this.getValue().length); });
Event.observe('description', 'keyup', function(event) { $("description_counter_num").update(this.getValue().length); });
});
/* END OF CHAR COUNTER MODULE */
我确实意识到我所做的是一个如此快速而肮脏的伎俩。我实际上编辑了核心文件。这意味着该脚本将在 magento 升级后删除。我的老板要求我将此功能放入模块中。但我没有任何创建 magento 模块的经验。我试图找到一些关于如何创建简单的 magento 模块的基本教程。但这些教程都没有给我提供注入新脚本的方法。这可能是最接近的指南:
http://www.magentocommerce.com/wiki/5_-_modules_and_development/admin/how_to_customize_backend_template_f.e._sales_order_information http://www.magentocommerce.com/wiki/5_-_modules_and_development/admin/how_to_customize_backend_template_f.e._sales_order_information
但我仍然不知道从哪里开始这个简单的模块创建。如果这个问题感觉太新手,我很抱歉,但我真的需要帮助,与往常不同,这次谷歌无法帮助我(或者至少我找不到一个好的关键字来开始谷歌搜索)。所以在这里我希望有人会很乐意帮助我:)