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
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)
0
Popular in Questions
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
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
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
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
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
For example for a current url like
http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2,
I would like to get:
...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
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
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
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
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
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
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
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
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
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
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
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
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









