asiaaaes
asiaaaes

Reputation: 23

Table , localStorage, js

I have code:

function inicjalizacja(){
    var pracownicy = localStorage.getItem('Pracownicy');
    if (pracownicy === null) {
         localStorage.setItem('Pracownicy',JSON.stringify([]));
    }
}

function Pracownik(imie, drugie_imie, nazwisko, ulica, miejscowosc, kod_pocztowy, telefon, email, wynagrodzenie) {
    this.imie = imie;
    this.drugie_imie=drugie_imie;
    this.nazwisko=nazwisko;
    this.ulica=ulica;
    this.miejscowosc=miejscowosc;
    this.kod_pocztowy=kod_pocztowy;
    this.telefon=telefon;
    this.wynagrodzenie=wynagrodzenie;
}

function Zapisz() {
    inicjalizacja();
    var formularz=document.forms.formularz;
    var nowy_pracownik = new Pracownik(formularz.imie.value,formularz.drugie_imie.value,formularz.nazwisko.value,formularz.ulica.value,
        formularz.miejscowosc.value, formularz.kod_pocztowy.value,formularz.telefon.value,formularz.email.value,formularz.wynagrodzenie.value);

    var pracownicyString = localStorage.getItem('Pracownicy');
    var pracownicy = JSON.parse(pracownicyString);
    pracownicy.push(nowy_pracownik);
    localStorage.setItem('Pracownicy',JSON.stringify(pracownicy));
    confirm("Dodano nowego pracownika!");
}

where I save my data in localStorage. Now, I would like to read the data, so i'm doing like this:

function Wyswietl(){

    var zawartosc = localStorage.getItem('Pracownicy');
    document.write(zawartosc)
}

but it shows me this disordered . I would like to ask , how could I do this to me right away that displayed a table ? I'am begginer in js.

Upvotes: 2

Views: 112

Answers (1)

Ellbar
Ellbar

Reputation: 4054

The easiest way would be sorting an array after you get it from the localStorage. This way you will have an array always in the same order. In the real world scenario you should always have an employeeId property or any other key which you can use in the sort method.

Upvotes: 1

Related Questions