Magento 2自定义网格列排序顺序


20

我使用UI组件在Magento管理员中创建了一个自定义网格。基本上,除了未正确显示的列顺序之外,其他所有程序都工作正常。 在此处输入图片说明

  • 如您所见,“操作”列位于开头,而选定的列位于结尾
  • 当我在XML中创建列时,我考虑了sortOrder每一列的属性,但是某种程度上,顺序并不是我将其设置为

来自的代码listing.xml如下

<columns name="inactive_columns" class="Module\MyModule\Ui\Component\Listing\Columns">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="storageConfig" xsi:type="array">
                <item name="provider" xsi:type="string">inactive_listing.inactive_listing.listing_top.bookmarks</item>
                <item name="namespace" xsi:type="string">current</item>
            </item>
            <item name="editorConfig" xsi:type="array">
                <item name="selectProvider" xsi:type="string">inactive_listing.inactive_listing.inactive_columns.ids</item>
                <item name="enabled" xsi:type="boolean">true</item>
                <item name="indexField" xsi:type="string">entity_id</item>
                <item name="clientConfig" xsi:type="array">
                    <item name="saveUrl" xsi:type="url" path="customer/index/inlineEdit"/>
                    <item name="validateBeforeSave" xsi:type="boolean">false</item>
                </item>
            </item>
            <item name="childDefaults" xsi:type="array">
                <item name="fieldAction" xsi:type="array">
                    <item name="provider" xsi:type="string">inactive_listing.inactive_listing.inactive_columns_editor</item>
                    <item name="target" xsi:type="string">startEdit</item>
                    <item name="params" xsi:type="array">
                        <item name="0" xsi:type="string">${ $.$data.rowIndex }</item>
                        <item name="1" xsi:type="boolean">true</item>
                    </item>
                </item>
                <item name="storageConfig" xsi:type="array">
                    <item name="provider" xsi:type="string">inactive_listing.inactive_listing.listing_top.bookmarks</item>
                    <item name="root" xsi:type="string">columns.${ $.index }</item>
                    <item name="namespace" xsi:type="string">current.${ $.storageConfig.root }</item>
                </item>
            </item>
        </item>
    </argument>
    <selectionsColumn name="ids">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="indexField" xsi:type="string">entity_id</item>
                <item name="sortOrder" xsi:type="number">0</item>
            </item>
        </argument>
    </selectionsColumn>
    <column name="entity_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 name="sortOrder" xsi:type="number">20</item>
            </item>
        </argument>
    </column>
    <column name="firstname">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">text</item>
                <item name="label" xsi:type="string" translate="true">Firstname</item>
                <item name="editor" xsi:type="string">text</item>
                <item name="sortOrder" xsi:type="number">40</item>
            </item>
        </argument>
    </column>
    <column name="lastname">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">text</item>
                <item name="label" xsi:type="string" translate="true">Lastname</item>
                <item name="editor" xsi:type="string">text</item>
                <item name="sortOrder" xsi:type="number">50</item>
            </item>
        </argument>
    </column>
    <column name="email">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">text</item>
                <item name="editor" xsi:type="string">text</item>
                <item name="label" xsi:type="string" translate="true">Email Cacat</item>
                <item name="sortOrder" xsi:type="number">30</item>
            </item>
        </argument>
    </column>
    <column name="group_id">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">select</item>
                <item name="editor" xsi:type="string">select</item>
                <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
                <item name="dataType" xsi:type="string">select</item>
                <item name="label" xsi:type="string" translate="true">Group</item>
                <item name="sortOrder" xsi:type="number">90</item>
            </item>
        </argument>
    </column>
    <column name="billing_telephone">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">text</item>
                <item name="editor" xsi:type="string">text</item>
                <item name="label" xsi:type="string" translate="true">Phone</item>
                <item name="sortOrder" xsi:type="number">100</item>
            </item>
        </argument>
    </column>
    <column name="created_at" class="Magento\Ui\Component\Listing\Columns\Date">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">dateRange</item>
                <item name="dataType" xsi:type="string">date</item>
                <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/date</item>
                <item name="label" xsi:type="string" translate="true">Customer Since</item>
                <item name="sortOrder" xsi:type="number">110</item>
            </item>
        </argument>
    </column>
    <column name="last_visit_at" class="Magento\Ui\Component\Listing\Columns\Date">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/date</item>
                <item name="filter" xsi:type="string">dateRange</item>
                <item name="visible" xsi:type="boolean">false</item>
                <item name="dataType" xsi:type="string">date</item>
                <item name="label" xsi:type="string" translate="true">Last Logged In</item>
                <item name="sortOrder" xsi:type="number">120</item>
            </item>
        </argument>
    </column>
    <column name="created_in">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="visible" xsi:type="boolean">false</item>
                <item name="label" xsi:type="string" translate="true">Account Created in</item>
                <item name="sortOrder" xsi:type="number">140</item>
            </item>
        </argument>
    </column>
    <column name="kpi_views_customer">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">text</item>
                <item name="label" xsi:type="string" translate="true">Kpi Views</item>
                <item name="sortOrder" xsi:type="number">60</item>
            </item>
        </argument>
    </column>
    <column name="customer_membership">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">text</item>
                <item name="label" xsi:type="string" translate="true">Customer Membership</item>
                <item name="sortOrder" xsi:type="number">70</item>
            </item>
        </argument>
    </column>
    <column name="customer_access">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">text</item>
                <item name="label" xsi:type="string" translate="true">Customer Access</item>
                <item name="sortOrder" xsi:type="number">80</item>
            </item>
        </argument>
    </column>
    <actionsColumn name="actions" class="Magento\Customer\Ui\Component\Listing\Column\Actions">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="indexField" xsi:type="string">entity_id</item>
                <item name="sortOrder" xsi:type="number">222</item>
            </item>
        </argument>
    </actionsColumn>
</columns> 

并且类Module \ MyModule \ Ui \ Component \ Listing \ Columns是

class Columns extends \Magento\Ui\Component\Listing\Columns
{

/**
 * @var \KPIs\CustomerReports\Model\Customer\Attribute\Repository\AttributeRepository $attributeRepository
 */
protected $attributeRepository;

protected $manageAttributes;

/** @var int */
protected $columnSortOrder;

public function __construct(
    ContextInterface $context,
    ColumnFactory $columnFactory,
    AttributeRepository $attributeRepository,
    ManageAttributes $manageAttributes,
    InlineEditUpdater $inlineEditor,
    array $components = [],
    array $data = []
)
{
    parent::__construct($context, $components, $data);
    $this->columnFactory = $columnFactory;
    $this->attributeRepository = $attributeRepository;
    $this->manageAttributes = $manageAttributes;
    $this->inlineEditUpdater = $inlineEditor;
}

public function prepareDataSource(array $dataSource)
{
    $customerAccessOptions = $this->attributeRepository->get(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, "customer_access")->getOptions();
    $customerMembershipOptions = $this->attributeRepository->get(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, "customer_membership")->getOptions();
    if (isset($dataSource['data']['items'])) {
        foreach ($dataSource['data']['items'] as & $item) {
            if (isset($item["customer_access"])) {
                $item["customer_access"][0] = $customerAccessOptions[$item["customer_access"][0]]->getLabel();
            }
            if (isset($item["customer_membership"])) {
                $item["customer_membership"] = $customerMembershipOptions[$item["customer_membership"][0]]->getLabel();
            }
        }
    }
    return $dataSource;
}
}
  • 如您所见,在该类中,我不执行与sortOrder相关的任何操作:),所以它应该是默认行为
  • 另外,动作和选择列不能拖放(据我所知)

有谁知道如何从XML或代码中对列进行排序?该操作应该是最后一列,选择应该是第一列。谢谢!:)


2
尝试在网格中拖放列
Keyur Shah

我已经尝试过了,它无法与操作和选择列一起使用
Dragos

Answers:


53

试试这个解决方案

  1. 在下面的行中对网格中的列进行排序。

    <item name="sortOrder" xsi:type="number">6</item>
  2. 打开数据库表ui_bookmark

  3. 从中搜索uiComponent的名称 ui_bookmark表的namespce列中 。您可以从相应的布局xml检查uiComponent名称。以下仅是示例路径。

    /app/code/Vendor/Module/view/adminhtml/layout/module__controller_index.xml
  4. 现在,从ui_bookmark表中删除找到的记录。

  5. 清除缓存

    php bin/magento cache:flush
    
    php bin/magento cache:clean

就这样。请享用!


是的,这是正确的答案。谢谢!:)
Dragos

7
可以,但是很高兴知道如何在没有直接数据库操作的情况下刷新它。
Volvox

这就像薄荷:)
Praveen Negimani

5

表中存储了Magento2中ui网格的列顺序 ui_bookmark

对于namespace相等的列,您的网格名称和标识符= current您应更改config列的值。您可以在行的每一行末尾找到列顺序,参数“ positions”:{}


真的对我有帮助!这是有效的解决方案。我已经删除了当前视图和默认视图,所以现在按我的要求可以正常工作。
彭德拉·贾达贾

2

您需要首先检查您的“ module.xml”并找到代码。例如:

并检查您的模块顺序是否必须在模块加载顺序之后或之前加载,或者确保模块加载符合您的要求。

您可以检查模块的加载顺序etc-> config.xml文件。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.