I've created a model management pages in admin by UI Component. When saving this model, the controller will save the user id
of creator to the table then show this on the model listing page. It is simple to achieve:
<column name="creator_id">
<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">Creator Id</item>
</item>
</argument>
</column>
But, user id
is difficult to distinguish. So, now I want to transform the creator ID to the user Name, how to achieve that?
Notice the model table has the
user_id
column, but doesn't have theuser_name
. The UI Component need to use theuser_id
to getuser_name
, then put the name in the listing page.
Best Answer
Solution 2(better, can filter): join table in collection
In the
Vendor\Module\Model\ResourceModel\ModelName\Collection
, add the _initSelect() function like belowIf you want to join more tables or know more details about join tables, you could see my another answer join multiple table in collection which build Admin Grid
In the UI Componet, add the
username
columnI think this is a better solution than solution 1. Because you could use the
username
column to filter collection.Solution 1(cannot filter): add class in UI Componet
In the UI Componet, add class for the creator_id:
Create the class
Vendor\Module\Ui\Component\Listing\Column\CreatorName
, add the user name in thepublic function prepareDataSource(array $dataSource)
. Whole codes is shown below: