I have created grid using ui component. I want to add column serial number.

I added primary key i.e item_id of table. But if record gets deleted then serial number does not work.

=> Ex : I deleted record 2 then serial number column will display no. 1,3,4,5...

Is there any element which define autoincrement in xml.

     <column name="item_id">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sorting" xsi:type="string">asc</item>
                <item name="label" xsi:type="string" translate="true">Sr No</item>

I tried sql query i.e

 set @row_number=0;
 select (@row_number := @row_number +1) as num,name from feedback

How to implement in magento 2?

  echo "set @row_number=0; ";
    ['secondTable' => $this->getTable('sales_order')],'main_table.feedback_id = secondTable.entity_id',['main_table.name','(@row_number := @row_number +1) as num']);

Replace this code :

<column name="item_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>

This code will be helpful to you.

  • I want to add serial number 1,2,3,4..... . How can be added? Is there any element like autoincrement defined in ui component? – jassi Jan 13 '18 at 4:23
  • It's auto increment/primary key field. There is no need to add serial number. – Rohan Hapani Jan 13 '18 at 4:25
  • If I delete record then serial number will display 1,3,4...format – jassi Jan 13 '18 at 4:27
  • It's default fuctionality. we can't change it. because it's auto increment field. – Rohan Hapani Jan 13 '18 at 4:29
  • we can delete record from grid or we can add specific condition also for getting specific record. Then at that time how we can use primary key for Serial No.? – jassi Jan 13 '18 at 4:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.