当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JQuery Ajax动态加载Table数据的实例讲解

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

2018年08月19日  | 移动技术网IT编程  | 我要评论

我们在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数据的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网