How to access a javascript object in another HTML page?

Asked

Viewed 187 times

0

I’m designing a dynamic table using javascript and localstorage.

The project has two HTML documents one showing the table with a list of registered students, and 3 options: Enroll a New Student, Edit Student, Remove Student. And the other with a small form with the registration fields, name and year of entry.

inserir a descrição da imagem aqui In this case, when I press the sign-up button I open the form page, fill in the data and when I click the confirm button it stores the data in the localstorage. I’m doing like this:

//Variáveis Globais
        var campo_matricula = document.querySelector("input.matricula");
        var campo_nome = document.querySelector("input.nome");
        var campo_ano = document.querySelector("input.ano");
        var botao_confirmar = document.querySelector("input.confirmarBtn");
        var botao_cancelar = document.querySelector("input.cancelarBtn");

        //Objetos
        function Aluno(matricula, nome, ano) {
            this.matricula = matricula;
            this.nome = nome;
            this.ano_de_ingresso = ano;
        }

        //Funções
        function adicionar_dados_localstorage(event){
            event.preventDefault();
            //Criando uma nova instância do objeto Aluno
            novo_aluno = new Aluno(campo_matricula.value, campo_nome.value, campo_ano.value);
            //Adicionando no localStorage
            localStorage.setItem("matricula", novo_aluno.matricula);
            localStorage.setItem("nome", novo_aluno.nome);
            localStorage.setItem("ano", novo_aluno.ano_de_ingresso);
        }

        function abrir_documento_tabela(event) {
            window.location.assign("index.html");
        }

        //Rotina Principal
        botao_confirmar.addEventListener("click", adicionar_dados_localstorage);
        botao_confirmar.addEventListener("click", abrir_documento_tabela);

However, when I go back to the table page I cannot insert this new student registered in the table and I have no idea how to show all students already registered every time the table page is reloaded.

The script of the table’s HTML page looks like this:

//Variáveis globais
            var corpo_tabela = document.querySelector("tbody");

            //Objetos

            //Funções
                function criar_linha_tabela(){
                    //criar linha da tabela de Alunos
                    var linha = document.createElement("tr");
                    var campo_matricula = document.createElement("td");
                    var campo_nome = document.createElement("td");
                    var campo_ano = document.createElement("td");

                    //Criar o conteúdo da linha criada
                    var text_matricula = localStorage.getItem("matriucla");
                    var text_nome = localStorage.getItem("nome");
                    var text_ano = localStorage.getItem("ano");;

                    //Vincular o conteúdo aos elementos da linha criada
                    campo_matricula.appendChild(text_matricula);
                    campo_nome.appendChild(text_nome);
                    campo_ano.appendChild(text_ano);

                    linha.appendChild(campo_matricula);
                    linha.appendChild(campo_nome);
                    linha.appendChild(campo_ano);

                    //Vincular os elementos da linha criada ao documento
                    corpo_tabela.appendChild(linha);
                }

            //Rotina Principal
            window.onload = criar_linha_tabela();

It’s been a while since I’ve been trying to figure it out, somebody please help kkkjkj

  • The best way to persist the data, in this case, is to save as an object.

  • How do I save the data as an object in localstorage? I’m a beginner kkk

  • Yes, it does. Do like this: localStorage.setItem("students", JSON.stringify(arrayDelunos));

  • the variables text_matricula, text_name and text_year are receiving the correct values? the problem is not in the passage of values between one screen and another but in the presentation, right?

  • don’t know, I check the localstorage and the data so there, but n appears nd in the table

  • I took a look now, and gave this error on the table page: Uncaught Typeerror: Failed to execute 'appendchild' on 'Node': Parameter 1 is not of type 'Node'. at create_table_string (index.html:57) at index.html:70

Show 1 more comment

1 answer

0


Try to use

campo_matricula.innerHTML = text_matricula
campo_nome.innerHTML = text_nome
campo_ano.innerHTML = text_ano

instead of :

campo_matricula.appendChild(text_matricula);
campo_nome.appendChild(text_nome);
campo_ano.appendChild(text_ano);
  • blz, I’ll try here my consecrated

  • It worked, I can get the new enrolled student hehe vlwzão

  • Do you have any idea how I can store all registered students so that when the page shows all registered students instead of just the last registered student? I will need to have a certain control over them so you can apply on the student edit option

  • mark my answer as please accept my consecrated

  • you can save everything to a JSON and save to localStorage the same way, but to save a JSON to localStoage you need to put a localStorage.setItem("key", JSON.stringify(json)); and to get it you have to give a const result = JSON.parse(localStorage.getItem("key"));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.