JQuery异步加载表格选择记录

JQuery异步加载表格选择记录

JQuery操作表格

首先在页面中定义一个表格对象

html 复制代码
<table id="insts" class="table">
    <thead>
        <tr>
            <th>列1</th>
            <th>列2</th>
            <th>例3</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

其中<tbody>中数据为空,当加载数据时,需要往里填充数据。

另外在页面中加入下面标签用以触发响应。

html 复制代码
<button id="btnSearch">查找</button>

在页面初始化的处理函数中加入下面代码用以响应。

javascript 复制代码
<script type="text/javascript">
$(function () {
	$("#btnSearch").button().on("click", function(){
});

JQuery操作表格的方法

  • 清空表格内的数据
javascript 复制代码
$("#insts tbody").empty();
  • 加载表格数据
javascript 复制代码
$("#insts tbody").append("<tr><td>" + details + "</td><td>" + item["assLangName"] + "</td><td>" + sel + "</td></tr>");

其中details, item["assLangName"]sel是要填充的数据。

异步加载

异步加载使用JQuery提供的getJSON方法,利用JSON对象可以方便的填充数据。其中item["assLangName"]JSON数组中某一子项中的assLangName成员。

假设从服务端传过来的成员对象如下

javascript 复制代码
var instLink = 远程链接地址;
                $.getJSON(instLink,
                    function (data) {
                        $.each(data, function (i, item) {
                            var details = 数据1;
                            var sel = 数据2;
                            $("#insts tbody").append("<tr><td>" + details + "</td><td>" + item["assLangName"] + "</td><td>" + sel + "</td></tr>");
                        });
                    });

然后将上述代码加入到$("#btnSearch")的单击响应函数中。

javascript 复制代码
<script type="text/javascript">
$(function () {
	$("#btnSearch").button().on("click", function(){
		var instLink = 远程链接地址;
        $.getJSON(instLink,
        	function (data) {
        		$.each(data, function (i, item) {
        			var details = 数据1;
        			var sel = 数据2;
        			$("#insts tbody").append("<tr><td>" + details + "</td><td>" + item["assLangName"] + "</td><td>" + sel + "</td></tr>");
        	});
        });
});
相关推荐
刀法如飞4 天前
一款Go语言Gin框架MVC脚手架,满足大部分场景
go·mvc·gin
莫寒清5 天前
Spring MVC:@PathVariable 注解详解
java·spring·mvc
开开心心_Every6 天前
音频格式互转工具,支持Mp3ApeWavFlac互转
linux·运维·服务器·typescript·edge·pdf·asp.net
莫寒清8 天前
Spring MVC:@RequestParam 注解详解
java·spring·mvc
莫寒清8 天前
Spring MVC:MultipartFile 详解
java·spring·mvc
*.✧屠苏隐遥(ノ◕ヮ◕)ノ*.✧8 天前
JSP, MVC, El, JSTL, MAC
java·开发语言·mvc·mac·jsp
fchampion9 天前
最终一致性
java·spring·rabbitmq·github·mvc
bugcome_com9 天前
【C# 数组详解】Array 定义、初始化、遍历、内存原理与面试高频问题
后端·c#·asp.net
好学且牛逼的马12 天前
从“Servlet汪洋”到“响应式时代”:Spring MVC 1.x到5.x演进全记录与核心知识点详解
spring·servlet·mvc