兒這個小分享,道哥給大家分享下js中怎么動態(tài)輸出一個表格到頁面上,這里面呢就涉及到兩個大的知識點 一個是document.wiite一個是for循環(huán)。請看下例
<script type="text/javascript"> //打印一個表格 在外層循環(huán)呢先把table標簽打印出來 document.write('<table border="1" style="width:500px;">') //下面用for循環(huán)開始動態(tài)的逐一打印tr和td //第一層for循環(huán)循環(huán)行 tr //打印一個十行十列的表格 //并且根據(jù)奇偶行和列設置不同的背景顏色 for(var j=0;j<10;j++) { if(j%2==0) { document.write("<tr>") }else { document.write("<tr style='background:green'>") } //內層循環(huán)循環(huán)列 td for(var i=0;i<10;i++) { //判斷列的奇偶性 if(i%2==0) { document.write("<td style='background:gray'>"+i+"</td>") }else { document.write("<td>"+i+"</td>") } } document.write("</tr>") } //表格的結尾 document.write("</table>") </script>
實現(xiàn)結果如下
《大數(shù)據(jù)和人工智能交流》頭條號向廣大初學者新增C 、Java 、Python 、Scala、javascript 等目前流行的計算機、大數(shù)據(jù)編程語言,希望大家以后關注本頭條號更多的內容。
一、使用for循環(huán)和CSS實現(xiàn)下列的表格效果
<style>
.c_red{
background:red;
}
.c_blue{
background:blue;
}
table{
width:300px;
border:1px solid #ccc;
}
td{
border:1px solid #ccc;
}
</style>
<table>
<script>
for(var i=1 ; i <=9 ; i++){
if(i%2==0){
document.write("<tr class='c_red'><td> </td></tr>");
} else{
document.write("<tr class='c_blue'><td> </td></tr>");
}
}
</script>
</table>
二、使用javascript和for循環(huán)、CSS實現(xiàn)九九乘法表
<style>
.c_red{
background:red;
}
.c_blue{
background:blue;
}
table{
width:600px;
border:0px solid #ccc;
}
td{
border:1px solid #ccc;
}
</style>
<table>
<script>
for(var i=1 ; i <=9 ; i++){
if(i%2==0){
document.write("<tr class='c_red'>");
for(var j=1 ; j <=i ; j++)
{
document.write("<td>"+j+"x"+i+"="+(i*j)+"</td>");
}
document.write("</tr>");
} else{
document.write("<tr class='c_blue'>");
for(var j=1 ; j <=i ; j++)
{
document.write("<td>"+j+"x"+i+"="+(i*j)+"</td>");
}
document.write("</tr>");
}
}
</script>
</table>
《大數(shù)據(jù)和人工智能交流》的宗旨
1、將大數(shù)據(jù)和人工智能的專業(yè)數(shù)學:概率數(shù)理統(tǒng)計、線性代數(shù)、決策論、優(yōu)化論、博弈論等數(shù)學模型變得通俗易懂。
2、將大數(shù)據(jù)和人工智能的專業(yè)涉及到的數(shù)據(jù)結構和算法:分類、聚類 、回歸算法、概率等算法變得通俗易懂。
3、最新的高科技動態(tài):數(shù)據(jù)采集方面的智能傳感器技術;醫(yī)療大數(shù)據(jù)智能決策分析;物聯(lián)網智慧城市等等。
根據(jù)初學者需要會有C語言、Java語言、Python語言、Scala函數(shù)式等目前主流計算機語言。
根據(jù)讀者的需要有和人工智能相關的計算機科學與技術、電子技術、芯片技術等基礎學科通俗易懂的文章。
段時間因工作需求,特地學習了下sp_send_dbmail的使用,好不容易完工來和大家分享一下,不談理論,只管實踐!
如下是實際需求:
--=============================================
-- Title: 集團資質一覽表
-- Description1:<1、距離到期日期1年內和已過期的發(fā)到期提醒>
-- Description2:<2、表頭【非附件】:公司名稱、發(fā)證部門、證書名稱、類別、等級、到期日期、預警級別>
-- Description3:<3、預警級別:假設距離到期日期月數(shù)為N。一級:N<=3;二級:3<N<=6;三級:N>=6>
-- Description4:<4、提醒人員:郵件提醒
--=============================================
在這里sp_send_dbmail的參數(shù)不去做詳述(我也不懂~),實際過程中我們需要用到的并不多,只需下面幾行就能發(fā)送html格式的郵件了
Exec dbo.sp_send_dbmail
@profile_name='crm***', --發(fā)件人姓名
@recipients='156240***@qq.com', --郵箱(多個用;隔開)
@body=@tableHTML, --消息主體
@body_format='HTML', --指定消息的格式,一般文本直接去掉即可,發(fā)送html格式的內容需加上
@subject='資質到期預警'; -- 消息的主題
下面最主要的部分就是@tableHTML了,在這里我們使用兩種方式去拼接html。
1.通過sql CAST 函數(shù),網上的示例大多數(shù)是這種,愚笨的我不太看得懂,只能依瓢。
declare @tableHTML varchar(max)
SET @tableHTML=N'<H1 style="text-align:center">資質相關信息</H1>' +
N'<table border="1" cellpadding="3" cellspacing="0" align="center">' +
N'<tr><th width=100px" >公司名稱</th>'+
N'<th width=250px>發(fā)證部門</th><th width=150px>證書名稱</th>'+
N'<th width=50px>類別</th><th width=50px>等級</th>'+
N'<th width=60px>到期日期</th><th width=60px>預警級別</th></tr>'+
CAST ( (
select td=p.CompanyName, '',td=p.DeptName, '',td=p.Name,'', td=p.QualificationType, '',td=p.Level, '',td=p.ExpireDates, '',td=p.YJ,''
from(
select
CompanyName,DeptName,Name,QualificationType,Level,Convert(varchar(50),ExpireDate,111)ExpireDates,
case when DATEDIFF(mm,getDate(),ExpireDate)<=3 then '一級預警'
when DATEDIFF(mm,getDate(),ExpireDate)<=6 then '二級預警' else '三級預警'end YJ
from T_Market***_JTZZ
where 12>=DATEDIFF(mm,getDate(),ExpireDate)
) p order by p.ExpireDates asc
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>' ;
Exec dbo.sp_send_dbmail
@profile_name='crm***',
@recipients='156240***@qq.com',
@subject='資質到期預警',
@body=@tableHTML,
@body_format='HTML' ;
2.通過游標動態(tài)繪制html,感覺這種更方便,雖然寫起來有點啰嗦,但很靈活。
BEGIN
declare @tableHTML varchar(max)
declare @Companyname varchar(250) --公司名稱
declare @Deptname varchar(250) --發(fā)證部門
declare @Certname varchar(250) --證書名稱
declare @Certtype varchar(50) --證書類別
declare @Certlevel varchar(50) --證書等級
declare @Expirdate varchar(20) --到期時間
declare @Warnlevel varchar(20) --預警級別
begin
set @tableHTML='<html><body><table><tr><td><p><font color="#000080" size="3" face="Verdana">您好!</font></p><p style="margin-left:30px;"><font size="3" face="Verdana">以下資質即將到期或已過期,請盡快辦理資質延續(xù):</font></p></td></tr>';
--創(chuàng)建臨時表#tbl_result
create table #tbl_result(companyname varchar(250),deptname varchar(250),certname varchar(250),certtype varchar(50),certlevel varchar(50),expirdate varchar(20),warnlevel varchar(10));
insert into #tbl_result
select CompanyName,DeptName,Name,QualificationType,Level,convert(varchar(20),ExpireDate,23) ExpireDate,case when ms<=3 then '一級' when ms>3 and ms<=6 then '二級' else '三級' end warnlevel
from (
select *,Datediff(MONTH,GETDATE(),ExpireDate) ms
from T_Market***_JTZZ
where ExpireDate is not null and Datediff(MONTH,GETDATE(),ExpireDate)<=12
) res;
declare @counts int;
select @counts=count(*) from #tbl_result;
--- 提醒列表
if(@counts>0)
begin
set @tableHTML=@tableHTML+'<tr><td><table border="1" style="border:1px solid #d5d5d5;border-collapse:collapse;border-spacing:0;margin-left:30px;margin-top:20px;"><tr style="height:25px;background-color: rgb(219, 240, 251);"><th style="width:100px;">公司名稱</th><th style="width:200px;">發(fā)證部門</th><th>證書名稱</th><th style="width:60px;">類別</th><th style="width:80px;">等級</th><th style="width:100px;">到期日期</th><th style="width:80px;">預警級別</th></tr>';
--申明游標
Declare cur_cert Cursor for
select companyname,deptname,certname,certtype,certlevel,expirdate,warnlevel from #tbl_result order by expirdate;
--打開游標
open cur_cert
--循環(huán)并提取記錄
Fetch Next From cur_cert Into @Companyname,@Deptname,@Certname,@Certtype,@Certlevel,@Expirdate,@Warnlevel
While (@@Fetch_Status=0)
begin
set @tableHTML=@tableHTML + '<tr><td align="center">'+@Companyname+'</td>';
set @tableHTML=@tableHTML + '<td align="center">'+@Deptname+'</td>';
set @tableHTML=@tableHTML + '<td align="center">'+@Certname+'</td>';
set @tableHTML=@tableHTML + '<td align="center">'+@Certtype+'</td>';
set @tableHTML=@tableHTML + '<td align="center">'+@Certlevel+'</td>';
set @tableHTML=@tableHTML + '<td align="center">'+@Expirdate+'</td>';
set @tableHTML=@tableHTML + '<td align="center">'+@Warnlevel+'</td></tr>';
--繼續(xù)遍歷下一條記錄
Fetch Next From cur_cert Into @Companyname,@Deptname,@Certname,@Certtype,@Certlevel,@Expirdate,@Warnlevel
end
--關閉游標
Close cur_cert
--釋放游標
Deallocate cur_cert
set @tableHTML=@tableHTML + '</table></td></tr>';
end
-- 發(fā)送郵件
exec msdb.dbo.sp_send_dbmail
@profile_name='crm***',
@recipients='156240***@qq.com',
@body=@tableHTML,
@body_format='HTML',
@subject='資質到期預警';
-- 刪除臨時表(#tbl_result)
if object_id('tempdb..#tbl_result') is not null
begin
drop table #tbl_result;
end
end
END
看起來無疑第二種特別啰嗦,但個人感覺很好理解,游標拼接html部分思路很清楚,以上兩個方式均經過實踐,如需使用只需要將其中對應的字段、數(shù)據(jù)源替換掉即可,感謝諸位賞足,有什么不足之處還望大家見諒,
*請認真填寫需求信息,我們會在24小時內與您取得聯(lián)系。