刷新Bootstrap表

And*_*son 6 javascript json twitter-bootstrap twitter-bootstrap-3 bootstrap-table

问题是我无法bootstrap-table在完成注册后使table()更新数据.我试图通过JS来做,但没有成功.我尝试过以下方法:

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')
        //ATTEMPT  REFRESH BOOTSTRAP-TABLE:
        $('#teste').bootstrapTable('refresh')
    }
}, 'json');
Run Code Online (Sandbox Code Playgroud)

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> 

<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>
Run Code Online (Sandbox Code Playgroud)

Dan*_*ose 5

当然不能刷新,你还没有按照doco或示例使用可以刷新的数据源。

http://bootstrap-table.wenzhixin.net.cn/documentation/

http://issues.wenzhixin.net.cn/bootstrap-table/

http://issues.wenzhixin.net.cn/bootstrap-table/#methods/refresh.html

您使用的是data from html类型方法,而不是data-url.

当页面首次请求时使用 php 打印到页面时,您如何期望表知道从哪里获取刷新的数据?

更容易修复创建表格的方式,这样您仍然可以使用下拉菜单等。

查看上面的示例和 doco 链接:

  1. 使用formatter列选项来制作下拉菜单
  2. data-url要从数据源加载,请参阅 doco 以获取示例格式
  3. 只需使用 html 定义 TH,使用这 2 个新选项来处理 tbody 内容



表选项

https://bootstrap-table.com/docs/api/table-options/


表#url

https://bootstrap-table.com/docs/api/table-options/#url

  • 属性:数据-url

  • 类型:字符串

  • 细节:

    从远程站点请求数据的 URL。

    请注意,根据是否指定“sidePagination”选项,所需的服务器响应格式有所不同。请参阅以下示例:

    • 没有服务器端分页
    • 使用服务器端分页
  • 默认值:未定义

  • 示例:来自 URL


table#rowStyle(用于设置所有正文列的样式)

https://bootstrap-table.com/docs/api/table-options/#rowstyle

  • 属性:数据行样式

  • 类型:函数

  • 细节:

    行样式格式化程序函数有两个参数:

    • row:行记录数据。
    • 索引:行索引。

    支持类或CSS。

  • 默认: {}

  • 示例:行样式


column#formatter(每列选项)

https://bootstrap-table.com/docs/api/column-options/#formatter

  • 属性:数据格式化程序

  • 类型:函数

  • 细节:

    上下文(this)是对象列。

    单元格格式化函数,采用三个参数:

    • value:字段值。
    • row:行记录数据。
    • 索引:行索引。
    • 字段:行字段。
  • 默认值:未定义


Yev*_*yev 5

1)在名为table的表格上填写html标签 data-url

2)当需要刷新时调用js函数 $('#teste').bootstrapTable('refresh')