Skip to content Skip to sidebar Skip to footer

Splice Json Array Javascript

I created a function for reusing the splice feature for javascript arrays however, after I run it once, it cannot be reused. var removePerson = function(d, person_id) { var per

Solution 1:

Your solution doesn't work because of how your populateList works.

In your populateList, you have a line:

$('#load').empty();

This line empties the table and removes the buttons attached with click event listener.

Then, you add completely new button.delete, which aren't attached with any event listener.

To solve this, you can put your .on() into populateList function.

var populateList = function(d) {
    $("#load").empty();
    var new_rows;
    for(var i = 0; i < d.length; i++) {
        new_rows += "<tr>" + 
            "<td>" + d[i].id + "</td>" +
            "<td>" + d[i].name + "</td>" +
            "<td>" + d[i].phone + "</td>" +
            "<td><button class='delete' data-id='" + d[i].id + "'>delete</button></td>" +
            "</tr>";
    }
    $("#load").append(new_rows);

    // delete event
    $(".delete").on("click", function() {
      var delete_sel = $(this).attr("data-id");
      populateList(removePerson(d, delete_sel));
    });
};

Here's a working jsFiddle.

Alternatively, you can use solution from this answer (which is a cleaner solution imo).

$("table").on("click",".delete", function() {
  var delete_sel = $(this).attr("data-id");
  populateList(removePerson(data, delete_sel));
});

More explanation on why his answer works on jQuery documentation (Look at the selector parameter).

Solution 2:

Try this code:JSFiddle

or the code snippet:

var removePerson = function(d, person_id) {	
	var person_index = d.findIndex(i => i.id == person_id);
	d.splice(person_index, 1);
  return d;
};

var populateList = function(d) {
	$("#load").empty();
	var new_rows;
	for(var i = 0; i < d.length; i++) {
		new_rows += "<tr>" + 
			"<td>" + d[i].id + "</td>" +
			"<td>" + d[i].name + "</td>" +
			"<td>" + d[i].phone + "</td>" +
			"<td><button class='delete' data-id='" + d[i].id + "'>delete</button></td>" +
			"</tr>";
	}
	$("#load").append(new_rows);
};


$(document).ready( function() {

		// initial listvar data = [
    	{
        "id": 1001,
        "name": "Andy Roy",
        "phone": "555-555-5551"
      },
      {
        "id": 1002,
        "name": "Bob Dillon",
        "phone": "555-555-5552"
      },
      {
        "id": 1003,
        "name": "Carl Sagan",
        "phone": "555-555-5553"
      }
    ];
		
    //initial populate listpopulateList(data);
    
    // delete event
    $("table").on("click",".delete", function() {
      var delete_sel = $(this).attr("data-id");
      populateList(removePerson(data, delete_sel));
    });
    
});
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><tableborder><thead><tr><th>ID</th><th>Name</th><th>Phone</th></tr></thead><tbodyid="load"></tbody></table>

 $("table").on("click",".delete", function() {
  var delete_sel = $(this).attr("data-id");
  populateList(removePerson(data, delete_sel));
});

Post a Comment for "Splice Json Array Javascript"