简体   繁体   中英

setting jQuery value into “items” attribute of <c:forEach>

I'm trying to replace the "items" attribute value of c:forEach tag, with the jQuery value I got from below.

$.ajax({
    type:"POST",
    url:"tour_list_json?"+$.param(ratingJSON),
    async:true,
    contentType:"application/x-www-form-urlencoded;charset=utf-8",
    success:function(jsonObject) {
        $('c\\:forEach:first').attr('items', jsonObject);
    }
});

Below is the tag I'm trying to make a change.

<c:forEach var="product" items="${productList}">

Here I want items="${productList}" to become items="jsonObject" so the tag only prints out the newly obtained list of products.

As you can see, I tried with $('c\:forEach:first').attr('items', jsonObject); but it doesn't change the result and I still see the original list of products. Can anyone help?

You cannot use like $('c\\:forEach:first').attr('items', jsonObject); since you are using JSP you set productList in your ModelMap

   model.put("productList",productList);

And in your jsps just use

@RequestMapping(value = "/getAllProducts", method = RequestMethod.GET)
public List getAllProducts(ModelMap model) {
    ArrayList<Product> productList=new ArrayList<E>();
    //productList  add values
    return productList;
}

 success:function(jsonObject) {
     $(jsonObject).each(function(product){
       let productHTML = `<p>Product ${product.name}  Price ${product.price}`;
       $("#yourProductListEl").append(productHTML);
     })
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM