/    Sign up×
Community /Pin to ProfileBookmark

Updating a table without refreshing page

Hey

I have a table, where i can delete an item, when i refresh the page the item i deleted is gone. I want the item to disappear when i click the delete link (“Slet”). I think the problem is that i just call the showCart(); function when the item is deleted and the showCart(); function still holds the same data as when the page was loaded. So how to i make it update, without a refresh?

[code=html]<script type=”text/javascript”>
// Load cart
showCart();

// AJAX – remove an item from cart
function removeItemFromCart(id){
$.get(“sites/sell.php”, {remove: id}, function(xml) {
var output = “ew”+xml;
$(‘#formOutput’).append(output);
});
// Reset cart
showCart();
}

// AJAX – gets items for cart
function showCart(){
$.get(“sites/sell.php”, {data: $(this).html()}, function(xml) {
var output = “”;

$(xml).find(‘product’).each(function() {
var id = $(this).find(‘id’).text()
var name = $(this).find(‘name’).text()
var amount = $(this).find(‘amount’).text()
var price = $(this).find(‘price’).text()
var totalPrice = $(this).find(‘totalPrice’).text()

output += “<tr><td>”+name+”</td><td>”+amount+”</td><td>”+price+”</td><td>”+totalPrice+”</td><td><a href=”#” onclick=”removeItemFromCart(‘”+id+”‘)”>Slet</a></td></tr>”;
output += “<tr><td colspan=’5′ class=’shop_cart_table_break’><img src=’images/grafics/blank.gif’ alt=” /></td></tr>”;
});

$(‘#table2’).html(output);
});
}

$(document).ready(function(){
// Toggle cart table
$(“#tablebar”).click(function(e){
$(“#table”).toggle(“slow”);
});

// Do something with submit
$(“form”).submit(function() {
if ($(“input:first”).val()) {
$(“#formOutput”).html(“Validated…”).show();
return false;
}
$(“#formOutput”).text(“Not valid!”).show().fadeOut(1000);
return false;
});
});
</script>[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@MrNobodyJan 04.2009 — Well, depending upon the content of your [B]id[/B] variable, you can do this:
[CODE]output += "<tr [COLOR="Red"]id=""+id+"_row1"[/COLOR]><td...
output += "<tr [COLOR="Red"]id=""+id+"_row2"[/COLOR]><td...[/CODE]

and then change your function as follows:
[CODE] function removeItemFromCart(id){
$.get("sites/sell.php", {remove: id}, function(xml) {
var output = "ew"+xml;
$('#formOutput').append(output);
});
[COLOR="Red"]// update table
var obj = document.getElementById(id+"_row2");
obj.parentNode.removeChild(obj);
obj = document.getElementById(id+"_row1");
obj.parentNode.removeChild(obj);[/COLOR]
}[/CODE]
Copy linkTweet thisAlerts:
@smuikasJan 04.2009 — One way to do it is to hold a reference to your table:

[CODE]
var Cart = function() {
this.table = document.getElementById("TABLE_ID");
}
Cart.prototype.updateCart = function(xml) {
// clear the table:
while(this.table.childNodes.length > 0) {
this.table.removeChild(this.table.firstChild);
}
// populate it:
$(xml).find('product').each(function() {
var id = $(this).find('id').text()
var name = $(this).find('name').text()
var amount = $(this).find('amount').text()
var price = $(this).find('price').text()
var totalPrice = $(this).find('totalPrice').text()
var els = [];
els[els.length] = document.createElement("TD");
els[els.length-1].innerHTML = id;
/* snip */
els[els.length] = document.createElement("TD");
els[els.length-1].innerHTML = totalPrice;
var tr = document.createElement("TR");
for(var i = 0; i < els.length; i++) {
tr.appendChild(els[i]);
}
this.table.appendChild(tr);
}
}
[/CODE]


Doing it this way will keep your code readable (no human parsing of html strings), but you'll need to use event management to set up the link. Keeping the cart object persistent will help you out in the future, too.. it makes it much easier to do all sorts of things (sorting, etc).

Even better would be to create a data object that holds one item's information, and have a "parent" data object that holds a collection of the data objects. That way you can mutate them in code without having to parse out the XML each time.
Copy linkTweet thisAlerts:
@JJohnsenDKauthorJan 04.2009 — MyNoby

im not at my computer atm, but does'nt that remove the data from the table without putting any data in?

i mean the obj.parentNode.removeChild(obj); part.

Smukas, thanks... i will try that
Copy linkTweet thisAlerts:
@MrNobodyJan 04.2009 — MyNoby

im not at my computer atm, but does'nt that remove the data from the table without putting any data in?[/QUOTE]

It is not supposed to put any in. Your original function didn't put any in either. All it did was display what was already in it. In this case, it is already displayed, so there is no need to worry about that. That is what you asked for when you said, "So how to i make it update, without a refresh?"
×

Success!

Help @JJohnsenDK spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...