2013-10-17 162 views
0

我有這個模板,我做了一個ajax調用來獲取藥品的價格,我得到了一個數據對象,我無法在表格中顯示數據的問題,''導致它覆蓋最後一個值和其他問題,因爲它不會讓表中的任何其他數據清除所有數據,如nombre_medicamento或presentacion,它會創建一個新表。 我使用Django和PythonJson響應表格覆蓋

<script> 
$(document).ready(function() { 
    var data = $("#attendees tr.data").map(function (index, elem) { 
    var ret = []; 
     $('.inputValue', this).each(function() { 
      var d = $(this).val()||$(this).text(); 
      ret.push(d); 
      buscarprecio(d); 
    }); 
    return ret; 
}); 

function buscarprecio(idgrupo) { 
     $.ajax({ 
      url: "/precio_search", 
      type: "POST", 
      dataType: "json", 
      data: { 
       grupname: idgrupo, 
       csrfmiddlewaretoken: '{{ csrf_token }}' 
      }, 
       success: function (json) { 

       var jsonResponse = eval(json); 
       var tbl_body = ""; 
         var tbl_row = ""; 
       $.each(jsonResponse, function(index, element){ 

         $.each(this, function(k , v) { 
         tbl_row += "<td>"+ jsonResponse[0]["fields"]["medicamento_precio"]+"  </td>"; 
        }) 
        tbl_body += "<tr>"+tbl_row+"</tr>"; 
      }) 

        $("#attendees tbody").html(tbl_body); 
       }, 
      error: function (xhr, errmsg, err) { 
       alert(xhr.status + " Inside error : " + xhr.responseText); 
      } 
     }); 
    } 

}); 
</script> 
<table id="attendees" class="attendees"> 
<tbody> 
<thead> 
     <tr> 
       <th style="text-align: center"><label>Nombre Del Medicamento</label></th> 
       <th style="text-align: center"><label>Presentaciòn</label></th> 
       <th style="text-align: center"><label>Precio</label></th> 
       <th style="text-align: center"><label>Fecha En Que Se Extendio La Receta</label></th> 

      </tr> 
      </thead> 
      {% if formulario %} 
       {% for post in formulario %} 
       <tr class="data"> 
        <td style="text-align: center" class="inputValue"> {{ post.nombre_medicamento_2.id}}</td> 
        <td style="text-align: center"> {{ post.presentacion_3}}</td> 
        <td style="text-align: center" class="precio"></td> 
        <td style="text-align: center"> {{ post.receta_farmacos }}</td> 
       </tr> 
      {% endfor %} 
     {% endif %} 
    </tbody> 
</table> 
{% endblock %} 

回答

0

工作,所以在你的代碼的問題是你是overwritting表身與該行$("#attendees tbody").html(tbl_body);

您可以使用$().append()將新行添加到您的表。

查看文檔here的示例。