asp.net使用jquery模板引擎jtemplates呈現(xiàn)表格
在Asp.net MVC 中,使得我們能夠更加自由控制我們所想顯示HTML。通常情況下,都要做一下數(shù)據(jù)列表。那么我們可以手動(dòng)去拼一個(gè)表格出來(lái),但這樣有時(shí)對(duì)于復(fù)雜的表格說(shuō),那就JS代碼比較復(fù)雜了。我們可以借助JS下的模板引擎,來(lái)實(shí)現(xiàn)這一功能。下面要介紹就是JTemplates,它也是基于Jquery的。
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="Content/default.css" rel="stylesheet" type="text/css" />
<script src="Scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-jtemplates.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
url: '<%=Url.Action("TempleteData", "Home") %>',
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
//instantiate a template with data
ApplyTemplate(msg);
}
});
});
function ApplyTemplate(msg) {
$('#Container').setTemplate($("#TemplateResultsTable").html());
$('#Container').processTemplate(msg);
}
</script>
</head>
<body>
<div id="Container"> </div>
<%-- Results Table Template --%>
<script type="text/html" id="TemplateResultsTable">
{#template MAIN}
<table cellpadding="10" cellspacing="0">
<tr>
<th>Username</th>
<th>Password</th>
<th>Url</th>
<th>Email</th>
<th>PassportID</th>
</tr>
{#foreach$Tasuu}
{#includeROWroot=$T.uu}
{#/for}
</table>
{#/templateMAIN}
{#templateROW}
<trclass="{#cyclevalues=['','evenRow']}">
<td>{$T.UserName.bold()}</td>
<td>{$T.Password}</td>
<td>{$T.Url.link($T.Url)}</td>
<td>{$T.Email.link('mailto:'+$T.Email)}</td>
<td>{$T.PassportID}</td>
</tr>
{#/templateROW}
</script>
</body>
</html>
通過(guò)ajax返回json數(shù)據(jù),setTemplate根據(jù)Id設(shè)置模板,然后ApplyTemplate就可以了。
CS代碼:
///<summary>
///Templetesthedata.
///</summary>
///<returns></returns>
publicJsonResultTempleteData()
{
IList<UserEntity>userlist=newList<UserEntity>()
{
newUserEntity(){UserName="Tina",PassportID=23433,Email="asdfa@asdf.com",Password="NKASD",Url="http://www.gefds.cn"}
,newUserEntity(){UserName="Lucy",PassportID=3444,Email="2v2@asdf.com",Password="v23sda",Url="http://www.qqfsad.cn"}
};
returnJson(userlist);
}
- Springboot讀取templates文件html代碼實(shí)例
- 深入淺析springboot中static和templates區(qū)別
- SpringBoot中關(guān)于static和templates的注意事項(xiàng)以及webjars的配置
- SpringBoot用JdbcTemplates訪問(wèn)Mysql實(shí)例代碼
- AngularJs Understanding Angular Templates
- 詳解Python的Django框架中的templates設(shè)置
- Blitz templates 最快的PHP模板引擎
- Springboot訪問(wèn)templates html頁(yè)面過(guò)程詳解
相關(guān)文章
WPF實(shí)現(xiàn)流光動(dòng)畫(huà)特效
這篇文章介紹了WPF實(shí)現(xiàn)流光動(dòng)畫(huà)特效的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
ASP.NET?MVC5網(wǎng)站開(kāi)發(fā)之用戶添加和瀏覽2(七)
這篇文章主要為大家詳細(xì)介紹了ASP.NET?MVC5網(wǎng)站開(kāi)發(fā)之用戶添加和瀏覽,感興趣的小伙伴們可以參考一下2016-08-08
Hangfire在ASP.NET CORE中的簡(jiǎn)單實(shí)現(xiàn)方法
下面小編就為大家分享一篇Hangfire在ASP.NET CORE中的簡(jiǎn)單實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
asp.net core下給網(wǎng)站做安全設(shè)置的方法詳解
這篇文章主要給大家介紹了關(guān)于asp.net core下給網(wǎng)站做安全設(shè)置的相關(guān)資料,文章通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
如何利用HttpClientFactory實(shí)現(xiàn)簡(jiǎn)單的熔斷降級(jí)
這篇文章主要給大家介紹了關(guān)于如何利用HttpClientFactory實(shí)現(xiàn)簡(jiǎn)單的熔斷降級(jí)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
AspNetPager與Socut.Data使用實(shí)例代碼
最近對(duì)AspNetPager與Socut.Data這兩個(gè)控件產(chǎn)生了濃厚的興趣,這兩個(gè)控件配合可以減輕很多程序員編寫(xiě)代碼的壓力。ASpNetPager為分頁(yè)控件,而Socut.Data為數(shù)據(jù)操作控件,ACCESS,MSSQL都可以。2008-07-07
ASP.NET?Core?MVC中的標(biāo)簽助手(TagHelper)用法
這篇文章介紹了ASP.NET?Core?MVC中標(biāo)簽助手(TagHelper)的用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04

