0

I'm using jQuery bootgrid to display data in table. I get data using Ajax and I return the values in JSON format. On the JSON string it comes a variable I want to read to display in other section out of the table.

Ajax function is the following:

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);
            }
            }); }

I'm watching response from PHP page and it comes with the following format:

{"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\"" }]}

On the page, my table looks like this, I want to display obtained value in the field below the table: enter image description here

Now, what I want to do is to read returned value named: "cantidad_totales".

I would like to catch it to display in resume section of the page.

Does anyone know how can I do it ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lenin Ocaña
  • 81
  • 1
  • 9

1 Answers1

1

This is how you could handle it:

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);
    });
})
zinczinc
  • 544
  • 5
  • 11
  • Thank you very much, didn't know about respondeHandler and requestHandler. This function solved the problem now I can watch data and use it outside the table. – Lenin Ocaña Jul 26 '17 at 17:13
  • @zinczinc I a in same situation but the response handler is not executing, any idea? – Abdul Rehman May 31 '18 at 06:05
  • @Bsienn, sorry for the late reply. It's been quite a while since I used Bootgrid. Please, try to check for JavaScript or html errors on client-side by going to > More tools > Developer tools > Console (Chrome) or F12 (Edge/IE Explorer). – zinczinc Jun 04 '18 at 11:11
  • Thanks for reply, There are no errors at all, I have switched to datatable, relieved. – Abdul Rehman Jun 04 '18 at 11:13
  • I don't get what you mean, I'm not using bootgrid anymore, And I have changed my current code to simple html table in ajax, I've simplified my code no searching sorting, just listing. – Abdul Rehman Jun 04 '18 at 11:25
  • @Bsienn, I might have misunderstood. Anyway, good to hear that you have solved your problem by switching to datatable. -:) Cheers! – zinczinc Jun 04 '18 at 11:30