DEV Community

Cover image for Adding an inline delete action on the listing page
Cristian Molina
Cristian Molina

Posted on

Adding an inline delete action on the listing page

This is the part III of the Phlex, htmx combo for building UIs.

deleting an article

Pretty similar to the insert action,
it consist of:

  • Edit ArticleComponent to add a delete button code.

  • Add a delete route, if needed (not here).

  • Add a controller method to handle the removal of the article using the id received.

  def template
    article(class: ARTICLE_STYLE, id: "article-#{@article.id}") {
      h3(class: HEADER_STYLE) {
        i { "#{@article.id} - #{@article.name}" }
      }
      p(class: "bg-slate-200 p-4 basis-3/4") { @article.details || "No description" }
        button(class: DELETE_BUTTON_STYLE,
          **hx(delete: article_path(@article), confirm: "Delete Article?", target: "#article-#{@article.id}", swap: "outerHTML swap:.5s")) {
          "X"
        }
      }
    }
  end
Enter fullscreen mode Exit fullscreen mode

The new button inside the Phlex Article component

  def destroy
    Article.delete params[:id]
    render plain: ""
  end
Enter fullscreen mode Exit fullscreen mode

The controller action

article.item-article.htmx-swapping {
    opacity: 0;
    transition: opacity 0.5s ease-out;
}
Enter fullscreen mode Exit fullscreen mode

Some CSS for htmx effect

Note that it should return an empty string or it would not do the removal effect in the UI and JS would be needed.

It felt pretty easy to add this functionality. It start to looks good.

Next will be that inline edit action or some inline filtering of the items in the list.

Happy hacking!

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Cloudinary image

Zoom pan, gen fill, restore, overlay, upscale, crop, resize...

Chain advanced transformations through a set of image and video APIs while optimizing assets by 90%.

Explore

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay