Jquery html<table>数据行的添加和删除
2014-01-17 12:16
573 查看
1.运行结果
2.需要引用的js文件
demo_page.css
demo_table.css
demo_table_jui.css
jquery-1.7.2.min.js
jquery.dataTables.min.js
3.页面源码之Html部分
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="UEP.SalesOrder.Web.SalesOrder.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="../Css/demo_page.css" rel="stylesheet" />
<link href="../Css/demo_table.css" rel="stylesheet" />
<link href="../Css/demo_table_jui.css" rel="stylesheet" />
<script src="../JavaScript/jquery-1.7.2.min.js"></script>
<script src="../JavaScript/jquery.dataTables.min.js"></script>
<title></title>
<style type="text/css">
#xb {
width: 110px;
}
.selected {
background:yellow;
color:red;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
姓名:<input id="xm"/>
性别:<select id="xb"><option value="男">男</option><option value="女">女</option></select>
年龄:<input id="age"/>
</div>
<div>
<br /><input type="button" value="添加新条目" onclick="addItem()" style="height: 41px; width: 124px;" />
<input type="button" value="删除选中条目" onclick="deleItem()" style="height: 40px; width: 130px;" />
<input type="button" value="清空表数据" onclick="clearTable()" style="height: 41px; width: 125px" /><br />
<br />
<table id="example">
<thead>
<tr>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
</tr>
</thead>
</table>
</div>
</form>
</body>
</html>
4.页面源码之Javascript部分
<script>
$(document).ready(function () {
$("#example").dataTable(
{
"bFilter": false,
"bSort": false,
"oLanguage": {
"sLengthMenu": "每页显示 _MENU_ 条记录",
"sZeroRecords": "",
"sInfo": "当前显示 _START_ 到 _END_ 条,共 _TOTAL_ 条记录",
"sInfoEmtpy": "找不到相关数据",
"sInfoFiltered": "数据表中共为 _MAX_ 条记录)",
"sProcessing": "正在加载中...",
"sSearch": "搜索",
"sUrl": "", //多语言配置文件,可将oLanguage的设置放在一个txt文件中,例:Javascript/datatable/dtCH.txt
"oPaginate": {
"sFirst": "第一页",
"sPrevious": "",
"sNext": "",
"sLast": " 最后一页 "
}
}, //多语言配置
"bLengthChange": false,
"bPaginate": false,
"bInfo": false
});
});
//添加一行
function addItem() {
$("#example").dataTable().fnAddData(
[$("#xm").val(), $("#xb").val(), $("#age").val()]
);
clickRow();//每添加一行则添加每行单击变色事件
}
//删除选中行
function deleItem() {
$("#example").dataTable().fnDeleteRow(clickRowIndex);//删除指定行号的行
}
//清空所有行
function clearTable() {
$("#example").dataTable().fnClearTable();
}
//添加每行单击变色事件
var clickRowIndex;
function clickRow() {
$("#example tbody tr").click(function (e) {
var rowIndex = $(this).context._DT_RowIndex;//获得单击行行号
clickRowIndex = rowIndex;
$(this).addClass("selected").siblings().removeClass("selected");
});
}
</script>
2.需要引用的js文件
demo_page.css
demo_table.css
demo_table_jui.css
jquery-1.7.2.min.js
jquery.dataTables.min.js
3.页面源码之Html部分
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="UEP.SalesOrder.Web.SalesOrder.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="../Css/demo_page.css" rel="stylesheet" />
<link href="../Css/demo_table.css" rel="stylesheet" />
<link href="../Css/demo_table_jui.css" rel="stylesheet" />
<script src="../JavaScript/jquery-1.7.2.min.js"></script>
<script src="../JavaScript/jquery.dataTables.min.js"></script>
<title></title>
<style type="text/css">
#xb {
width: 110px;
}
.selected {
background:yellow;
color:red;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
姓名:<input id="xm"/>
性别:<select id="xb"><option value="男">男</option><option value="女">女</option></select>
年龄:<input id="age"/>
</div>
<div>
<br /><input type="button" value="添加新条目" onclick="addItem()" style="height: 41px; width: 124px;" />
<input type="button" value="删除选中条目" onclick="deleItem()" style="height: 40px; width: 130px;" />
<input type="button" value="清空表数据" onclick="clearTable()" style="height: 41px; width: 125px" /><br />
<br />
<table id="example">
<thead>
<tr>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
</tr>
</thead>
</table>
</div>
</form>
</body>
</html>
4.页面源码之Javascript部分
<script>
$(document).ready(function () {
$("#example").dataTable(
{
"bFilter": false,
"bSort": false,
"oLanguage": {
"sLengthMenu": "每页显示 _MENU_ 条记录",
"sZeroRecords": "",
"sInfo": "当前显示 _START_ 到 _END_ 条,共 _TOTAL_ 条记录",
"sInfoEmtpy": "找不到相关数据",
"sInfoFiltered": "数据表中共为 _MAX_ 条记录)",
"sProcessing": "正在加载中...",
"sSearch": "搜索",
"sUrl": "", //多语言配置文件,可将oLanguage的设置放在一个txt文件中,例:Javascript/datatable/dtCH.txt
"oPaginate": {
"sFirst": "第一页",
"sPrevious": "",
"sNext": "",
"sLast": " 最后一页 "
}
}, //多语言配置
"bLengthChange": false,
"bPaginate": false,
"bInfo": false
});
});
//添加一行
function addItem() {
$("#example").dataTable().fnAddData(
[$("#xm").val(), $("#xb").val(), $("#age").val()]
);
clickRow();//每添加一行则添加每行单击变色事件
}
//删除选中行
function deleItem() {
$("#example").dataTable().fnDeleteRow(clickRowIndex);//删除指定行号的行
}
//清空所有行
function clearTable() {
$("#example").dataTable().fnClearTable();
}
//添加每行单击变色事件
var clickRowIndex;
function clickRow() {
$("#example tbody tr").click(function (e) {
var rowIndex = $(this).context._DT_RowIndex;//获得单击行行号
clickRowIndex = rowIndex;
$(this).addClass("selected").siblings().removeClass("selected");
});
}
</script>
相关文章推荐
- Html表格<table>还是需要添加一些标签进行优化,可以添加标题<caption>和摘要<table summary>
- S动态添加与删除select中的Option对象 2009年09月23日 15:26 <html> <head> <meta http-equiv="Content-Type" content="t
- HTML中如何为表格添加标题(添加的标题与表格相对静止)(<table><caption>标题</caption></table>)
- 用jquery删除table列表中<u>标签
- angular jQuery css html混合table表格查询 排序 添加 删除 隔行换色
- html <table>表数据转 Json 格式
- jquery通过<a>标签删除table中的一行
- jquery table表格添加删除数据(含批量删除,全选全不选和简单的表单验证)
- <java><JTable>使用AbstractTableModel实现更新、删除、插入数据
- jQuery给table添加行和删除行
- jquery操作html文档(table添加行,IE 中遇到的bug问题)
- JavaScript导出table数据到Excel<一>
- jQuery为table表格动态添加或删除tr
- HTML <table> 标签的 frame 属性
- HTML:添加<meta>标记说明内容的类型(文件的内容类型和编程用的字符类型)
- Jquery实现table行数的增加,删除,实现指定行数的添加等应用~~~~!!!!
- jquery操作html,table。点击按钮,逐行滚动显示,自动选中下一行数据
- jquery 添加插入元素技巧<前面和后面>
- jQuery DOM-获取、设置、添加、删除html文本的内容
- 利用jquery给指定的table动态添加一行、删除一行