JQuery Ajax动态加载Table数据的实例讲解

时间:2021-05-26

我们在jsp定义一个select和一个table,要求实现根据select的选值,动态加载table数据。

<select id="type" name="type" onchange="reloadTable(this)"></select><table id="import-table" class="table table-striped table-bordered table-hover" width="100%"></table>

table第一次加载数据的function定义如下:

function loadData() { var c = '<label><input type="checkbox" id="checkbox1" class="ace" onchange="javascrpt:selectAll(this);"/><span class="lbl"></span></label>'; $('#import-table').DataTable({ ajax: { url: '<%=request.getContextPath()%>' + "../../../hot/getByCode.action?code=APP", type: "post", dataType: "json", data: {} }, "scrollCollapse": true, ordering: false, visible: true, api: true, serverSide: true, columns: [{ "data": "id", "class": "center", "width": "80px", "name": "importId", orderable: false, "title": c, "render": function(a, b, c, d) { return getColumnReturnStr("checkbox", c.id, "importId") } }, { "data": "name", "title": "名称" }, ], "dom": "<'row'<'col-sm-6'l><'col-sm-6'f>r>t<'row'<'col-sm-6'i><'col-sm-6'p>>", initComplete: function() {} });}

接着需要考虑,如何在select选值改变的时候,更新table中ajax的url地址,实现table的reload

function reloadTable(){ var code = $("#type option:selected").val(); $('#import-table').DataTable() .ajax.url( '<%=request.getContextPath()%>'+"../../../hot/getByCode.action?code="+ code ).load();}

这样我们便可以通过改变select选值,动态加载table数据。

通过$(‘#import-table').DataTable().ajax.url().load();方法实现。

以上这篇JQuery Ajax动态加载Table数据的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章