Redeem Now
Read More
Read now
Menu Close
    Start a Project Request quote Reading list Switch to dark mode

    Magento 2 Development 16: Action Column

    In the previous blog we have seen about Mass-actions but sometimes we will need actions for each row. For example, if we want to give permission to admin to edit the blogs then we will have to give an edit link in each row; just like we did on front-end.

    In this blog, we will create action column which will allow us to delete single blog at a time. We will look into the edit blog feature in the next blog.

    Since we are adding a column in the grid, at first we need to edit the ui-component file of the grid view/adminhtml/ui_component/blogmanager_blog_listing.xml

    Here, at the end I have added a new tag actionsColumn to add the action column which will let us delete a single row at a time. We have used a class for this column. With that class we will be displaying the urls for each row.
    With the argument tag we can pass some data to the class. It will get clear when we see the code for the DeleteAction class.
    NOTE that here for the url we are using the route id instead of the front name. It’s because of some strange issue with magento. So please keep both the route id and the front name the same when you are developing any module.

    Now let’s create the class of the action column, Ui/Component/Listing/Columns/DeleteAction.php

    Just like we did earlier with the Content column where we used prepareDataSource to loop through each row and set the data for each row. We have looped over $dataSource['data']['items'] and we are setting url data for each row as [ ‘view’ => [ ‘href’ => ‘#’ , ‘label’ => ‘Link’ ] ]; format.
    Here you can notice that we have used getData function. So what it does is that it allows us to get the values that we passed from the ui component in the argument tag.

    Now let’s create the controller which will handle the delete action Ui/Component/Listing/Columns/DeleteAction.php

    I don’t think I have to explain in this code. Because we have already seen all of these code in one blog or another. Here we have got the id from the param and loaded the blog model based on the id. Then we have deleted that blog. After deleting we have redirected back to the blog listing page. And we have also used the message manager to show the success message or error message in case some exception occurs.

    Now when you view the grid, you should be able to see the delete column and perform delete action using that.

    Next Blog -> Magento 2 Development 17: uiComponent form

    Previous Blog -> Magento 2 Development 15: MassAction

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


  • jordan
    this blog is really helpful.
    • Sanjay Chouhan (Moderator)
      Thank you. I am glad you enjoyed reading it. Stay tuned for more blogs on Magento Development.
  • Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again