jquery implementation class that obtains information from the background through AJAX and displays it on the table

亚连
Release: 2018-05-24 17:34:19
Original
2226 people have browsed it

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 = ""; $.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; }
Copy after login

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:

ID 名称 简介
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!