Jquery creates tables, fills table data, and resets tables
Jquery creates a table
/** * 创建表格 * @param label 标题 * @param data 数据 * @param tableElement html元素,表格插入至此元素中 */function createTable(label, data, tableElement) { //创建表格 var table = $("<table> </table>"); //也可以为元素对象设定id,class等属性. /*var table = $("<table>",{ "id" : "tableId", "class" : "table_class" });*/ //设定样式 table.css({ width: "98%", "border-collapse": "collapse", border: "0px solid #d0d0d0", margin: "3px", "font-size": "14px" }); //标题行 var tr = $("<tr></tr>"); tr.css({ border: "1px solid #d0d0d0", height: "30px", color: "#FFF", background: "#37b5ad" }); $.each(label, function (index, value) { var th = $("<th>" + value + "</th>"); th.appendTo(tr); }); tr.appendTo(table); $.each(data, function (index, row) { //数据行 var tr = $("<tr></tr>"); //数据列 $.each(row, function (key, value) { //console.info(key + ":" + value); var td = $("<td>" + value + "</td>"); td.css({ border: "1px solid #d0d0d0", height: "24px" }); td.appendTo(tr); }); tr.appendTo(table); }); table.appendTo(tableElement); }
Attached: the data structure of label and data
//label.json['事项编码','事项名称']// data.json[{"code":"44530200","name":"办理《计划生育情况证明》"}, {"code":"44530200","name":"申请《再生育一胎子女审批》"}, {"code":"44530200","name":"办理《符合政策生育一孩登记》"}, {"code":"44530200","name":"办理《流动人口婚育证明》"}]
2. Jquery fills the table data
function fill_table_data() { //table var table = $("#tableId"); // 通过嵌套了table的元素id获取table对象 // 例如:<div id="contain_table_elementId"><table></table></div> //var table = $("#contain_table_elementId").find("table"); // row cell 从1开始 $("tr:nth-child(2) td:nth-child(2)", table).html('第2行第2列'); $("tr:nth-child(2) td:nth-child(3)", table).html('第2行第3列'); $("tr:nth-child(2) td:nth-child(4)", table).html('第2行第4列'); $("tr:nth-child(2) td:nth-child(5)", table).html('第2行第5列'); $("tr:nth-child(3) td:nth-child(2)", table).html('第3行第2列'); $("tr:nth-child(3) td:nth-child(3)", table).html('第3行第3列'); $("tr:nth-child(3) td:nth-child(4)", table).html('第3行第4列'); $("tr:nth-child(3) td:nth-child(5)", table).html('第3行第5列'); //第4行第5列不存在,你猜会发生什么? //$("tr:nth-child(4) td:nth-child(5)", table).html('第4行第5列'); }
The premise of filling the table data is: the html table row and column elements have been created.
For example: table.html
<table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr align="center" height="36" class="tr1"> <td class="td1">第1列</td> <td class="td1">第2列</td> <td class="td1">第3列</td> <td class="td1">第4列</td> <td class="td1">第5列</td> </tr> <tr align="center" height="36"> <td>第2行</td> <!-- td-第2行第2列已创建,你可以为其填充数据 --> <td></td> <td></td> <td ></td> <td class="td2" ></td> </tr> <tr align="center" height="36"> <td>第3行</td> <td ></td> <td ></td> <td ></td> <td class="td2"></td> </tr> </table>
3. Jquery adds (delete) table rows and columns
Mostly used for dynamic tables, that is, the data rows and rows of the table are not fixed, and ajax fills the data.
Note: Because the table is reset here, all rows except the first row (header row) are deleted and then the data rows are added.
//If the original table data is not deleted, new data rows will only be appended instead of overwritten.
function rest_table_data() { var table = $("#tableId"); //删除原有表格数据 table.find("tr").each(function(i){ if(i != 0){ //表头不删 this.remove(); } }); //添加行列数据 $.get('table_data.json', function (data) { // row cell 从1开始,因为明确知道数据是12行,所以i<12 for (var i = 0; i < 12; i++) { //数据行 var tr = $("<tr>", { align: "center", height: "36" }); //数据列 $.each(data, function (key, value) { var td = $("<td>" + value[i] + "</td>"); td.appendTo(tr); if (key == "column_4") { //这一列的数据,要指定样式 td.attr("class","td2"); } }); tr.appendTo(table); } }); }
Attached: table_data.json
//按列 {"column_1":["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"], "column_2":[1858,2120,3466,3513,3829,3035,2934,2761,2576,1635,0,0], "column_3":[0,0,1,46,86,69,102,82,118,61,0,0], "column_4":[0,0,0,39,44,59,101,81,101,57,0,0], "column_5":["0.0%","0.0%","0.0%","85.0%","51.0%","86.0%","99.0%","99.0%","86.0%","93.0%","0.0%","0.0%"]}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s
