我对Java比较陌生。我正在创建一个基于CRUD的应用程序,需要“编辑”功能中的帮助

时间:2018-09-23 12:33:42

标签: javascript html arrays json crud

我正在尝试创建一个纯Javascript CRUD应用程序。数据显示在HTML表中。 我已经创建了创建,读取和删除功能。在“编辑”功能中,我希望单击“编辑”链接时将数据显示在文本框中,并且还应具有一个“保存”按钮。 我应该如何进行?

var name = document.getElementById('Name').value;
var age = document.getElementById('Age').value;
var email = document.getElementById('Email').value;
var password = document.getElementById('Password').value;
var confirmpassword = document.getElementById('ConfirmPassword').value;
var dob = document.getElementById('DOB').value;
//add
var users = [];

function data(name, age, email, dob) {
  var user = {
    "name": name,
    "age": age,
    "email": email,
    "dob": dob
  }
  users.push(user);
  read(users);
  console.log(users);
}

//Read 

function read(users) {

  var html = "<table border='1|1'>";
  html += "<tr>";
  html += "<th>" + "Name" + "</th>"
  html += "<th>" + "Age" + "</th>"
  html += "<th>" + "Email" + "</th>";
  html += "<th>" + "DOB" + "</th>";
  html += "<th>" + "Edit" + "</th>";
  html += "<th>" + "Delete" + "</th>";
  html += "</tr>"

  var userhtml = document.getElementById('user');
  userhtml.innerHTML = '';
  for (var i = 0; i < users.length; i++) {
    html += "<tr>";
    html += "<td>" + users[i].name + "</td>";
    html += "<td>" + users[i].age + "</td>";
    html += "<td>" + users[i].email + "</td>";
    html += "<td>" + users[i].dob + "</td>";

    html += "<td>" + "<a href='#' onclick='editUsers()'>Edit</a>" + "</td>";
    html += "<td>" + "<a href='#' onclick='deleteUsers()'>Delete</a>" + "</td>";
    html += "</tr>";
  }
  html += "</table>";
  document.getElementById("user").innerHTML = html;
}

function deleteUsers(i) {
  debugger;
  users.splice(i, 1)
  read();
  console.log(users);
}
<form action="#" onsubmit="data(name, age, email, dob)">
  <div class="form-group">
    <label class="form-text">Name :</label>
    <input type="text" id="Name" placeholder="Enter Name" class="form-control" />
    <span id="ErrorName" class="text-danger"></span>
  </div>
  <div class="form-group">
    <label class="form-text">Age :</label>
    <input type="text" id="Age" placeholder="Enter Age" class="form-control" />
    <span id="ErrorAge" class="text-danger"></span>
  </div>
  <div class="form-group">
    <label class="form-text">Email :</label>
    <input type="text" id="Email" placeholder="Enter Email" class="form-control" />
    <span id="ErrorEmail" class="text-danger" />
  </div>
  <div class="form-group">
    <label class="form-text">Password  :</label>
    <input type="password" id="Password" placeholder="Enter Password" class="form-control" />
    <span id="ErrorPassword" class="text-danger"></span>
  </div>
  <div class="form-group">
    <label class="form-text">Confirm Password  :</label>
    <input type="password" id="ConfirmPassword" placeholder="Confirm Password" />
    <span id="ErrorConfirmPassword" class="text-danger"></span>
  </div>
  <div class="form-group">
    <label class="form-text">Date of Birth :</label>
    <input type="date" id="DOB" class="form-control" />
    <span id="ErrorDOB" class="text-danger"></span>
  </div>

  <div class="form-group col-xl-4 text-center">
    <input type="submit" id="Submit" class="btn btn-success" />
  </div>

</form>
<div class="user" id="user">

</div>

1 个答案:

答案 0 :(得分:0)

 function editUsers(index) {

        var x = document.getElementById('edit');
        x.style.display = "block";
        debugger;
        var userhtml1 = document.getElementById('edit');
        userhtml1.innerHTML = '';
        for (var i = 0; i < users.length; i++) {
            if (i == index) {
                userhtml1.innerHTML += ' <div class="form-group"> Name :<input class="form-control" id="EditName" type="text" value ="' + users[i].name + '"><br />' +
            'Age :<input class="form-control" id="EditAge" type="text" value="' + users[i].age + '"> <br /> ' +
            'Email :<input class="form-control" id="EditEmail" type="text" value="' + users[i].email + '"> <br /> ' +
            'DOB :<input class="form-control" id="EditDOB" type="text" value="' + users[i].dob + '"> <br /> ' +
            '<button class="edit" onclick="updateUser(' + i + ')">Update</button>';
            }
            else {

                userhtml1.innerHTML += '';
            }
        }
    }

    //CRUD - UPDATE
    function updateUser(index) {

        debugger;
        var updatename = document.getElementById('EditName').value;
        var updateage = document.getElementById('EditAge').value;
        var updateemail = document.getElementById('EditEmail').value;
        var updatedob = document.getElementById('EditDOB').value;

        if (updatename == '' || updateemail == '' || updateage == '' || updatedob == '') {
            alert("Please Fill the Fields!");
        }
        else {
            users[index].name = updatename;
            users[index].email = updateemail;
            users[index].age = updateage;
            users[index].dob = updatedob;
            read(users);
            var x = document.getElementById('edit');
            x.style.display = "none";
        }
    }