如何在Magento 2后台菜单中显示项目统计数量?
Magento 2后台菜单添加子项记录统计数实现方案
实现效果

具体实现步骤
1. 配置菜单标识(menu.xml)
在你的模块etc/adminhtml/menu.xml中,给需要显示统计数的子菜单项添加自定义count_model属性,指定负责获取对应记录数的模型类:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd"> <menu> <add id="Vendor_Module::parent" title="我的模块" module="Vendor_Module" sortOrder="100" resource="Vendor_Module::parent"/> <!-- 子项1:指定统计模型 --> <add id="Vendor_Module::child1" title="子项1" module="Vendor_Module" sortOrder="10" parent="Vendor_Module::parent" resource="Vendor_Module::child1" count_model="Vendor\Module\Model\Count\Child1"/> <!-- 子项2:指定统计模型 --> <add id="Vendor_Module::child2" title="子项2" module="Vendor_Module" sortOrder="20" parent="Vendor_Module::parent" resource="Vendor_Module::child2" count_model="Vendor\Module\Model\Count\Child2"/> </menu> </config>
2. 创建统计数模型类
针对每个子菜单项,创建对应的统计模型,实现记录数获取逻辑:
比如子项1的模型Vendor/Module/Model/Count/Child1.php:
<?php namespace Vendor\Module\Model\Count; use Magento\Framework\Model\AbstractModel; use Vendor\Module\Model\ResourceModel\YourModel\CollectionFactory; class Child1 extends AbstractModel { protected $collectionFactory; public function __construct( CollectionFactory $collectionFactory, \Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, array $data = [] ) { $this->collectionFactory = $collectionFactory; parent::__construct($context, $registry, $resource, $resourceCollection, $data); } /** * 获取子项1对应的记录总数 * @return int */ public function getCount() { // 替换为你的业务逻辑,比如获取对应模型的集合数量 return $this->collectionFactory->create()->getSize(); } }
同理创建子项2的Child2.php,修改对应的集合工厂和统计逻辑即可。
3. 用插件注入菜单徽章
通过插件拦截菜单渲染逻辑,动态添加统计数徽章:
首先在etc/adminhtml/di.xml配置插件:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Backend\Block\Menu\Item"> <plugin name="vendor_module_add_menu_count_badge" type="Vendor\Module\Plugin\Backend\Block\Menu\Item" sortOrder="10"/> </type> </config>
然后创建插件类Vendor/Module/Plugin/Backend/Block/Menu/Item.php:
<?php namespace Vendor\Module\Plugin\Backend\Block\Menu; use Magento\Backend\Block\Menu\Item as MenuItem; use Magento\Framework\ObjectManagerInterface; class Item { protected $objectManager; public function __construct(ObjectManagerInterface $objectManager) { $this->objectManager = $objectManager; } /** * 在菜单HTML渲染后添加统计徽章 * @param MenuItem $subject * @param string $result * @return string */ public function afterToHtml(MenuItem $subject, $result) { $countModelClass = $subject->getMenu()->getConfig()->getAttribute($subject->getId(), 'count_model'); if ($countModelClass && class_exists($countModelClass)) { $countModel = $this->objectManager->get($countModelClass); $count = $countModel->getCount(); if ($count > 0) { $badgeHtml = '<span class="menu-item-badge">' . $count . '</span>'; $result = str_replace('</a>', $badgeHtml . '</a>', $result); } } return $result; } }
4. 添加CSS样式美化徽章
在view/adminhtml/web/css/source/_module.less中添加徽章样式:
.menu-item-badge { display: inline-block; background-color: #ff4444; color: #ffffff; font-size: 11px; padding: 1px 5px; border-radius: 10px; margin-left: 5px; vertical-align: middle; }
5. 部署与缓存清理
执行以下命令完成部署并清理缓存:
bin/magento setup:static-content:deploy -f bin/magento cache:clean
内容的提问来源于stack exchange,提问作者Mateen Ahmed
相关产品推荐
相关产品推荐

