8.如何添加后臺(tái)數(shù)據(jù)網(wǎng)格

Magento 2 模塊開(kāi)發(fā)基礎(chǔ)部分 - 目錄

本章節(jié)討論Magento 2后臺(tái)數(shù)據(jù)網(wǎng)格。如你所知Magento 2 網(wǎng)格是一種數(shù)據(jù)表格,用來(lái)顯示數(shù)據(jù)庫(kù)中對(duì)應(yīng)表的數(shù)據(jù),并提供了排序、篩選、刪除、更新數(shù)據(jù)的功能。示例網(wǎng)格可以查看后臺(tái)產(chǎn)品列表和用戶列表。

Magento 2 提供了兩種創(chuàng)建網(wǎng)格的方式:使用布局文件和使用組件(component)。接下來(lái)會(huì)討論他們的實(shí)現(xiàn)細(xì)節(jié)。在此之前請(qǐng)確保已經(jīng)創(chuàng)建了前幾章節(jié)的簡(jiǎn)單模塊,并添加了后臺(tái)菜單和路由文件,這些在本節(jié)都會(huì)用到。

創(chuàng)建管理網(wǎng)格步驟如下:

  • 1 創(chuàng)建數(shù)據(jù)表
  • 2 創(chuàng)建后臺(tái)路由
  • 3 添加后臺(tái)導(dǎo)航菜單項(xiàng)
  • 4 添加控制器
  • 5 使用組件創(chuàng)建網(wǎng)格
  • 6 使用布局文件創(chuàng)建網(wǎng)格

第一步 創(chuàng)建數(shù)據(jù)表

編輯安裝配置文件 app/code/Aqrun/HelloWorld/Setup/InstallSchema.php

這個(gè)文件只會(huì)運(yùn)行一次在模塊安裝時(shí)。創(chuàng)建數(shù)據(jù)表的代碼:

<?php
namespace Aqrun\HelloWorld\Setup;

use Magento\Framework\DB\Ddl\Table as T;

class InstallSchema implements \Magento\Framework\Setup\InstallSchemaInsterface
{

    private $_postTable = 'aqrun_helloworld_post';

    public function install(
        \Magento\Framework\Setup\SchemaSetupInterface $setup,
        \Magento\Framework\Setup\ModuleContextInterface $context
    ){
        $installer = $setup;
        $installer->startSetup();
        if(!$installer->tableExists($this->_postTable)){
            $this->createTable($installer);
        }
        $installer->endSetup();
    }

    protected function createPostTable($installer)
    {
        $table = $installer->getConnection()->newTable(
            $installer->getTable($this->_postTable)
        )->addColumn(
            'post_id', T::TYPE_INTEGER, null,
            ['identity'=>true, 'nullable' => false, 'primary' => true, 'unsigned' => true],
            'Post ID'
        )->addColumn('name', T::TYPE_TEXT, 255, ['nullable' => false], 'Post Name')
        ->addColumn('url_key', T::TYPE_TEXT, 255, [], 'Post URL Key')
        ->addColumn('post_content', T::TYPE_TEXT, '64k', [], 'Post Post Content')
        ->addColumn('tags', T::TYPE_TEXT, 255, [], 'Post Tags')
        ->addColumn('status', T::TYPE_INTEGER, 1, [], 'Post status')
        ->addColumn('featured_image', T::TYPE_TEXT, 255, [], 'Post Featured Image')
        ->addColumn('created_at', T::TYPE_TIMESTAMP, null,
            ['nullable' => false, 'default' => T::TIMESTAMP_INI],
            'Created At'
        )->addColumn('updated_at', T::TYPE_TIMESTAMP, null,
            ['nullable' => false, 'default' => T::TIMESTAMP_INIT_UPDATE],
            'Updated At'
        );
        $installer->getConnection->createTable($table);

        $installer->getConnection->addIndex(
            $installer->getTable($this->_postTable),
            $installer->getIdxName(
                $installer->getTable($this->_postTable),
                ['name', 'url_key', 'post_content', 'tags', 'featured_image'],
                \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT
            ),
            ['name', 'url_key', 'post_content', 'tags', 'featured_image'],
            \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT
        );
    }
}

第二步 添加后臺(tái)路由

文件: app/code/Aqrun/HelloWorld/etc/adminhtml/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="aqrun_helloworld" frontName="aqrun_helloworld">
            <module name="Aqrun_HelloWorld">
        </route>
    </router>
</config>

第三步 添加導(dǎo)航菜單

使用的路由名稱是 aqrun_helloworld,那后臺(tái)頁(yè)面鏈接就會(huì)是 aqrun_helloworld/post/index

如何添加查看后臺(tái)菜單部分內(nèi)容

第四步 添加控制器

文件: app/code/Aqrun/HelloWorld/Controller/Adminhtml/Post/Index.php

<?php
namespace Aqrun\HelloWorld\Controller\Adminhtml\Post;

class Index extends \Magento\Backend\App\Action
{
    protected $resultPageFactory = false;
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ){
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->prepend(__('Posts'));
        return $resultPage;
    }
}

第五步 方式一:使用組件創(chuàng)建后臺(tái)網(wǎng)格

5.1 定義數(shù)據(jù)資源

創(chuàng)建依賴注入文件 di.xml 并定義數(shù)據(jù)資源,為我們的網(wǎng)格提供的數(shù)據(jù)關(guān)聯(lián)模型。

文件: app/code/Aqrun/HelloWorld/etc/di.xml

代碼:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="aqrun_helloworld_post_listing_data_source">
                    Aqrun\HelloWorld\Model\ResourceModel\Post\Grid\Collection
                </item>
            </argument>
        </arguments>
    </type>
    <virtualType name="Aqrun\HelloWorld\Model\ResourceModel\Post\Grid\Collection"
        type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
        <arguments>
            <argument name="mainTable" xsi:type="string">aqrun_helloworld_post</argument>
            <argument name="resourceModel" xsi:type="string">Aqrun\HelloWorld\Model\ResourceModel\Post</argument>
        </arguments>
    </virutalType>
</config>

這個(gè)文件為數(shù)據(jù)表定義了定義了 Post 集合類參數(shù)是 表名稱和資源模型(ResouceModel)。節(jié)點(diǎn)說(shuō)明:

  • type節(jié)點(diǎn)是編輯系統(tǒng)的CollectionFactory類根據(jù)參數(shù)會(huì)給他的變量collections增加一個(gè)資源鍵=>值 ['aqrun_helloworld_post_listing_data_source' => 'Aqrun\HelloWorld\Model\ResourceModel\Post\Grid\Collection']。
  • virtualType 節(jié)點(diǎn)是定義一個(gè)類 Aqrun\HelloWorld\Model\ResourceModel\Post\Grid\Collection 類繼承自 Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult。

在下面的布局文件中網(wǎng)格會(huì)調(diào)用這個(gè)數(shù)據(jù)資源鍵來(lái)獲取數(shù)據(jù)。

5.2 創(chuàng)建布局文件

控制器動(dòng)作是 aqrun_helloworld/post/index, 對(duì)應(yīng)的布局文件就是 aqrun_helloworld_post_index.xml

文件: app/code/Aqrun/HelloWorld/view/adminhtml/layout/aqrun_helloworld_post_index.xml

代碼:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <update handle="styles"/>
    <body>
        <referenceContainer name="content">
            <uiComponent name="aqrun_helloworld_post_listing"/>
        </referenceContainer>
    </body>
</page>

當(dāng)前頁(yè)面內(nèi)容通過(guò)布局文件中定義的 uiComponet 節(jié)點(diǎn)獲取對(duì)應(yīng)組件

5.3 創(chuàng)建組件布局文件

根據(jù)上一步布局文件內(nèi)容,現(xiàn)在我們創(chuàng)建組件布局文件 aqrun_helloworld_post_listing.xml

文件: app/code/Aqrun/HelloWorld/view/adminhtml/ui_component/aqrun_helloworld_post_index.xml

代碼:

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">aqrun_helloworld_post_listing.aqrun_helloworld_post_listing_data_source</item>
            <item name="deps" xsi:type="string">aqrun_helloworld_post_listing.aqrun_helloworld_post_listing_data_source</item>
        </item>
        <item name="spinner" xsi:type="string">spinner_columns</item>
        <item name="buttons" xsi:type="array">
            <item name="add" xsi:type="array">
                <item name="name" xsi:type="string">add</item>
                <item name="label" xsi:type="string" translate="true">Add New Post</item>
                <item name="class" xsi:type="string">primary</item>
                <item name="url" xsi:type="string">*/*/new</item>
            </item>
        </item>
    </argument>
    <dataSource name="aqrun_helloworld_post_listing_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider</argument>
            <argument name="name" xsi:type="string">aqrun_helloworld_post_listing_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">post_id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
                    <item name="update_url" xsi:type="url" path="mui/index/render" />
                    <item name="storageConfig" xsi:type="array">
                        <item name="indexField" xsi:type="string">post_id</item>
                    </item>
                </item>
            </argument>
        </argument>
    </dataSource>
    <columns name="spinner_columns">
        <selectionsColumn name="ids">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="resizeDefaultWidth" xsi:type="string">55</item>
                    <item name="resizeEnabled" xsi:type="boolean">false</item>
                    <item name="indexField" xsi:type="string">post_id</item>
                </item>
            </argument>
        </selectionsColumn>
        <column name="post_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">textRange</item>
                    <item name="sorting" xsi:type="string">asc</item>
                    <item name="label" xsi:type="string" translate="true">ID</item>
                </item>
            </argument>
        </column>
        <column name="name">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string"></item>
                    <item name="editor" xsi:type="array">
                        <item name="editorType" xsi:type="string">text</item>
                        <item name="validation" xsi:type="array">
                            <item name="required-entry" xsi:type="boolean">true</item>
                        </item>
                    </item>
                    <item name="label" xsi:type="string" translate="true"></item>
                </item>
            </argument>
        </column>
        <column name="created_at">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">dataRange</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/date</item>
                    <item name="dataType" xsi:type="string">date</item>
                    <item name="label" xsi:type="string" translate="true">Created</item>
                </item>
            </argument>
        </column>
        </column name="updated_at">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">dateRange</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/date</item>
                    <item name="dataType" xsi:type="string">date</item>
                    <item name="label" xsi:type="string" translate="true">Modified</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

根據(jù)以上代碼可以了解如何定義網(wǎng)格布局,如何調(diào)用數(shù)據(jù)?,F(xiàn)在清除緩存如下顯示:

8-post-list-1.png

5.4 添加列表工具條

如本文開(kāi)始所提到的,網(wǎng)格系統(tǒng)也支持一些交互操作如:排序、篩選、刪除更新等。排序是網(wǎng)格的默認(rèn)功能,可以點(diǎn)擊表頭進(jìn)行排序顯示。接下來(lái)看看其它功能如何實(shí)現(xiàn):

添加控件需要在組件布局文件的父節(jié)點(diǎn)listing下面進(jìn)行編輯:

文件: app/code/Aqrun/HelloWorld/view/adminhtml/ui_component/aqrun_helloworld_post_listing.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <!-- ... other block of code -->
    <listingToolbar name="listing_top">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="stick" xsi:type="boolean">true</item>
            </item>
        </argument>
    </listingToolbar>
    <!--... other block of code-->
</listing>

5.5 添加書簽控件

這個(gè)參數(shù)會(huì)加載模板 Magento/Ui/view/base/web/templates/grid/toolbar.html,用來(lái)處理網(wǎng)格所有異步更新操作??丶翱诳梢苑旁诹性厍盎蚝螅?/p>

文件: app/code/Aqrun/HelloWorld/view/adminhtml/ui_component/aqrun_helloworld_post_listing.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <!-- ... other block of code -->
    <listingToolbar name="listing_top">
        <!-- ... other block of code -->    
        <bookmark name="bookmarks"/>
    </listingToolbar>
    <!-- ... other block of code -->
</listing>

書簽控件允許管理員設(shè)置網(wǎng)格不同狀態(tài)。每種狀態(tài)可有不同的字段列表。因此每個(gè)管理員用戶可以選擇它特定的信息顯示。

5.6 列控件

這個(gè)控件會(huì)添加一個(gè)字段列表可以控制網(wǎng)格顯示指定字段,改動(dòng)之后可以選擇把這個(gè)狀態(tài)操作為一個(gè)書簽,方便下次快速顯示。

文件: app/code/Aqrun/HelloWorld/view/adminhtml/ui_component/aqrun_helloworld_post_listing.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <!-- ... other block of code -->
    <listingToolbar name="listing_top">
        <!-- ... other block of code -->    
        <columnsControls name="columns_controls"/>
    </listingToolbar>
    <!-- ... other block of code -->
</listing>

5.7 全文搜索

這個(gè)節(jié)點(diǎn)會(huì)在網(wǎng)格上面添加一個(gè)搜索框,可以搜索數(shù)據(jù)表的所有數(shù)據(jù)

文件: app/code/Aqrun/HelloWorld/view/adminhtml/ui_component/aqrun_helloworld_post_listing.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <!-- ... other block of code -->
    <listingToolbar name="listing_top">
        <!-- ... other block of code -->    
        <filterSearch name="fulltext"/>
    </listingToolbar>
    <!-- ... other block of code -->
</listing>

5.8 篩選

這個(gè)節(jié)點(diǎn)為每個(gè)列定義一個(gè)篩選框

文件: 'app/code/Aqrun/HelloWorld/view/adminhtml/ui_component/aqrun_helloworld_post_listing.xml'

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <!-- ... other block of code -->
    <listingToolbar name="listing_top">
        <!-- ... other block of code -->    
        <filters name="listing_filters" />
    </listingToolbar>
    <!-- ... other block of code -->
</listing>

5.9 批量操作控件

這個(gè)節(jié)點(diǎn)會(huì)添加一個(gè)對(duì)列表進(jìn)行批量操作的下拉列表。管理員可以使用這個(gè)控件一次性操作多條數(shù)據(jù)

文件: app/code/Aqrun/HelloWorld/view/adminhtml/ui_component/aqrun_helloworld_post_listing.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <!-- ... other block of code -->
    <listingToolbar name="listing_top">
    <!-- ... other block of code -->    
        <massaction name="listing_massaction">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item>
                </item>
            </argument>
            <action name="delete">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="type" xsi:type="string">delete</item>
                        <item name="label" xsi:type="string" translate="true">Delete</item>
                        <item name="url" xsi:type="url" path="mageplaza_helloworld/post/massDelete"/>
                        <item name="confirm" xsi:type="array">
                            <item name="title" xsi:type="string" translate="true">Delete Post</item>
                            <item name="message" xsi:type="string" translate="true">Are you sure you wan't to delete selected items?</item>
                        </item>
                    </item>
                </argument>
            </action>
        </massaction>
    </listingToolbar>
    <!-- ... other block of code -->
</listing>

5.10 分頁(yè)

這個(gè)節(jié)點(diǎn)可以為網(wǎng)格添加分頁(yè)按鈕,如果有大量數(shù)據(jù)就會(huì)很實(shí)用

文件: app/code/Aqrun/HelloWorld/view/adminhtml/ui_component/aqrun_helloworld_post_listing.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <!-- ... other block of code -->
    <listingToolbar name="listing_top">
    <!-- ... other block of code -->    
    <paging name="listing_paging"/>
    </listingToolbar>
    <!-- ... other block of code -->
</listing>

5.11 導(dǎo)出

這個(gè)節(jié)點(diǎn)可以添加導(dǎo)出功能,你可以導(dǎo)出當(dāng)前網(wǎng)格的數(shù)據(jù)

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <!-- ... other block of code -->
    <listingToolbar name="listing_top">
    <!-- ... other block of code -->    
    <exportButton name="export_button"/>
    </listingToolbar>
    <!-- ... other block of code -->
</listing>

清空緩存如下顯示

8-post-list-2.png

第六步 第二種方式 使用布局文件創(chuàng)建網(wǎng)格

注意! 如果第五步已經(jīng)完成了就路過(guò)這一步,當(dāng)然也可以新增一個(gè)路由在新的頁(yè)面使用這種方式實(shí)現(xiàn)

上面已經(jīng)使用了組件的方式添加網(wǎng)格,現(xiàn)在這個(gè)來(lái)看看如何使用普通的 布局/區(qū)塊 文件實(shí)現(xiàn)

6.1 為網(wǎng)格創(chuàng)建區(qū)塊

文件: app/code/Aqrun/HelloWorld/Block/Adminhtml/Post.php

namespace Aqrun\HelloWorld\Block\Adminhtml;

class Post extends \Magento\Backend\Blcok\Widget\Grid\Container
{
    protected function _construct()
    {
        $this->_controller = 'adminhtml_post';
        $this->_blockGroup = 'Aqrun_HelloWorld';
        $this->_headerText = __('Posts');
        $this->_addButtonLabel = __('Create New Post');
        parent::_construct();
    }
}

網(wǎng)格區(qū)塊繼承了 \Magento\Backend\Blcok\Widget\Grid\Container 類,然后在 _construct() 方法中定義了一些變量

  • _blockGroup 是我們模塊的名稱格式為: VendorName_ModuleName
  • _controller 是區(qū)塊文件夾中網(wǎng)格區(qū)塊的路徑,這里放的是 Adminhtml/Post 文件夾在的 Grid.php 文件
  • _headerText 是網(wǎng)格頁(yè)面標(biāo)題
  • _addButtonLabel 是創(chuàng)建按鈕的顯示文字

6.2 添加布局文件

現(xiàn)在添加布局文件并調(diào)用網(wǎng)格區(qū)塊顯示網(wǎng)格:

文件: app/code/Aqrun/HelloWorld/view/adminhtml/layout/aqrun_helloworld_post_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="styles"/>
    <body>
        <referenceContainer name="content">
            <block class="Aqrun\HelloWorld\Block\Adminhtml\Post" name="aqrun_post_grid">
                <block class="Magento\Backend\Block\Widget\Grid" name="aqrun_post_grid.grid" as="grid">
                    <arguments>
                        <argument name="id" xsi:type="string">post_id</argument>
                        <argument name="dataSource" xsi:type="object">Aqrun\HelloWorld\Model\ResourceModel\Post\Collection</argument>
                        <argument name="default_sort" xsi:type="string">id</argument>
                        <argument name="default_dir" xsi:type="string">ASC</argument>
                        <argument name="save_parameters_in_session" xsi:type="string">1</argument>
                    </arguments>
                    <block class="Magento\Backend\Block\Widget\Grid\ColumnSet" name="aqrun_post_grid.grid.columnSet" as="grid.columnSet">
                        <arguments>
                            <argument name="rowUrl" xsi:type="array">
                                <item name="path" xsi:type="string">*/*/edit</item>
                            </argument>
                        </arguments>
                        <block class="Magento\Backend\Block\Widget\Grid\Column" as="post_id">
                            <arguments>
                                <argument name="header" xsi:type="string" translate="true">ID</argument>
                                <argument name="index" xsi:type="string">post_id</argument>
                                <argument name="type" xsi:type="string">text</argument>
                                <argument name="column_css_class" xsi:type="string">col-id</argument>
                                <argument name="header_css_class" xsi:type="string">col-id</argument>
                            </arguments>
                        </block>
                        <block class="Magento\Backend\Block\Widget\Grid\Column" as="name">
                            <arguments>
                                <argument name="header" xsi:type="string" translate="true">Name</argument>
                                <argument name="index" xsi:type="string">name</argument>
                                <argument name="type" xsi:type="string">text</argument>
                                <argument name="column_css_class" xsi:type="string">col-id</argument>
                                <argument name="header_css_class" xsi:type="string">col-id</argument>
                            </arguments>
                        </block>
                        <block class="Magento\Backend\Block\Widget\Grid\Column" as="created_at">
                            <arguments>
                                <argument name="header" xsi:type="string" translate="true">Created</argument>
                                <argument name="index" xsi:type="string">created_at</argument>
                                <argument name="type" xsi:type="string">date</argument>
                                <argument name="column_css_class" xsi:type="string">col-id</argument>
                                <argument name="header_css_class" xsi:type="string">col-id</argument>
                            </arguments>
                        </block>
                        <block class="Magento\Backend\Block\Widget\Grid\Column" as="updated_at">
                            <arguments>
                                <argument name="header" xsi:type="string" translate="true">Modified</argument>
                                <argument name="index" xsi:type="string">updated_at</argument>
                                <argument name="type" xsi:type="string">date</argument>
                                <argument name="column_css_class" xsi:type="string">col-id</argument>
                                <argument name="header_css_class" xsi:type="string">col-id</argument>
                            </arguments>
                        </block>
                    </block>
                </block>
            </block>
        </referenceContainer>
    </body>
</page>

布局文件中,定義了一些網(wǎng)格需要的參數(shù),最主要的是 dataSource 節(jié)點(diǎn)。這個(gè)節(jié)點(diǎn)調(diào)用 di.xml 文件中定義的數(shù)據(jù)資源可以獲取數(shù)據(jù)

6.4 添加控件

如何要使用批量操作控件可以添加下面的節(jié)點(diǎn)

<block class="Magento\Backend\Block\Widget\Grid\Massaction" name="mageplaza.helloWorld.massaction" as="grid.massaction">
    <arguments>
        <argument name="massaction_id_field" xsi:type="string">post_id</argument>
        <argument name="form_field_name" xsi:type="string">ids</argument>
        <argument name="use_select_all" xsi:type="string">1</argument>
        <argument name="options" xsi:type="array">
            <item name="disable" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Delete</item>
                <item name="url" xsi:type="string">*/*/massDelete</item>
            </item>
        </argument>
    </arguments>
</block>

清空緩存如下顯示:

8-post-list-3.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容