Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax to refresh a partial view using express and JQuery?

I would like to refresh a partial view using ajax. I kwow how to append the new data to the HTML, but I would like to know if there is a simpler way to do it.

I have partial view that does this

  • each x in data li x.name

I pass the data using !=partial('test',{data:data})

I want to call a function to render the partial view again without reloading the page.. and without doing append( ... ); Any Idea?? Or other way to do it...

like image 498
JJS Avatar asked Jun 11 '11 16:06

JJS


1 Answers

First, add a route for just the partial like

app.get('/mypartial', function (req, res) {
    //compute data here
    res.render('mypartial', {layout: false, data: data});
});

Then load the new HTML with a jQuery .get or .ajax call to /mypartial.

Then use jQuery to replace the HTML of the parent element by doing

$('#idofparentelementofyourpartial').html(responseHTML);

See also my answer to this similar question.

like image 170
Peter Lyons Avatar answered Sep 23 '22 06:09

Peter Lyons