Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Localstorage - mala prosba o pomoc
#1
Co usunac w tym kodzie, aby zamiast 4 wartosci (first name, last name, email, entry_id) wyświetlal tylko jedną - first name ?

Kod:
<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <title>test</title>
    </head>
<body>
    <table id="contacts-table">
        <tr id="contacts-head">
            <th>ID</th>
            <th>First name</th>
            <th>Last name</th>
            <th>Email</th>
            <th>Actions</th>
        </tr>
    </table>

    <form id="contacts-form">
        <div class="item text">
            <label>First name:</label>
            <div class="field"><input type="text" name="first_name" /></div>
        </div>
        <div class="item text">
            <label>Last name:</label>
            <div class="field"><input type="text" name="last_name" /></div>
        </div>
        <div class="item text">
            <label>Email:</label>
            <div class="field"><input type="text" name="email" /></div>
        </div>
        <div class="button-wrapper"><div style="display: none;">
            <div class="item button">
                <div class="field"><input type="button" id="contacts-op-discard" value="Discard" /></div>
            </div></div>
            <div class="item button button-default">
                <div class="field"><input type="submit" id="contacts-op-save" value="Save" /></div>
            </div>
        </div>
        <input type="hidden" name="id_entry" value="0" />


    <script>
        var Contacts = {
            index: window.localStorage.getItem("Contacts:index"),
            $table: document.getElementById("contacts-table"),
            $form: document.getElementById("contacts-form"),
            $button_save: document.getElementById("contacts-op-save"),
            $button_discard: document.getElementById("contacts-op-discard"),

            init: function() {
                // initialize storage index
                if (!Contacts.index) {
                    window.localStorage.setItem("Contacts:index", Contacts.index = 1);
                }

                // initialize form
                Contacts.$form.reset();
                Contacts.$button_discard.addEventListener("click", function(event) {
                    Contacts.$form.reset();
                    Contacts.$form.id_entry.value = 0;
                }, true);
                Contacts.$form.addEventListener("submit", function(event) {
                    var entry = {
                        id: parseInt(this.id_entry.value),
                        first_name: this.first_name.value,
                        last_name: this.last_name.value,
                        email: this.email.value
                    };
                    if (entry.id == 0) { // add
                        Contacts.storeAdd(entry);
                        Contacts.tableAdd(entry);
                    }
                    else { // edit
                        Contacts.storeEdit(entry);
                        Contacts.tableEdit(entry);
                    }

                    this.reset();
                    this.id_entry.value = 0;
                    event.preventDefault();
                }, true);

                // initialize table
                if (window.localStorage.length - 1) {
                    var contacts_list = [], i, key;
                    for (i = 0; i < window.localStorage.length; i++) {
                        key = window.localStorage.key(i);
                        if (/Contacts:\d+/.test(key)) {
                            contacts_list.push(JSON.parse(window.localStorage.getItem(key)));
                        }
                    }

                    if (contacts_list.length) {
                        contacts_list
                            .sort(function(a, b) {
                                return a.id < b.id ? -1 : (a.id > b.id ? 1 : 0);
                            })
                            .forEach(Contacts.tableAdd);
                    }
                }
                Contacts.$table.addEventListener("click", function(event) {
                    var op = event.target.getAttribute("data-op");
                    if (/edit|remove/.test(op)) {
                        var entry = JSON.parse(window.localStorage.getItem("Contacts:"+ event.target.getAttribute("data-id")));
                        if (op == "edit") {
                            Contacts.$form.first_name.value = entry.first_name;
                            Contacts.$form.last_name.value = entry.last_name;
                            Contacts.$form.email.value = entry.email;
                            
                        }
                        else if (op == "remove") {
                            if (confirm('Are you sure you want to remove "'+ entry.first_name +' '+ entry.last_name +'" from your contacts?')) {
                                Contacts.storeRemove(entry);
                                Contacts.tableRemove(entry);
                            }
                        }
                        event.preventDefault();
                    }
                }, true);
            },

            storeAdd: function(entry) {
                entry.id = Contacts.index;
                window.localStorage.setItem("Contacts:index", ++Contacts.index);
                window.localStorage.setItem("Contacts:"+ entry.id, JSON.stringify(entry));
            },
            storeEdit: function(entry) {
                window.localStorage.setItem("Contacts:"+ entry.id, JSON.stringify(entry));
            },
            storeRemove: function(entry) {
                window.localStorage.removeItem("Contacts:"+ entry.id);
            },

            tableAdd: function(entry) {
                var $tr = document.createElement("tr"), $td, key;
                for (key in entry) {
                    if (entry.hasOwnProperty(key)) {
                        $td = document.createElement("td");
                        $td.appendChild(document.createTextNode(entry[key]));
                        $tr.appendChild($td);
                    }
                }
                $td = document.createElement("td");
                $td.innerHTML = '<a data-op="edit" data-id="'+ entry.id +'">Edit</a> | <a data-op="remove" data-id="'+ entry.id +'">Remove</a>';
                $tr.appendChild($td);
                $tr.setAttribute("id", "entry-"+ entry.id);
                Contacts.$table.appendChild($tr);
            },
            tableEdit: function(entry) {
                var $tr = document.getElementById("entry-"+ entry.id), $td, key;
                $tr.innerHTML = "";
                for (key in entry) {
                    if (entry.hasOwnProperty(key)) {
                        $td = document.createElement("td");
                        $td.appendChild(document.createTextNode(entry[key]));
                        $tr.appendChild($td);
                    }
                }
                $td = document.createElement("td");
                $td.innerHTML = '<a data-op="edit" data-id="'+ entry.id +'">Edit</a> | <a data-op="remove" data-id="'+ entry.id +'">Remove</a>';
                $tr.appendChild($td);
            },
            tableRemove: function(entry) {
                Contacts.$table.removeChild(document.getElementById("entry-"+ entry.id));
            }
        };
        Contacts.init();
    </script>
    
</body>
</html>

Z gory dzieki za pomoc Smile
FM
Odpowiedz


Podobne wątki…
Wątek: Autor Odpowiedzi: Wyświetleń: Ostatni post
  [JS] Pomoc w dodaniu polecenia do strony internetowej Miczki 5 4,793 19-06-2013, 14:26
Ostatni post: Kartofelek
  [CSS] Problem z wyglądem menu w Firefox a IE proszę o pomoc adrin6 4 4,988 18-06-2013, 22:33
Ostatni post: adrin6
Sad pomoc w kodzie css rapek 2 2,727 13-01-2013, 17:41
Ostatni post: Kartofelek
  Pomoc przy tłach erfix 12 5,447 09-09-2012, 23:34
Ostatni post: erfix
  Drop Down Menu, prośba o pomoc razekAB 5 4,771 13-08-2012, 16:59
Ostatni post: Kartofelek

Skocz do:


Użytkownicy przeglądający ten wątek: 1 gości
Sponsorzy i przyjaciele
SeoHost.pl