2015-11-20 27 views
0

我使用此代碼在模態隱藏事件上使用Ajax查詢創建表。Dinamically創建表並在末尾添加小計行

$('#modalarticulos').on('hidden.bs.modal', function() {; 

    sel_articulos = $("input[name='check_art']:checked").map(function() 
    { 
     return this.value; 
    }).get(); 
console.log(sel_articulos); 
$("#sel_articulos tbody").empty(); 
ii =0; 
var tbody = $('#sel_articulos tbody'); 
$.each(sel_articulos, function(ii, sel_articulo) { 
    var tr = $("<tr id=artrow["+ii+"]>"); 
    $.ajax({ 
     type: 'POST', 
     url: "<?php echo $_SERVER_ADDR . PUBLIC_PATH .'presupuestos/buscart/'; ?>", 
     data: { 'id':sel_articulos[ii]}, 
     dataType: "json", 
     success: function(response) { 
      $("<td id='id["+ii+"]'>").html(response["id"]).appendTo(tr); 
      $("<td id='tipart["+ii+"]'>").html(response["tipart"]).appendTo(tr); 
      $("<td id='codart["+ii+"]'>").html(response["codart"]).appendTo(tr); 
      $("<td id='desart["+ii+"]'>").html(response["desart"]).appendTo(tr); 
      $("<td id='canart["+ii+"]'><input type='number' id='cantidad["+ii+"]' min='0' max='999' step='1' class='input-mini text-right cantidart' value='1'>").appendTo(tr); 
      $("<td id='precio["+ii+"]'>").html(response["precio"]).appendTo(tr); 
      $("<td id='subtot["+ii+"]'>").html("<label id='subtotal["+ii+"]' class='text-success subtotal'>"+response["precio"]).appendTo(tr); 
      tbody.append(tr); 
      }, 
     error: function() { 
      alert('Error occurs!'); 
      } 
     }); 
    }); 
$('#sel_articulos > tbody').after(<tr><td></td><td></td><td></td><td></td><td><label class='subtotalg'>SUB-TOTAL</label></td><td></td><td><label id='subtotalsi' class='text-success subtotalg'>Subtotal</label></td></tr>; 
}) 

問題1我現在面臨的問題是,我需要的最後一行(後被添加的一個,身體)是身體本身的內部,現在正在外面和創建因此它不是由清:

$("#sel_articulos tbody").empty(); 

問題2:除了我想知道是否有安排該表的創建,使之更有效率的另一種方式。謝謝。

+3

對於初學者,您不應該爲表中的每一行執行ajax調用。這是非常低效的。嘗試做一個單一的Ajax調用來返回一個數組數組,然後通過循環來填充你的表。 – Scottie

+0

@Scottie謝謝,我會盡力做一些研究並改進它。關於最後的,你知道有什麼問題嗎? – Pongr

回答

0

$.after在所選元素之後添加元素。您需要$.append,它在所選元素內添加一個元素。

這樣做:

$('#sel_articulos > tbody').append(<tr><td></td><td></td><td></td><td></td><td><label class='subtotalg'>SUB-TOTAL</label></td><td></td><td><label id='subtotalsi' class='text-success subtotalg'>Subtotal</label></td></tr>; 

UPDATE

根據您的意見,您需要添加它被添加的行之後。只要檢查你是否最先添加最後一行。

$.each(sel_articulos, function(ii, sel_articulo) { 
     $.ajax({ 
     type: 'POST', 
     url: "<?php echo $_SERVER_ADDR . PUBLIC_PATH .'presupuestos/buscart/'; ?>", 
     data: { 'id':sel_articulos[ii]}, 
     dataType: "json", 
     success: function(response) { 
      $("<td id='id["+ii+"]'>").html(response["id"]).appendTo(tr); 
      $("<td id='tipart["+ii+"]'>").html(response["tipart"]).appendTo(tr); 
      $("<td id='codart["+ii+"]'>").html(response["codart"]).appendTo(tr); 
      $("<td id='desart["+ii+"]'>").html(response["desart"]).appendTo(tr); 
      $("<td id='canart["+ii+"]'><input type='number' id='cantidad["+ii+"]' min='0' max='999' step='1' class='input-mini text-right cantidart' value='1'>").appendTo(tr); 
      $("<td id='precio["+ii+"]'>").html(response["precio"]).appendTo(tr); 
      $("<td id='subtot["+ii+"]'>").html("<label id='subtotal["+ii+"]' class='text-success subtotal'>"+response["precio"]).appendTo(tr); 
      tbody.append(tr); 

      // Check if added last row 
      if(ii == sel_articulos.length - 1){ 
        $('#sel_articulos > tbody').append(<tr><td></td><td></td><td></td><td></td><td><label class='subtotalg'>SUB-TOTAL</label></td><td></td><td><label id='subtotalsi' class='text-success subtotalg'>Subtotal</label></td></tr>; 
      } 
     }, 
     error: function() { 
      alert('Error occurs!'); 
     } 
    }); 
}); 

但是我會做什麼@Scottie在評論中說,並獲得一個請求中的所有表數據,而不是每行一個。這會讓這變得容易很多,因爲你可以在追加所有其他人之後追加它。

+0

這種方式其在tbody中創建,但在ajax查詢中生成的行之前。 – Pongr

+0

我更新了我的答案,將其考慮在內 –

0

您可以使用延遲(和承諾)更好地管理它,確實是在jQuery的阿賈克斯將返回無極一樣的界面,所以你可以嘗試:

var data = {}; 
var promise = $ajax.({ 
    ... 
    success: function (response) { 
     // Collect your data here 
     data['id'] = response['id']; 
     .... 
     // This means success and the done callbacks in the deferred will be executed. 
    } 
    ... 
}); 

// The done call back will be executed after your ajax success callback is called 
promise.done(function() { 
    // now use your data to create a table 
    ... 
}); 

瞭解更多關於jQuery的承諾,並推遲: https://api.jquery.com/promise/ https://api.jquery.com/category/deferred-object/

+0

通過在我的ajax調用中設置async:false來解決此問題...默認情況下它是true。 – Pongr

+1

異步:假可能不是一個好主意,因爲這是阻塞。 ajax的一個好處是,您仍然可以允許用戶在從服務器獲取內容的同時與您的網頁進行交互。 但是,當然,如果你需要在從表中獲取數據時阻止用戶交互,那麼這也許很好。 –