栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript > Ajax教程

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

Ajax教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

在上篇文章给大家介绍了JQuery通过AJAX从后台获取信息显示在表格上并支持行选中 ,现在,抽个时间他们处理了一下,这样就不需要每次写代码了,可以节省大量的时间,具体请看下文:

具体代码如下:

//获取数据并显示数据表格
function GetTableData(tableId,ChlickEvent) {
 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 = "";
   $.each(fileds, function (i, el) {
    if (fileds[i]) {
     tr += '' + formatJsonData(json[index][fileds[i]]) + '';
    }
   });
   tr += "";
   tbody += tr;
  });
  table.children('tbody').append(tbody);
  if (ChlickEvent) {//如果需要支持行选中事件
   table.children('tbody').addClass('sel');
   table.children('tbody.sel').children('tr').click(function (event) {
    $(this).siblings('tr').removeClass('active');//删除其他行的选中效果
    $(this).addClass('active');//增加选中效果
    ChlickEvent($(this).children('td:eq(0)').text());//新增点击事件
   });
  }
 }).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里。

支持点击事件。

用法:

ID 名称 简介

以上代码简单易懂,jquery通过AJAX从后台获取信息并显示在表格上的实现类就这样完成了,希望大家喜欢。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/128569.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号