Elisa Agelou
Elisa Agelou

Reputation: 3

How do I create an edit function for a simple crud-application? - CRUD operations with VANILLA JAVASCRIPT

So I have created a simple CRUD-application using only JavaScript. Right now you can add countries to the array and delete countries from the array. I want to be able to edit existing countries in the array, say for example I want to change "Stockholm" to "Spain".

What would that edit function look like? Below is my current code, html and javascript.

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link href="style.css" type="text/css" rel="stylesheet">
</head>
<body>
    <div class="container">
    <h1>Städer</h1>
    <div id="output"></div>
    <div class="submit">
    <input type="text" id="staden" placeholder="Ny stad"/>
    <button onclick="laggaTill()" id="btnClick">Lägg till</button>
        </div>
    <script src="checkpoint1.js"></script>
    </div>
</body>
</html>

var stader = ["Stockholm", "Köpenhamn", "Paris"];
uppdateraOutput();

function uppdateraOutput(){
   var output = "";
   for (var i = 0; i < stader.length; i ++) {
       output += stader[i] + "[<span title = 'Ta bort " + stader[i] + "' onclick= 'taBort("+ i +")'> x </span>]<br/>";
   }
   document.getElementById("output").innerHTML = output;
}

function laggaTill() {
   console.log("Lägg till");
   var stad = document.getElementById("staden").value;
   if (stad.length != 0) {
       stader[stader.length] = stad;
       document.getElementById("staden").value = "";
       uppdateraOutput();
   }
}

function taBort(id) {
   console.log("Ta bort: " + id);
   var staderTemp = [];
   for (var i = 0; i < stader.length; i++){
       if (i !=id) {
           staderTemp.push(stader[i]);
       }
   }

   stader = staderTemp;
   uppdateraOutput();
}

function edit() {

}

Upvotes: 0

Views: 1621

Answers (3)

Jenny
Jenny

Reputation: 1

public IActionResult Edit(int id)
{
    var member = _context.TeamMembers.FirstOrDefault(x => x.Id == id);

    if (member == null)
        return RedirectToAction("error", "dashboard");

    return View(member);
}

[HttpPost]
public IActionResult Edit(TeamMember member)
{
    var existMember = _context.TeamMembers.FirstOrDefault(x => x.Id == member.Id);

    if (existMember == null)
        return RedirectToAction("error", "dashboard");

    if (existMember.ImageFile != null)
    {
        if (member.ImageFile.ContentType != "image/png" && member.ImageFile.ContentType != "image/jpeg")
        {
            ModelState.AddModelError("ImageFile", "Image file must be png or jpeg!");
            return View();
        }

        if (member.ImageFile.Length > 2097152)
        {
            ModelState.AddModelError("ImageFile", "Image file must be less than 2MB!");
            return View();
        }

        if (!ModelState.IsValid)
            return View();

        var newFileName = FileManager.Save(_env.WebRootPath, "uploads/teamMembers", member.ImageFile);

        FileManager.Delete(_env.WebRootPath, "uploads/teamMembers", existMember.Image);

        existMember.Image = newFileName;
    }

    existMember.FullName = member.FullName;
    existMember.Profession = member.Profession;
    existMember.Desc = member.Desc;
    existMember.TwitterUrl = member.TwitterUrl;
    existMember.FacebookUrl = member.FacebookUrl;
    existMember.InstagramUrl = member.InstagramUrl;
    existMember.LinkedinUrl = member.LinkedinUrl;

    _context.SaveChanges();
    return RedirectToAction("index");
}

Upvotes: 0

aksappy
aksappy

Reputation: 3400

There are two issues that I see.

  1. Manipulating document/html using text. This is hard to maintain. While learning it is fine, but not otherwise.
  2. Looping through the array for every change, add and edit

I have given you a different approach, without performing the two problems stated above.

        var clickedItem = null;
        function clickListener(e) {
            document.getElementById('staden').value = e.target.id
            clickedItem = e.target
        }
        function laggaTill() {
            var ul = document.getElementById("list")
            var stad = document.getElementById("staden").value;
            var li = document.createElement("li")
            li.id = stad
            li.addEventListener("click", clickListener)
            li.appendChild(document.createTextNode(stad))
            ul.appendChild(li)
            document.getElementById("staden").value = ""
        }
        function edit() {
            var item = document.getElementById(clickedItem.id)
            var stad = document.getElementById('staden').value
            item.id=stad
            item.innerHTML = stad
            clickedItem = null
            document.getElementById("staden").value = ""
        }

Upvotes: 0

Diogo Peres
Diogo Peres

Reputation: 1372

You can add a click function to each item and when you click on the text it opens a prompt window to change the text.

I changed the uppdateraOutput function to add a click function to the text.

<span onclick='edit("+ i +")'>" + stader[i] + "</span>

Like the code below.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link href="style.css" type="text/css" rel="stylesheet">
</head>
<body>
    <div class="container">
    <h1>Städer</h1>
    <div id="output"></div>
    <div class="submit">
    <input type="text" id="staden" placeholder="Ny stad"/>
    <button onclick="laggaTill()" id="btnClick">Lägg till</button>
        </div>
    <script src="checkpoint1.js"></script>
    </div>
	
	<script>
	var stader = ["Stockholm", "Köpenhamn", "Paris"];
uppdateraOutput();

function uppdateraOutput(){
   var output = "";
   for (var i = 0; i < stader.length; i ++) {
       output += "<span onclick='edit("+ i +")'>" + stader[i] + "</span> [<span title = 'Ta bort " + stader[i] + "' onclick= 'taBort("+ i +")'> x </span>] <br/>";
   }
   document.getElementById("output").innerHTML = output;
}

function laggaTill() {
   console.log("Lägg till");
   var stad = document.getElementById("staden").value;
   if (stad.length != 0) {
       stader[stader.length] = stad;
       document.getElementById("staden").value = "";
       uppdateraOutput();
   }
}

function taBort(id) {
   console.log("Ta bort: " + id);
   var staderTemp = [];
   for (var i = 0; i < stader.length; i++){
       if (i !=id) {
           staderTemp.push(stader[i]);
       }
   }

   stader = staderTemp;
   uppdateraOutput();
}

function edit(index) {
	var item = prompt("Please enter your name", stader[index]);

	if (item !== null && item !== "") {
		stader[index] = item;
	}	
	uppdateraOutput();
}
	</script>
	
</body>
</html>

Upvotes: 1

Related Questions