loics2

loics2

I’m currently updating the lists in my app to live streams and infinite scrolling as describe in liveview bindings documentation, and I want to handle the empty case.

Previously, I could display a message when the items list is empty, but it’s not possible anymore with streams, since they don’t implement count or empty.

Is there a way to check when a stream is empty?

Showing Posts 1 to 10

03juan

03juan

If you know factually that the data is empty on the server, perhaps set an empty_data: true assign that guards the empty message in the template?

loics2

loics2 OP

I thought about it, but I was wondering if there was a way to avoid implementing additional checks, only using the stream.

LostKobrakai

LostKobrakai

There’s not, because without additional constraints there’s no way to know the current state of the stream on the server as it is observed on the client.

chrismccord

chrismccord

Creator of Phoenix

if you have infinite scrolling, then you necessarily have pagination for next/previous page, no? Then you can toggle an empty state assign as other have alluded to when you hit no results without having to check the existing stream contents.

codeanpeace

codeanpeace

Thinking out loud, this could be a good use case for the :first-child or :last-child pseudoselectors. This will let the browser determine how to display the empty list message based on whether there are any other streamed elements rendered.

I got an example working by adding the following line to the .table core component as the first <tr> before the streamed ones. You could also add it to the end and use the :first-child pseudoselector instead.

          <tr class="hidden last:table-row"><td>No Streamed Items</td></tr>

https://github.com/codeanpeace/live_cal/commit/cbdda424d6d352af9c240734c334207016651d4b

loics2

loics2 OP

Yeah, you’re right, that shouldn’t be an issue, using something like the end_of_timeline? assign in the docs.

Oh nice idea! I tried to do something with the :empty pseudoselector, but it’s sensible to whitespace which makes it hard to have something generic.

belgoros

belgoros

I have no pagination at all on my articles index template. I tried to apply your fix, but the message is displayed after deleting a single item and even if there is at least 1 item left.

sodapopcan

sodapopcan

What does the code look like?

You would need something like this:

<ul>
  <li class="first:hidden">No articles</li>
  <li :for={article <- @streams.articles}>
    <%= article.body %>
  </li>
</ul>
belgoros

belgoros

Ah, no, I just modified the core_components.ex, function table/1 by adding a single line as you did in your GitHub repo:

<tr class="hidden last:table-row"><td>No Streamed Items</td></tr>`

In my index template the code to display items looks like that:

<.table
    id="articles"
    rows={@streams.articles}
    row_click={fn {_id, article} -> JS.navigate(~p"/articles/#{article}") end}
  >
    <:col :let={{_id, article}} label="Title"><%= article.title %></:col>
    <:col :let={{_id, article}} label="Content">
      <%= String.slice(article.content, 0, 200) %>
    </:col>
    <:action :let={{_id, article}}>
      <div class="sr-only">
        <.link navigate={~p"/articles/#{article}"}>Show</.link>
      </div>
      <.link patch={~p"/articles/#{article}/edit"}>Edit</.link>
    </:action>
    <:action :let={{id, article}}>
      <.link
        phx-click={JS.push("delete", value: %{id: article.id}) |> hide("##{id}")}
        data-confirm="Are you sure?"
      >
        Delete
      </.link>
    </:action>
  </.table>

I was redirected here from this thread :slight_smile:

sodapopcan

sodapopcan

I’m not @codeanpeace, I’m from that other thread and just butting in since we were talking there :sweat_smile:

The important bit to see is where you put it in the table component. Is it possible you put it outside the tbody or something or maybe have other modifications that would result in there being more than tr left even when there are no articles? I would inspect the DOM after deleting the last one and ensure there is only one tr left.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews