星空网 > 软件开发 > Java

jquery通过AJAX从后台获取信息并显示在表格上的类

前一阵我写了:《jquery通过AJAX从后台获取信息并显示在表格上,并支持行选中。》现在,我把他们处理了一下,不需要每次写代码了:


具体代码如下:

//获取数据并显示数据表格function GetTableData(tableId,isClickEvent) {  var table = $(tableId);  var url=table.data('url');  $.ajax({    url: url,    type: 'post',    dataType: 'json',  })  .done(function (json) {    var fileds = new Array();    table.children('thead').children('tr').children('th').each(function (index, el) {      var field = $(this).data('field');      fileds[index] = field;    });    var tbody = '';    $.each(json, function (index, el) {      var tr = "<tr>";      $.each(fileds, function (i, el) {        if (fileds[i]) {          tr += '<td>' + formatJsonData(json[index][fileds[i]]) + '</td>';        }      });      tr += "</tr>";      tbody += tr;    });    table.children('tbody').append(tbody);    if (isClickEvent) {//如果需要支持行选中事件则增加事件      table.children('tbody').addClass('sel');      table.children('tbody.sel').children('tr').click(function (event) {        $(this).siblings('tr').removeClass('active');//删除其他行的选中效果        $(this).addClass('active');//增加选中效果      });    }  }).fail(function () {    alert("Err");  });}//格式化JSON数据,比如日期function formatJsonData(jsondata){  if(jsondata==null){    return '无数据';  }  else if(/\/Date\(\d+\)/.exec(jsondata)){    var date = new Date(parseInt(jsondata.replace("/Date(", "").replace(")/", ""), 10));    return date.toLocaleString();  }  return jsondata;}

写的非常简单,功能也很少,但是我自己用暂时足够了。满足简单需求。

HTML代码必须以下格式,必须以POST方式获取JSON数据,获取地址写到data-url里,数据列名写到data-field里。


 

用法:

<table id="RoleGroupTable" class="table" data-url="@Url.Action("GetRoleGroups", "Account")"> <thead>  <tr>   <th data-field="ID">ID</th>   <th data-field="Name">名称</th>   <th data-field="Remark">简介</th>  </tr> </thead> <tbody></tbody></table><script> jQuery(document).ready(function ($) {  GetTableData('#RoleGroupTable',true); });</script>

 




原标题:jquery通过AJAX从后台获取信息并显示在表格上的类

关键词:jquery

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

美国零售商业绩持续疲弱,破产风险加剧!:https://www.kjdsnews.com/a/1701360.html
2024年拉丁美洲的下一件大事:https://www.kjdsnews.com/a/1701361.html
谷歌再曝大裁员!3万名员工或将被AI取代:https://www.kjdsnews.com/a/1701362.html
重磅!鲁班跨境通「2024年出海营销日历」发布,速览全球热点,戳文领取!:https://www.kjdsnews.com/a/1701363.html
亚马逊印度站下调Prime会员费,一大波买家正在靠近:https://www.kjdsnews.com/a/1701364.html
速卖通巴西接入Google Pay支付方式!:https://www.kjdsnews.com/a/1701365.html
黄果树瀑布景区景点 - 黄果树瀑布景区景点分布图:https://www.vstour.cn/a/408258.html
延边酒店(附近旅馆住宿50元):https://www.vstour.cn/a/409226.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流