Refresh Bootstrap-table

Asked

Viewed 1,558 times

1

The problem is that I can’t get the table (bootstrap-table) to update the data after the registration. I am trying to do this via JS, but without success. I tried the following:

-- JS --

$.post($form.attr('action'), $form.serialize(), function (result) {
        if (result.status == "true") {
            $(location).attr('href', result.acao.url);
        } else {
            $('#cargo').formValidation('resetForm', true)
            $('#cadastroCargo').modal('hide')
            //TENTATIVA DO REFRESH NA TABLE:
            $('#teste').bootstrapTable('refresh')
        }
    }, 'json');

-- HTML/PHP --

<button class="btn btn-primary pull-right btn-import-user btn-sm" data-toggle="modal" data-target="#cadastroCargo">Novo Cadastro</button>

<!-- Modal -->
<div class="modal fade" id="cadastroCargo" tabindex="-1" data-keyboard="false" data-backdrop="static" role="dialog" aria-labelledby="cargoLabel">

<div class="modal-dialog" role="document">
<div class="modal-content">
<form id="cargo" action="Cargo/inserir" method="POST" enctype="multipart/form-data" autocomplete="off">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="cargoLabel">Cadastrar Cargo</h4>
                </div>
                <div class="modal-body">
                    <fieldset>
                        <div class="form-group">
                            <label class="modal-font-body control-label">Informe o Cargo</label>
                            <input name="titulo" type="text" class="form-control input-sm" id="titulo" data-minlength="4" size="35" value="<?= @$cargo->titulo ?>" data-error="Por favor, preencha este campo corretamente!" required>
                            <input type="hidden" name="id"  value="<?= @$cargo->id ?>">
                            <input type="reset" id="configreset" value="reset" hidden>
                        </div>

                        <div id="mensagemSucesso" class="alert alert-success alerta-sucesso" hidden>

                        </div>
                    </fieldset>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
                    <input type="submit" id="salvar" value="Salvar" name="salvar" class="btn btn-primary">
                </div>
            </form>
</div>
        </div>
    </div>
</div> 

<table id="teste" name="teste" class="table table-striped" data-toggle="table" data-search="true" data-show-refresh="true" data-show-columns="true"
<thead>
                <tr>
                    <th class="th-small" data-align="left" data-sortable="true">ID</th>
                    <th data-align="left" data-sortable="true">Nome</th>
                    <th class="th-small">Ações</th>
                </tr>
            </thead>
            <tbody>
                <?php
                foreach ($cargo as $key => $v) {
                    ?>
                    <tr>
                        <td><?= $v->id ?></td>
                        <td><?= $v->titulo ?></td>

                        <td>
                            <div class="dropdown">
                                <button class="btn btn-default dropdown-toggle" type="submit" data-toggle="dropdown">... <span class="caret"></span></button>
                                <ul class="table-modal dropdown-menu">
                                    <li><a data-remote="Cargo/page/visualizar/<?= $v->id ?>" role="button" data-toggle="modal" data-target="#select-modal">Visualizar</a></li>
                                    <li><a data-remote="Cargo/page/alterar/<?= $v->id ?>" data-toggle="modal" data-target="#editarIdade">Editar</a></li>
                                </ul>
                            </div>  
                        </td>
                    </tr>
                <?php } ?>
            </tbody>        
        </table>
  • What is the response format of your ajax call?

  • I’m sorry, but I don’t understand. What do you mean?

  • Does the server-side response come as xml, json or what? This information will help you update your table. The PHP code displayed will not help much because you need the AJAX answer.

  • Okay, the answer comes as json..

  • You will have to parse the result and display after the request.

  • 1

    Bring the data back to the grid. By giving the refresh, do: .bootstrapTable('refresh', {url:&#xA;'link/para/meu/arquivo.json'});

Show 1 more comment

1 answer

3


This refresh only works if the plugin is mounting the table - in your case, you are mounting the table with foreach.

You can have the plugin create the table for you using js, or using the parameter data-url="dados-table.php". I usually use this option. Just create another file, play the query and return of the records on json_encode. (Ah, in that case you have to use the attribute data-toggle="table" pro plugin understand that has to mount that table for you - also you can initialize it via js if you prefer).

Your table in html code is very simple, only with ths (no td):

<table id="table"
       class="table"
       data-toggle="table"
       data-url="sql/dados-table.php">
  <thead>
    <tr>
      <th data-field="nome-do-campo-aqui">
        Titulo do TH
      </th>
   </tr>
  </thead>
</table>

If you do so, the refresh will work exactly as it is in your code.

Here you can see the refresh documentation (and everything else bootstrap-table provides).

Here there’s an example of what I tried to explain.

ps in the example they use a file .json, I’m wearing a .php returning me with json_encode and it works too.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.