Bootstrap table - reload records according to ajax json object


I'm creating a website with Bootstrap . Initially when loading the page I use PHP to insert the results in the cells of the table. So far so good.

Then I use a button with a modal menu of Bootstrap which has a series of fields that are indicated for later when clicking on execute called through JQuery , < strong> Ajax , to a PHP page by sending POST 4 parameters.

This query returns a Json object that I include in a array of JavaScript but I am not able to have my table show me that data.

I've really tried almost everything but something must be happening to me.

I know that the Json object returns a array such that:

  "dia_semana":"friday   ",

And as far as I know, which is not much, it is linked by JavaScript as follows:

  $('#executaquery').on('click', function(){

          //  Recupero valors per POST to php

          var usuari = document.getElementById('usuari').value;
          var estat = document.getElementById('estat').value;
          var data_in = document.getElementById('data_in').value;
          var data_fi = document.getElementById('data_fi').value;

          $.ajax({                                                              // Llamada a queryProduct.php devuelve objeto array JSON asigno a
             method: 'POST',                                                      // los campos de modal window
             dataType: 'json',
             url: 'queryFitxatges.php',
             data: { usuaris: usuari, estats: estat, datain: data_in, datafi: data_fi},

             success: function(response) {


                var myData = [];

                $('#tableprod').bootstrapTable('load', myData);

I have passed all the code I use to send the parameters of the query and return the json object. The line of code that links the data to the table is:

$ ('# tableprod'). bootstrapTable ('load', myData);

Finally, for the examples that I could see in the official documentation, it would be linked directly with the previous instruction and the definition of the fields of the json object identical to the data-fields of the table. That I also have it like this:

      <table class='table-bordered' id='tableprod'

      <thead class='thead-inverse'>
              <th data-switchable='false' data-checkbox='true'></th>
              <th data-field='estado' data-sortable='true'><?php echo $cols[0]; ?></th>
              <th data-field='id_usuario' data-sortable='true'><?php echo $cols[1]; ?></th>
              <th data-field='nombre' data-sortable='true'><?php echo $cols[2]; ?></th>
              <th data-field='apellido1' data-sortable='true'><?php echo $cols[3]; ?></th>
              <th data-field='id_addtime_horario' data-sortable='true'><?php echo $cols[4]; ?></th>
              <th data-field='fecha' data-sortable='true'><?php echo $cols[5]; ?></th>
              <th data-field='dia_semana' data-sortable='true'><?php echo $cols[6]; ?></th>
              <th data-field='hora_ini' data-sortable='true'><?php echo $cols[7]; ?></th>
              <th data-field='hora_fin' data-sortable='true'><?php echo $cols[8]; ?></th>
              <th data-field='jornada' data-sortable='true'><?php echo $cols[9]; ?></th>
              <th data-field='jornada_flexibilidad' data-sortable='true'><?php echo $cols[10]; ?></th>
              <th data-field='jornada_deficit' data-sortable='true'><?php echo $cols[11]; ?></th>
              <th data-field='extra_jornada' data-sortable='true'><?php echo $cols[12]; ?></th>
              <th data-field='extra_autoritzado' data-sortable='true'><?php echo $cols[13]; ?></th>
              <th data-field='edit' data-sortable='false' data-switchable='false'>EDIT</th>
    <?php while ($row = pg_fetch_row($result)){ ?>
          <tr id='<?php echo $row[14]; ?>' data-idfixatgedetall='<?php echo $row[15]; ?>' data-estado='<?php echo $row[0] ?>' data-autoritzat='<?php echo $autoritzat = $row[12]; ?>'>
              <td data-field='estado'><?php echo $estado = EstadoIcon($row[0]); ?></td>
              <td data-field='id_usuario'><?php echo $row[1]; ?></td>
              <td data-field='nombre'><?php echo $row[2]; ?></td>
              <td data-field='apellido1'><?php echo $row[3]; ?></td>
              <td data-field='id_addtime_horario'><?php echo $row[4]; ?></td>
              <td data-field='fecha'><?php echo $date = FormatDate($row[5]); ?></td>
              <td data-field='dia_semana'><?php echo $dia = Dia($row[6]); ?></td>
              <td data-field='hora_ini'><?php echo $time = FormatTime24($row[7]); ?></td>
              <td data-field='hora_fin'><?php echo $time = FormatTime24($row[8]); ?></td>
              <td data-field='jornada'><?php echo $time = FormatTime($row[9]); ?></td>
              <td data-field='jornada_flexibilidad'><?php echo $time = FormatTime($row[10]); ?></td>
              <td data-field='jornada_deficit'><?php echo $time = FormatTime($row[11]); ?></td>
              <td data-field='extra_jornada'><?php echo $time = FormatTime($row[12]); ?></td>
              <td data-field='extra_autoritzat'><?php echo $autoritzat = Autoritzat($row[13]); ?></td>
              <td><button class='btn btn-xs edit btn-addcom' data-toggle='modal' data-target='#edit'><i class="material-icons" style="font-size: 20px">edit</i> </button></td>
    <?php }  ?>

I have tried to do the same to the minimum expression without passing parameters, making a new table ... but there is no way.

Let's see if you can help me understand the theory and solve my problem.

Meanwhile I'll keep looking ...

Thank you!

asked by rumar 04.01.2017 в 17:52

2 answers


You should perform some tests that returns. That is, verify if you receive a JSON or a string and you must parse it.

What I see is that you are mixing a bit of jQuery with a little javascript + DOM . But I think the problem is with the array push that you get from PHP.

I went to clean as I think it should be ...

 $('#executaquery').on('click', function(){
          var usuari = $('#usuari').val();
          var estat = $('#estat').val();
          var data_in = $('#data_in').val();
          var data_fi = $('#data_fi').val();

       $.post('queryFitxatges.php',{ 'usuaris': usuari, 'estats': estat, 'datain': data_in, 'datafi': data_fi},function(response){
                if (typeof(response) == "string"){response= JSON.parse(response);) 
                $('#tableprod').bootstrapTable('load', response);           

answered by 29.01.2017 в 01:57

The problem is these two lines within success :

var myData = [];

What happens is that myData is a array of objects, and by making the push with the server response, what you are generating is an array that in first position has < strong> another fix which is the response of your server, basically:

[      // Empieza la posición 0 de myData
    {    // Empieza la posición 0 de la respuesta
    },   // Termina la posisión 0 de la respuesta
    {    // Empieza la posición 1 de la respuesta
    }    // Termina la posisión 1 de la respuesta
  ]   // Termina la posición 0 de myData

All you have to do is skip those lines and repopulate the table like this:

$('#tableprod').bootstrapTable('load', response);

Then the code of your AJAX call should look like this:

    method: 'POST',
    dataType: 'json',
    url: 'queryFitxatges.php',
    data: { usuaris: usuari, estats: estat, datain: data_in, datafi: data_fi},
    success: function(response) {
               $('#tableprod').bootstrapTable('load', response);
answered by 08.12.2017 в 21:49