asp. net renders tables using the jquery template engine jtemplates

  • 2020-10-31 21:43:47
  • OfStack

In ES1en. net MVC, we have more control over how we want to display HTML. In general, you make a list of data. So we can manually put together a table, but sometimes for complex tables, JS code is more complex. We can use the template engine under JS to achieve this function. The following is JTemplates, which is also based on 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>

json data is returned from ajax, setTemplate sets the template against Id, and ApplyTemplate is done.
CS code:

///<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);
}


Related articles: