星空网 > 软件开发 > Java

Jquery通过AJAX从后台获取数据显示在表格上(复选)

代码:

function GetMultiLineSelectTable(tableId, selectIds) {  var table = $(tableId);  var url = table.data('url');  var ischeckbox = false;  //获取数据项名称  var fileds = new Array();  table.children('thead').children('tr').children('th').each(function (index, el) {    var type = 'Content';    if ($(this).data('type')) type = $(this).data('type');    if (type == 'Content') {      var field = $(this).data('field');      fileds[index] = field;    }    else if (type == 'CheckBox') {      ischeckbox = true;    }  });  $.ajax({    url: url,    type: 'post',    dataType: 'json',  })  .done(function (json) {    //向表格内新增内容    var tbody = '';    $.each(json, function (index, el) {      var tr = "<tr>";      if (ischeckbox) {//生成复选按钮        //tr+='<td><div ><span><input type="checkbox"></span></div></td>'        tr += '<td><input type="checkbox"></td>'      }      $.each(fileds, function (i, el) {//生成内容        if (fileds[i]) {          tr += '<td>' + formatJsonData(json[index][fileds[i]]) + '</td>';        }      });      tr += "</tr>";      tbody += tr;    });    table.children('tbody').empty();    table.children('tbody').append(tbody);//显示数据    if (selectIds) {//将需要选中的行设为选中状态      selectIds.each(function (index, el) {        //建设中      });    }    table.children('tbody').addClass('sel');    table.children('tbody.sel').children('tr').click(function (event) {//点击行事件      $(this).toggleClass('active');//增加选中效果      if (ischeckbox) $(this).find('input[type="checkbox"]').attr('checked', $(this).hasClass('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;}

用法:

<table class="table" id="GroupTable" data-url="@Url.Action("GetRoleGroups", "Account")"> <thead>  <tr>   <th data-type="CheckBox"></th>   <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 ($) {  GetMultiLineSelectTable("#GroupTable"); });</script>

 

<th data-type="CheckBox"></th>代表需要显示复选框,不想要复选框直接删掉就行了


获取选中的行ID:

function GetGroupTableSelectIds(){  var selects=$('#GroupTable').children('tbody').children('tr.active');  var ids=new Array();  selects.each(function(index, el) {    ids[index]=el.cells[1].innerHTML;  });  return ids;}

 




原标题:Jquery通过AJAX从后台获取数据显示在表格上(复选)

关键词:jquery

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

Etsy 2024年重大更新:https://www.kjdsnews.com/a/1726964.html
亚马逊与TEMU资质上传指引:宠物食品美国FDA注册详情:https://www.kjdsnews.com/a/1726965.html
Temu打响开年第一枪!“半托管”降生,变化何在?机会何在?:https://www.kjdsnews.com/a/1726966.html
TikTok与HubSpot合作助力墨西哥B2B企业社交媒体营销:https://www.kjdsnews.com/a/1726967.html
联邦快递承诺持续深耕中国市场;微软游戏将裁员1900人 | 跨境电商日报:https://www.kjdsnews.com/a/1726968.html
季节性产品该怎么推广?:https://www.kjdsnews.com/a/1726969.html
为什么快递行业一直在打价格战?:https://www.kjdsnews.com/a/1842068.html
为什么快递行业一直在打价格战?:https://www.xlkjsw.com/news/90212.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流