JQuery实现table行折叠效果以JSON做数据源


Posted in Javascript onMay 26, 2014
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title></title> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script> 
<script type="text/javascript"> 
Array.prototype.filterRepeat = function () { 
var res = [], hash = {}; 
for (var i = 0, elem; (elem = this[i]) != null; i++) { 
if (!hash[elem]) { 
res.push(elem); 
hash[elem] = true; 
} 
} 
return res; 
} 
$(function () { 
var json = [ 
{ "SysName": "数据库", "SysGuid": "1", "CourseName": "SQL", "CourseGuid": "22", "Remarks": "大sb", "firstdate": "2013-1-1", "firstresult": "合格", "secdate": "2013-2-1", "secresult": "合格", "thirddate": "2013-3-1", "thirdresult": "合格" }, 
{ "SysName": "数据库", "SysGuid": "1", "CourseName": "MySQL", "CourseGuid": "23", "Remarks": "IQ太低", "firstdate": "2013-1-1", "firstresult": "不合格", "secdate": "2013-2-1", "secresult": "不合格", "thirddate": "2013-3-1", "thirdresult": "合格" }, 
{ "SysName": "数据库", "SysGuid": "1", "CourseName": "NoSQL", "CourseGuid": "24", "Remarks": "IQ太低", "firstdate": "2013-1-1", "firstresult": "不合格", "secdate": "2013-2-1", "secresult": "合格", "thirddate": "2013-3-1", "thirdresult": "合格" }, 
{ "SysName": "数据库", "SysGuid": "1", "CourseName": "Oracle", "CourseGuid": "25", "Remarks": "IQ太低", "firstdate": "2013-1-1", "firstresult": "不合格", "secdate": "2013-2-1", "secresult": "合格", "thirddate": "2013-3-1", "thirdresult": "合格" }, 
{ "SysName": "ASP.NET", "SysGuid": "2", "CourseName": "基础", "CourseGuid": "43", "Remarks": "IQ太低", "firstdate": "2013-1-1", "firstresult": "合格", "secdate": "2013-2-1", "secresult": "不合格", "thirddate": "2013-3-1", "thirdresult": "合格" }, 
{ "SysName": "ASP.NET", "SysGuid": "2", "CourseName": "高级", "CourseGuid": "44", "Remarks": "IQ太低", "firstdate": "2013-1-1", "firstresult": "合格", "secdate": "2013-2-1", "secresult": "不合格", "thirddate": "2013-4-1", "thirdresult": "合格" }, 
{ "SysName": "JavaScript", "SysGuid": "3", "CourseName": "基础", "CourseGuid": "54", "Remarks": "IQ太低", "firstdate": "2013-1-1", "firstresult": "合格", "secdate": "2013-2-1", "secresult": "合格", "thirddate": "2013-5-1", "thirdresult": "合格" }, 
{ "SysName": "JavaScript", "SysGuid": "3", "CourseName": "高级", "CourseGuid": "67", "Remarks": "IQ太低", "firstdate": "2013-1-1", "firstresult": "不合格", "secdate": "2013-2-1", "secresult": "合格", "thirddate": "2013-6-1", "thirdresult": "合格" }, 
]; 
createTable(json); 
$("#btnsave").click(function () { 
$("#ta").text(setDataXML()); 
}); 
}); 
function createTable(json) { 
var tb = $("#tb"); 
var sys = new Array; 
for (var i = 0; i < json.length; i++) { 
sys.push(json[i].SysName); 
} 
//过滤重复 
sys = sys.filterRepeat(); 
var tr = null; 
for (var j = 0 ; j < sys.length; j++) { 
tr += "<tr style='text-align: left' class=gridborder id=p" + j + "><td colspan=8>[-]" + sys[j] + "</td></tr>"; 
for (var i = 0; i < json.length; i++) { 
if (json[i].SysName == sys[j]) { 
tr += "<tr parent=p" + j + " style='text-align: center' pguid='" + json[i].SysGuid + "' cguid='" + json[i].CourseGuid + "'><td>" + json[i].CourseName + "</td><td>" + setDate(json[i].firstdate) + "</td><td>" + setSelect(json[i].firstresult) + "</td><td>" + setDate(json[i].secdate) + "</td><td>" + setSelect(json[i].secresult) + "</td><td>" + setDate(json[i].thirddate) + "</td><td>" + setSelect(json[i].thirdresult) + "</td><td>" + setInput(json[i].Remarks) + "</td></tr>" 
} 
} 
} 
tb.append(tr); 
//设置行点击事件 
$("tr.gridborder").css("cursor", "pointer") 
.toggle(function () { 
var txt = $(this).children().text(); 
$(this).children().text(txt.replace("-", "+")); 
}, function () { 
var txt = $(this).children().text(); 
$(this).children().text(txt.replace("+", "-")); 
}).click(function () { 
var id = $(this).attr("id"); 
$(this).siblings("tr[parent='" + id + "']").toggle(); 
}); 
//设置选中变色 
$("tr[parent^=p]").toggle(function () { 
$(this).attr('bgcolor', '#E3e4e5'); 
}, function () { 
$(this).attr('bgcolor', '#ffffff'); 
}); 
} 
function setSelect(obj) { 
return "<select style='width:96%'><option value ='" + obj + "'>" + obj + "</option ><option value='合格'>合格</option><option value='不合格'>不合格</option></select>"; 
} 
function setDate(obj) { 
return "<input style='width:96%' type='text' value='" + obj + "' />"; 
} 
function setInput(obj) { 
return "<input style='width:96%' type='text' value='" + obj + "' />"; 
} 
function setDataXML() { 
var dataxml = $("<DataXML></DataXML>"); 
$("tr[parent^=p]").each(function () { 
var item = $("<Course/>"); 
var sysguid = $(this).attr("pguid"); 
var cguid = $(this).attr("cguid"); 
var fdate = $(this).children().children().eq(0).val(); 
var fresult = $(this).children().children().eq(1).val(); 
var sdate = $(this).children().children().eq(2).val(); 
var sresult = $(this).children().children().eq(3).val(); 
var tdate = $(this).children().children().eq(4).val(); 
var tresult = $(this).children().children().eq(5).val(); 
var remark = $(this).children().children().eq(6).val(); 
item.attr("SysGuid", sysguid).attr("Remarks", remark).attr("CourseGUID", cguid) 
.attr("FirstDate", fdate).attr("FirstResult", fresult) 
.attr("SecDate", sdate).attr("SecResult", sresult) 
.attr("ThirdDate", tdate).attr("ThirdResult", tresult); 
dataxml.append(item); 
}); 
return dataxml[0].outerHTML; 
} 
</script> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
<table id="tb" border="1" style="border-collapse: collapse" width="100%"> 
<tbody> 
<tr style="text-align: center"> 
<td style="width: 100px">课程名称</td> 
<td style="width: 120px">初考时间</td> 
<td style="width: 120px">初考成绩</td> 
<td style="width: 120px">次考时间</td> 
<td style="width: 120px">次考成绩</td> 
<td style="width: 120px">清考时间</td> 
<td style="width: 120px">清考成绩</td> 
<td style="width: 250px">备注</td> 
</tr> 
</tbody> 
</table> 
</div> 
<input id="btnsave" type="button" value="保存" /> 
<textarea id="ta" cols="100" rows="20" ></textarea> 
</form> 
</body> 
</html>
Javascript 相关文章推荐
动态添加js事件实现代码
Mar 12 Javascript
解决jquery .ajax 在IE下卡死问题的解决方法
Oct 26 Javascript
Js 时间间隔计算的函数(间隔天数)
Nov 15 Javascript
JavaScript 学习笔记之数据类型
Jan 14 Javascript
js计算任意值之间随机数的方法
Jan 16 Javascript
浅谈JS中的bind方法与函数柯里化
Aug 10 Javascript
JS实现touch 点击滑动轮播实例代码
Jan 19 Javascript
基于vue2.0实现的级联选择器
Jun 09 Javascript
爬虫利器Puppeteer实战
Jan 09 Javascript
vue登录以及权限验证相关的实现
Oct 25 Javascript
vue-resourc发起异步请求的方法
Feb 11 Javascript
AudioContext 实现音频可视化(web技术分享)
Feb 24 Javascript
jquery attr方法获取input的checked属性问题
May 26 #Javascript
jQuery表格排序组件-tablesorter使用示例
May 26 #Javascript
jQuery实现鼠标可拖动调整表格列宽度
May 26 #Javascript
js处理表格对table进行修饰
May 26 #Javascript
利用jQuery实现可以编辑的表格
May 26 #Javascript
js实现简单的购物车有图有代码
May 26 #Javascript
js函数参数设置默认值的一种变通实现方法
May 26 #Javascript
You might like
Ajax+PHP快速上手及简单应用说明
2013/07/24 PHP
php利用单例模式实现日志处理类库
2014/02/10 PHP
php获取本机真实IP地址实例代码
2016/03/31 PHP
CI框架无限级分类+递归的实现代码
2016/11/01 PHP
php 三元运算符实例详细介绍
2016/12/15 PHP
javascript中的对象和数组的应用技巧
2007/01/07 Javascript
textarea的value是html文件源代码,存成html文件的代码
2007/04/20 Javascript
javascript 时间比较实现代码
2009/10/28 Javascript
超级酷和最实用的jQuery实例收集(20个)
2010/04/21 Javascript
JavaScript常用对象的方法和属性小结
2012/01/24 Javascript
基于jquery实现状态限定编辑的代码
2012/02/11 Javascript
使用js解决由border属性引起的div宽度问题
2013/11/26 Javascript
JavaScript实现动态删除列表框值的方法
2015/08/12 Javascript
jQuery.Callbacks()回调函数队列用法详解
2016/06/14 Javascript
深入理解Node.js 事件循环和回调函数
2016/11/02 Javascript
在js代码拼接dom对象到页面上去的模板总结(必看)
2017/02/14 Javascript
详解Vue 动态添加模板的几种方法
2017/04/25 Javascript
5 种JavaScript编码规范
2018/01/30 Javascript
Node.js中DNS模块学习总结
2018/02/28 Javascript
vue服务端渲染添加缓存的方法
2018/09/18 Javascript
vue 左滑删除功能的示例代码
2019/01/28 Javascript
angular使用md5,CryptoJS des加密的方法
2019/06/03 Javascript
利用Python脚本实现ping百度和google的方法
2017/01/24 Python
python获取指定字符串中重复模式最高的字符串方法
2018/06/29 Python
python调用百度地图WEB服务API获取地点对应坐标值
2019/01/16 Python
python挖矿算力测试程序详解
2019/07/03 Python
django项目中使用手机号登录的实例代码
2019/08/15 Python
用pandas划分数据集实现训练集和测试集
2020/07/20 Python
Troy-Bilt官网:草坪割草机、吹雪机、分蘖机等
2019/02/19 全球购物
某公司C#程序员面试题笔试题
2014/05/26 面试题
军训自我鉴定
2013/12/14 职场文书
幼儿园教师考核制度
2014/02/01 职场文书
爱岗敬业事迹材料
2014/12/24 职场文书
杨善洲电影观后感
2015/06/04 职场文书
祝福语集锦:给百岁老人祝寿贺词
2019/11/19 职场文书
Javascript 解构赋值详情
2021/11/17 Javascript