
1、首先输入下方的代码:
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>利用jquery给指定的table添加一行、删除一行</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript"
src="<%=request.getContextPath()%>/js/jquery-1.5.1.js"></script>
<script type="text/javascript">
2、然后在输入下方的代码:
////////添加一行、删除一行封装方法///////
/**
* 为table指定行添加一行
*
* tab 表id
* row 行数,如:0->第一行 1->第二行 -2->倒数第二行 -1->最后一行
* trHtml 添加行的html代码
*
*/
function addTr(tab, row, trHtml){
//获取table最后一行 $("#tab tr:last")
//获取table第一行 $("#tab tr").eq(0)
//获取table倒数第二行 $("#tab tr").eq(-2)
var $tr=$("#"+tab+" tr").eq(row)
if($tr.size()==0){
alert("指定的table id或行数不存在!")
return
}
$tr.after(trHtml)
}
3、然后在输入下方的代码:
function delTr(ckb){
//获取选中的复选框,然后循环遍历删除
var ckbs=$("input[name="+ckb+"]:checked")
if(ckbs.size()==0){
alert("要删除指定行,需选中要删除的行!")
return
}
ckbs.each(function(){
$(this).parent().parent().remove()
})
}
/**
* 全选
*
* allCkb 全选复选框的id
* items 复选框的name
*/
function allCheck(allCkb, items){
$("#"+allCkb).click(function(){
$('[name='+items+']:checkbox').attr("checked", this.checked )
})
}
////////添加一行、删除一行测试方法///////
$(function(){
//全选
allCheck("allCkb", "ckb")
})
function addTr2(tab, row){
var trHtml="<tr align='center'><td width='30%'><input type='checkbox' name='ckb'/>
</td><td width='30%'>地理</td><td width='30%'>60</td></tr>"
addTr(tab, row, trHtml)
}
function delTr2(){
delTr('ckb')
}
4、然后输入下方的代码:
</script>
</head>
<body>
<table border="1px #ooo" id="tab" cellpadding="0"
cellspacing="0" width="30%">
<tr align="center">
<td width="30%"><input id="allCkb" type="checkbox"/></td>
<td width="30%">科目</td>
<td width="30%">成绩</td>
</tr>
<tr align="center">
<td width="30%"></td>
<td width="30%">语文</td>
<td width="30%">80</td>
</tr>
</table>
<input type="button" onclick="addTr2('tab', -1)" value="添加">
<input type="button" onclick="delTr2()" value="删除">
</body>
</html>
5、然后这样就完成了。
========== 方法一DataTable tblDatas = newDataTable("Datas")
DataColumn dc = null
//赋值给dc,是便于对每一个datacolumn的 *** 作
dc =tblDatas.Columns.Add("ID",Type.GetType("System.Int32"))
dc.AutoIncrement= true//自动增加
dc.AutoIncrementSeed = 1//起始为1
dc.AutoIncrementStep = 1//步长为1
dc.AllowDBNull = false//
dc = tblDatas.Columns.Add("Product",Type.GetType("System.String"))
dc = tblDatas.Columns.Add("Version",Type.GetType("System.String"))
dc = tblDatas.Columns.Add("Description",Type.GetType("System.String"))
DataRow newRow
newRow = tblDatas.NewRow()
newRow["Product"] = "大话西游"
newRow["Version"] = "2.0"
newRow["Description"] = "我很喜欢"
tblDatas.Rows.Add(newRow)
newRow = tblDatas.NewRow()
newRow["Product"] = "梦幻西游"
newRow["Version"] = "3.0"
newRow["Description"] = "比大话更幼稚"
tblDatas.Rows.Add(newRow)
========== 方法二
DataTable tblDatas = newDataTable("Datas")
tblDatas.Columns.Add("ID", Type.GetType("System.Int32"))
tblDatas.Columns[0].AutoIncrement = true
tblDatas.Columns[0].AutoIncrementSeed = 1
tblDatas.Columns[0].AutoIncrementStep = 1
tblDatas.Columns.Add("Product",Type.GetType("System.String"))
tblDatas.Columns.Add("Version",Type.GetType("System.String"))
tblDatas.Columns.Add("Description",Type.GetType("System.String"))
tblDatas.Rows.Add(newobject[]{null,"a","b","c"})
tblDatas.Rows.Add(newobject[] { null, "a", "b", "c" })
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" })
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" })
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" })
========== 方法三
DataTable table = new DataTable ()
//创建table的第一列
DataColumn priceColumn = new DataColumn()
//该列的数据类型
priceColumn.DataType = System.Type.GetType("System.Decimal")
//该列得名称
priceColumn.ColumnName = "price"
//该列得默认值
priceColumn.DefaultValue =50
// 创建table的第二列
DataColumn taxColumn = new DataColumn()
taxColumn.DataType = System.Type.GetType("System.Decimal")
//列名
taxColumn.ColumnName = "tax"
//设置该列得表达式,用于计算列中的值或创建聚合列
taxColumn.expression_r_r = "price *0.0862"
// Create third column.
DataColumn totalColumn = new DataColumn()
totalColumn.DataType = System.Type.GetType("System.Decimal")
totalColumn.ColumnName = "total"
//该列的表达式,值是得到的是第一列和第二列值得和
totalColumn.expression_r_r = "price + tax"
// 将所有的列添加到table上
table.Columns.Add(priceColumn)
table.Columns.Add(taxColumn)
table.Columns.Add(totalColumn)
//创建一行
DataRow row = table.NewRow()
//将此行添加到table中
table.Rows.Add(row)
//将table放在试图中
DataViewview = new DataView(table)
dg.DataSource = view
dg.DataBind()
JSP页面中用c标签遍历list,要显示数据的对象列表放到list中。java代码:
1
request.setAttribute("list", yourList)
jsp代码:
<c:forEach var="user" items="${list}"> User Name: ${user.username}Age: ${user.age}</c:forEach>
注意在JSP中引入jstl的core标签,
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)