如何将数据保存在本地存储中?

时间:2019-06-16 14:22:49

标签: javascript html css

我想使用本地存储选项,即使刷新页面也可以在屏幕上显示数据。 就像使用addRow()添加行时一样,然后刷新页面,我还需要查看具有添加的Row数据的表。

我想使用javascript,因为我是javascript新手,我需要一些帮助。我能够自己进行编辑和删除选项。但是我在使用本地存储时需要帮助。

    let addRow = () => {
      let name = document.getElementById("name").value;
      let age = document.getElementById("age").value;
      let email = document.getElementById("email").value;
      let contact = document.getElementById("number").value;
        
      if(name === ""){
        alert('Please Fill Your Full Name');
        document.getElementById("name").focus();
        return false;
      }
      if(age === ""){
        alert('Please Fill Your Age');
        document.getElementById("age").focus();
        return false;
      }
      if(email === ""){
        alert('Please Fill Your Email');
        document.getElementById("email").focus();
        return false;
      }
      if(contact === ""){
        alert('Please Fill Your Mobile Number');
        document.getElementById("number").focus();
        return false;
      }
        
      let tr=document.createElement('tr');
      tr.innerHTML=`
      <td>${name}</td>
      <td>${age}</td>
      <td>${email}</td>
      <td>${contact}</td>
      <td>
        <button type="button" class="btn btn-warning" id="editBtn"> Edit </button>
        <button type="button" class="btn btn-danger" id="deleteBtn"> Delete </button>
      </td>`
        
      document.getElementById("tableDisplay").appendChild(tr);
      document.getElementById("name").value = '';
      document.getElementById("age").value = '';
      document.getElementById("email").value = '';
      document.getElementById("number").value = '';
      document.getElementById("tableDisplay").value = '';
    };
    document.getElementById("tableDisplay").addEventListener("click", (event) => {
      if (event.target.matches("#deleteBtn")) {
        let row = event.target.closest("tr");
        row.remove();
      }
    });

document.getElementById("tableDisplay").addEventListener("click", (event) => {
  if (event.target.matches("#editBtn")) {
    let row = event.target.closest("tr");
    row.setAttribute("contenteditable", "true");
    alert("Click on the text you want to edit!");
    row.cells[4].innerHTML = `<button type="button" class="btn btn-primary" id="saveBtn"> Save </button>`;
  }
});

document.getElementById("tableDisplay").addEventListener("click", (event) => {
  if (event.target.matches("#saveBtn")) {
    let row = event.target.closest("tr");
    row.setAttribute("contenteditable", "false");
    alert("Row data changed!");
    row.cells[4].innerHTML = `
      <button type="button" class="btn btn-warning" id="editBtn"> Edit </button>
      <button type="button" class="btn btn-danger" id="deleteBtn"> Delete </button>`;
  }
});
        
    document.getElementById("done").addEventListener("click", addRow);
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
    <div class="border border-dark rounded p-1 bg-light">
      <table class="text-center table" align="center" id="tableDisplay">
          <tr class="bg-warning">
            <th>Name</th>
            <th>Age</th>
            <th>Email</th>
            <th>Contact Number</th>
            <th></th>
          </tr>
          <tr>
            <td>John</td>
            <td>26</td>
            <td>foresto@worksmail.tk</td>
            <td>+917007984498</td>
            <td>
              <button type="button" class="btn btn-warning" id="editBtn"> Edit </button>
              <button type="button" class="btn btn-danger" id="deleteBtn"> Delete</button>
            </td>
          </tr>
          <tr>
            <td>Jimmy</td>
            <td>45</td>
            <td>foresto@official-sunveno.ru</td>
            <td>+917007985598</td>
            <td>
              <button type="button" class="btn btn-warning" id="editBtn"> Edit </button>
              <button type="button" class="btn btn-danger" id="deleteBtn"> Delete</button>
            </td>
          </tr>
          <tr>
            <td>Sarah</td>
            <td>32</td>
            <td>foresto@abac-compressoren.ru</td>
            <td>+917897984498</td>
            <td>
              <button type="button" class="btn btn-warning" id="editBtn"> Edit </button>
              <button type="button" class="btn btn-danger" id="deleteBtn"> Delete</button>
            </td>
          </tr>
          <tr>
            <td>Vanessa</td>
            <td>22</td>
            <td>foresto@newmotionrp.ru</td>
            <td>+917078984498</td>
            <td>
              <button type="button" class="btn btn-warning" id="editBtn"> Edit </button>
              <button type="button" class="btn btn-danger" id="deleteBtn"> Delete</button>
            </td>
          </tr>
        </table>
    </div>
    <div class="border border-dark rounded bg-warning p-3 mt-5">
      <form>
        <div class="form-group row">
          <label for="name" class="col-md-2 col-form-label">Name</label>
          <div class="col-md-10">
            <input class="form-control" type="text" placeholder="Full Name" id="name">
          </div>
        </div>
        <div class="form-group row">
          <label for="number" class="col-md-2 col-form-label">Age</label>
          <div class="col-md-10">
            <input class="form-control" type="number" placeholder="Age" id="age">
          </div>
        </div>
        <div class="form-group row">
          <label for="email" class="col-md-2 col-form-label">Email</label>
          <div class="col-md-10">
            <input class="form-control" type="email" placeholder="emailaddress@example.com" id="email">
          </div>
        </div>
        <div class="form-group row">
          <label for="number" class="col-md-2 col-form-label">Contact Number</label>
          <div class="col-md-10">
            <input class="form-control" type="text" placeholder="Call On" id="number">
          </div>
        </div>
      </form>
      <button class="btn btn-primary btn-block" id="done">ADD</button>
    </div>

1 个答案:

答案 0 :(得分:1)

HTML有点问题。您有多个具有相同ID的元素:例如

id="editBtn" & id="deleteBtn".  

此外,您在第5列中,但在行中只有4列。我对HTML和JS进行了一些编辑,使其看起来将删除行。您拥有的方式是在tbody后面追加行,但是您希望在tbody之前追加行,所以我在tbody中添加了一个id,但是您可以用另一种方式处理。

window.delRow = function(element)

之所以存在,是因为Fiddle显然将函数包装在一个函数中,因此调用该函数时出现问题。这并不是一个完整的答案,但我希望能有所帮助。您将必须查看对浏览器的支持:

element.closest('tr').remove();

我将HTML更改为此:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<div class="border border-dark rounded p-1 bg-light">
    <table class="text-center table" align="center" id="tableDisplay">
      <thead class="bg-warning">
        <tr>
          <th>Name</th>
          <th>Age</th>
          <th>Email</th>
          <th>Contact Number</th>
          <th></th>
        </tr>
      </thead>
      <tbody id = "tabelBody">
        <tr>
          <td>1</td>
          <td>Hello</td>
          <td>2</td>
          <td>2</td>
          <td>
            <button type="button" class="btn btn-warning editBtn"><i class="fa fa-pencil" aria-hidden="true"></i></button>
            <button type="button" class="btn btn-danger deleteBtn" onclick="delRow(this)"><i class="fa fa-trash" aria-hidden="true"></i></button>
          </td>
        </tr>
        <tr>
            <td>2</td>
            <td>Hello</td>
            <td>2</td>
            <td>2</td>
            <td>
              <button type="button" class="btn btn-warning editBtn"><i class="fa fa-pencil" aria-hidden="true"></i></button>
              <button type="button" class="btn btn-danger deleteBtn" onclick="delRow(this)"><i class="fa fa-trash" aria-hidden="true"></i></button>
            </td>
          </tr>
          <tr>
              <td>3</td>
              <td>Hello</td>
              <td>2</td>
              <td>2</td>
              <td>
                <button type="button" class="btn btn-warning editBtn"><i class="fa fa-pencil" aria-hidden="true"></i></button>
                <button type="button" class="btn btn-danger deleteBtn" onclick="delRow(this)"><i class="fa fa-trash" aria-hidden="true"></i></button>
              </td>
            </tr>
      </tbody>
    </table>
  </div>
  <div class="border border-dark rounded bg-warning p-3 mt-5">
    <form>
      <div class="form-group row">
        <label for="name" class="col-md-2 col-form-label">Name</label>
        <div class="col-md-10">
          <input class="form-control" type="text" placeholder="Full Name" id="name">
        </div>
      </div>
      <div class="form-group row">
        <label for="number" class="col-md-2 col-form-label">Age</label>
        <div class="col-md-10">
          <input class="form-control" type="number" placeholder="Age" id="age">
        </div>
      </div>
      <div class="form-group row">
        <label for="email" class="col-md-2 col-form-label">Email</label>
        <div class="col-md-10">
          <input class="form-control" type="email" placeholder="emailaddress@example.com" id="email">
        </div>
      </div>
      <div class="form-group row">
        <label for="number" class="col-md-2 col-form-label">Contact Number</label>
        <div class="col-md-10">
          <input class="form-control" type="text" placeholder="Call On" id="number">
        </div>
      </div>
    </form>    
    <button class="btn btn-primary btn-block" id="done">ADD</button>
  </div>

和JS:

let addRow = () => {
  let name = document.getElementById("name").value;
  let age = document.getElementById("age").value;
  let email = document.getElementById("email").value;
  let contact = document.getElementById("number").value;

  let tr=document.createElement('tr');
  tr.innerHTML=`
  <td>${name}</td>
  <td>${age}</td>
  <td>${email}</td>
  <td>${contact}</td>
  <td>
    <button type="button" class="btn btn-warning editBtn"><i class="fa fa-pencil" aria-hidden="true"></i></button>
    <button type="button" class="btn btn-danger deleteBtn" onclick="delRow(this)"><i class="fa fa-trash" aria-hidden="true"></i></button>
  </td>`

  document.getElementById("tabelBody").appendChild(tr);
  document.getElementById("name").value = '';
  document.getElementById("age").value = '';
  document.getElementById("email").value = '';
  document.getElementById("number").value = '';
  document.getElementById("tableDisplay").value = '';
};

window.delRow = function(element) {
    alert("test");
  element.closest('tr').remove();
}

document.getElementById("done").addEventListener("click", addRow);

不确定要在编辑时想要做什么。

显然有一个关于JS包装的Fiddle设置。