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>");
        	});
        });
});
相关推荐
Aevget5 小时前
界面控件DevExpress JS & ASP.NET Core v25.1 - 全新的Stepper组件
javascript·asp.net·界面控件·devexpress·ui开发
kkk_皮蛋9 小时前
信令是什么?为什么 WebRTC 需要信令?
后端·asp.net·webrtc
何中应13 小时前
【面试题-8】Spring/Spring MVC/Spring Boot/Spring Cloud
java·spring boot·后端·spring·mvc·面试题
JPX-NO14 小时前
使用cargo-generate自定义创建项目模板
rust·mvc
JPX-NO14 小时前
Rust Rocket Web 应用项目结构详解(MVC 风格)
rust·mvc·rocket
聊询QQ:276998851 天前
CNN - BiLSTM - SelfAttention在Matlab中的多变量回归预测探索
mvc
2301_800256111 天前
第十一章中的函数解读(1)
后端·asp.net
木易 士心1 天前
MVC、MVP 与 MVVM:Android 架构演进之路
android·架构·mvc
武藤一雄1 天前
[.NET] 中 System.Collections.Generic命名空间详解
windows·微软·c#·asp.net·.net·.netcore
还是大剑师兰特2 天前
MVC和MVVM模式详解+对比
mvc·mvvm·大剑师