2017-07-13 45 views
0

我正在使用jQuery bootgrid在表中显示数据。我使用Ajax获取数据,并以JSON格式返回值。在JSON字符串中,它包含一个我想要读取的变量,以便在表格的其他部分中显示。Bootgrid读取JSON中的返回值

的Ajax功能如下:

function ajaxAction(action) { 
      data = $("#frm_"+action).serializeArray(); 
      $.ajax({ 
       type: "POST", 
       url: "response_pedidos.php", 
       data: data, 
       dataType: "json",  
       success: function(response) 
       { 
       $('#'+action+'_model').modal('hide'); 
       $("#employee_grid").bootgrid('reload'); 
       }, 
       error: function (request, error) { 
       console.log(arguments); 
      } 
      }); } 

我看从PHP页面响应,并具有以下格式:

{"current":1, 
"rowCount":20, 
"total":7, 
"cantidad_totales":8.5, 
"id_pedido":13, 
"rows":[{"id_pedidos_productos" :"57", 
      "cantidad":"1.5", 
      "descripcion":"prueba con decimales", 
      "nombre":"Astro naranja"}, 
     {"id_pedidos_productos":"52", 
      "cantidad":"1", 
      "descripcion":"", 
      "nombre":"Gipso grande"}, 
     {"id_pedidos_productos":"54", 
      "cantidad":"1", 
      "descripcion":"", 
      "nombre":"Lilis Rosita"}, 
     {"id_pedidos_productos":"53", 
      "cantidad":"1", 
      "descripcion" :"", 
      "nombre":"Plumosos"}, 
     {"id_pedidos_productos":"56", 
      "cantidad":"1", 
      "descripcion":"", 
      "nombre":"ROSAS BABY VENDELA"}, 
     {"id_pedidos_productos":"55", 
      "cantidad":"1", 
      "descripcion":"", 
      "nombre":"Rosas rojas"}, 
     {"id_pedidos_productos":"51", 
      "cantidad":"2", 
      "descripcion":"", 
      "nombre":"ROSAS ROSITAS \"MATIZADAS\"" }]} 

在页面上,我的表看起来像这样,我想在下面的表格中显示获得的值: enter image description here

现在,我想要做的是读取返回的值:“cantidad_totales”。

我想赶上它显示在页面的简历部分。

有谁知道我该怎么做?

回答

1

这是你能怎么处理它:

var cantidadTotales; 

$('#tbl').bootgrid({ 
    formatters:{ 
    ... 
    }, 
    rowCount: [5, 10, 25], 
    ... 
    labels: { 
    .... 
    }, 
    css: { 
    ... 
    }, 
     responseHandler: function (data) { 
      var response = { 
       current: data.current, 
       rowCount: data.rowCount, 
       rows: data.rows, 
       total: data.total, 
       cantidad_totales: data.cantidad_totales, 
       id_pedido: data.id_pedido 
      }; 
      //store cantidad_totales into a variable... 
      cantidadTotales = data.cantidad_totales; 

      return response; 

     }, 
     ajaxSettings: { 
      method: 'POST', 
      contentType: 'application/json' 
     }, 
     ajax: true, 
     url: 'The_Url_To_Load_Your_Data' 
    }).on("loaded.rs.jquery.bootgrid", function (s) { 
     //Now, display the cantidad_totales in your div or whatever 
     $('div#YourTotalDiv').html(cantidadTotales); 
    }); 
}) 
+0

非常感谢你,不知道respondeHandler和requestHandler。这个功能解决了现在我可以观察数据并在表格外使用它的问题。 –