Reda RACHID Reda RACHID - 6 months ago 13
jQuery Question

jQuery Ajax query display List in Table

i think a picture is gonna explain my problem better

error

i need this ( " Object Appel d'Offre : [ La Fourniture et installation ] ")

to be shown only once.

the jQuery Code :

$(document).on("click","#bt_search",function (e) {
$.ajax({
url : '/Lots/Search',
type : 'POST',
dataType: 'json',
data : { RefAO : $("#RefAppelOffre").val() },
success : function(list) {
if(list == "fail"){

alert('error');

}
else{

$.each(list, function (i) {

$("#titreAppend").append("<h3 style='margin-top:55px'>Object Appel d'Offre : [ <u style='color:#f43030'><b> " + list[i].Objet + "</b></u> ] </h3>");
$("#tab tbody").append("<tr>" +
"<td>" + list[i].Ref_Lot + "</td>" +
"<td>" + list[i].Titre + "</td>" +
"<td>" + list[i].TotalLotTTC + "</td>" +
"<td>" + list[i].NombreConcurrent + "</td>" +
"<td>" + list[i].NombreArticle + "</td>" +
"</tr>");
})

}//Fermeture Else
}//Fermeture Success

})//fermeture ajax {}

})


the Asp.net Back Code :

[HttpPost]
public JsonResult Search(string RefAO)
{

var list = GlobalVariables.ctx.GetDetailedListLots(RefAO).ToList<GetDetailedListLots_Result>();
//var list = ctx.Bilan_Lot(RefAO).ToList<Bilan_Lot_Result>();
if(list != null)
{

return Json(list, JsonRequestBehavior.AllowGet);

}
else
{
return Json("fail");
}

}


i know it's abvious that the title am printing is going to be appended each time , can anybody give me a solution for this ?

Answer

Try this

$("#bt_search").click(function () {
    $.ajax({
        url : '/Lots/Search',
        type : 'POST',
        dataType: 'json',
        data : {  RefAO : $("#RefAppelOffre").val()  },
        success : function(list) {
            if(list == "fail") {
                alert('error');
            }
            else {
                if (list.length) {
                    $("#titreAppend").append("<h3 style='margin-top:55px'>Object Appel d'Offre  : [ <u style='color:#f43030'><b> " + list[0].Objet + "</b></u> ] </h3>");
                }
                $.each(list, function (i) {                
                   $("#tab tbody").append("<tr>" +
                                                "<td>" + list[i].Ref_Lot + "</td>" +
                                                "<td>" + list[i].Titre + "</td>" +
                                                "<td>" + list[i].TotalLotTTC + "</td>" +
                                                "<td>" + list[i].NombreConcurrent + "</td>" +
                                                "<td>" + list[i].NombreArticle + "</td>" +
                                            "</tr>");
                })

            }//Fermeture Else
        }//Fermeture Success

   })//fermeture ajax {}               
})