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

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

function loadData() {

var c = '';

$('#import-table').DataTable({

ajax: {

url: '' + "../../../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": "r>t>",

initComplete: function() {}

});

}

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

function reloadTable(){

var code = $("#type option:selected").val();

$('#import-table').DataTable()

.ajax.url(

''+"../../../hot/getByCode.action?code="+ code

).load();

}

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

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

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

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐