gilbertosj

gilbertosj

Liveview changing elements of the session

I am currently trying to use liveview, but I have a problem when the database is updated.

The liveview page always changes the element to “Buttom 0” which has class = active.

The question is:
As I do when the liveview page is updated, the “buttom 3” element that is selected must not change.

This is the code I’m using

html

                    <ul class="nav nav-tabs nav-pills ml-auto p-2 nav-mytabs" id="myTab" role="tablist">
                      <li class="nav-item">
                      <a class="nav-link active" phx-update="ignore" id="tab_0-tab" role="tab" aria-controls="tab_0" href="#tab_0" data-toggle="tab" aria-selected="true" phx-update="ignore">Buttom 0</a></li>
                      <li class="nav-item">
                      <a class="nav-link" phx-update="ignore" id="tab_1-tab" role="tab" aria-controls="tab_1" href="#tab_1" data-toggle="tab" aria-selected="false" phx-update="ignore">Buttom 1</a></li>
                      <li class="nav-item">
                      <a class="nav-link" phx-update="ignore" id="tab_2-tab" role="tab" aria-controls="tab_2" href="#tab_2" data-toggle="tab" aria-selected="false" phx-update="ignore">Buttom 2</a></li>
                      <li class="nav-item">
                      <a class="nav-link" phx-update="ignore" id="tab_3-tab" role="tab" aria-controls="tab_3" href="#tab_3" data-toggle="tab" aria-selected="false" phx-update="ignore">Buttom 3</a></li>
                    </ul>
                  <div class="card-body">
                    <div class="tab-content">

                      <div class="tab-pane active" id="tab_0" role="tabpanel" aria-labelledby="tab_0-tab" phx-hook="tab_0" phx-update="ignore">

                        <!-- TAB 0-->
                        <div class="row">
                          <div class="col-sm-12">
                         ...
                       </div>


                      <div class="tab-pane" id="tab_1" role="tabpanel" aria-labelledby="tab_1-tab" phx-hook="tab_1" phx-update="ignore">

                        <!-- TAB 1-->
                        <div class="row">
                          <div class="col-sm-12">
                         ...
                       </div>


<script>
$(document).ready(() => {
  let url = location.href.replace(/\/$/, "");
 
  if (location.hash) {
    const hash = url.split("#");
    $('#myTab a[href="#'+hash[1]+'"]').tab("show");
    url = location.href.replace(/\/#/, "#");
    history.replaceState(null, null, url);
    setTimeout(() => {
      $(window).scrollTop(0);
    }, 400);
  } 
   
  $('a[data-toggle="tab"]').on("click", function() {
    let newUrl;
    const hash = $(this).attr("href");
    if(hash == "#tab_0") {
      newUrl = url.split("#")[0];
    } else {
      newUrl = url.split("#")[0] + hash;
    }
    newUrl += "";
    history.replaceState(null, null, newUrl);
  });
});
</script>

live


def mount(params, _session, socket) do
    if connected?(socket), do: Process.send_after(self(), :update, 5000)
    # if connected?(socket), do: Process.send_after(socket, :update, 10000)

app.js

import "../css/app.scss"

import "phoenix_html"

import {Socket} from "phoenix"

import NProgress from "nprogress"

import {LiveSocket} from "phoenix_live_view"

let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")

let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}})

window.addEventListener("phx:page-loading-start", info => NProgress.start())

window.addEventListener("phx:page-loading-stop", info => NProgress.done())

liveSocket.connect()


window.liveSocket = liveSocket

First Post!

Ljzn

Ljzn

The “clicked” state is stored in your js runtime, in other word, the browser. When database updated, the phoenix server renders a new html page(including the js code in that page), the old state in browser will lost.

If you want to keep the “clicked” state after database updated, you need let server keeps the state, use the live template. (with phx-on-click and save the state in socket assisngs)

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement