jquery uses AJAX to obtain information from the background and display it on the form. The implementation class is written separately, so that programmers do not need to write code every time and can save a lot of time. Friends who are interested should take a look.
In the last article, I introduced you to JQuery using AJAX to obtain information from the background, display it on the table, and support row selection. Now, take some time to deal with it, so that you don’t need to write code every time. It can save a lot of time. Please see below for details:
The specific code is as follows:
//获取数据并显示数据表格 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 = "<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 (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; }
It is very simple to write and has few functions, but it is enough for my own use for the time being. Meet simple needs.
HTML code must be in the following format. JSON data must be obtained through POST. The acquisition address must be written in the data-url, and the data column name must be written in the data-field.
Support click events.
Usage:
<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', function (id) { alert(id) }); }); </script>
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
JQuery Ajax dynamically generates Table tables
A brief analysis of the use of Ajax in Asp.net MVC
Realizing mobile phone positioning based on h5 ajax
The above is the detailed content of jquery implementation class that obtains information from the background through AJAX and displays it on the table. For more information, please follow other related articles on the PHP Chinese website!