JQuery template plugin jquery. tmpl dynamic ajax Extension

Source: Internet
Author: User

This plug-in was introduced in the previous JQuery template plug-in-jquery. tmpl. Sometimes we need to use dynamic ajax to load templates or data, and load different templates and Data Based on url parameters or other information. I have this requirement in a certain project, so I specially wrote jquery tool functions and added local data and ajax data loading templates and data methods.

Parameter description:

Tmpl: function (template, data, fun)
1: template:
1): url: the url for ajax loading. ajax is loaded only when remote = true.
2): data: load parameters for ajax
3) templateSelector: Specifies a local template selector. It is used only when remote is set to false.
4) remote: true indicates ajax, and false indicates local data,
5) cache: Indicates whether to cache the template.
2: data:
1): url: the url for ajax loading. ajax is loaded only when remote = true.
2): data: load parameters for ajax
3) templateData: local data, used only when remote = false
4) remote: true indicates ajax, and false indicates local data,
5) cache: Indicates whether to cache the template.
3: fun is the callback function:
Fun (jquery. tmpl object, template script, data );

The Code is as follows:Copy codeThe Code is as follows:; (function ($ ){
$. Extend ({
Tmpl_Data: function (te, data, fun, templatecache ){
Data = jQuery. extend ({data: "", url: "", templateData :{}, remote: true, cache: true}, data );
If (! Data. remote ){
Fun (te. tmpl (data. templateData), te, data. templateData );
If (! Templatecache ){
Te. remove ();
}
Return;
}
Var da = te. data ("objdata ");
If (data. cache & da! = Null & da! = Undefined ){
Fun (te. tmpl (da), te, da );
If (! Templatecache ){
Te. remove ();
}
Return;
}
$. Ajax ({
Type: "GET ",
Data: data. data,
Url: data. url,
DataType: "json ",
Cache: false,
Context: {template: te, data: data },
Success: function (tmpldata ){
Fun (this. template. tmpl (tmpldata), this. template, tmpldata );
If (data. cache ){
This. template. data ("objdata", tmpldata );
}
If (! Templatecache ){
This. template. remove ();
}
},
Error: function (e ){
Throw "get data error (" + this. data. url + "? "+ This. data. data +"): "+ e;
}
});
},
JquerySelecotrCharChange: function (str ){
Return str. replace (".", "\."). replace ("#","\\#");
},
Tmpl: function (template, data, fun ){
Template = jQuery. extend ({data: "", url: "", templateSelector: "", remote: true, cache: true}, template );
If (! Template. remote ){
$. Tmpl_Data ($ (template. templateSelector), data, fun, true );
Return;
}
Var te = null;
Try {
Te = $ ("script: [url = '" + $. JquerySelecotrCharChange (template. url + "? "+ Template. data) +" '] ")
}
Catch (e ){
}
If (template. cache & te! = Null & te. length> 0 ){
$. Tmpl_Data (te, data, fun, template. cache );
Return;
}
$. Ajax ({
Type: "GET ",
Data: template. data,
Url: template. url,
DataType: "html ",
Cache: false,
Context: {template: template, data: data },
Error: function (e ){
Throw "get template error (" + this. template. url + "? "+ This. template. data +"): "+ e;
},
Success: function (tmpltemplate ){
Var te = $ ('<script type = "text/x-jquery-tmpl">' + tmpltemplate + '<\/script>'). appendTo (document. body );
Te. attr ("url", (this. template. url + "? "+ This. template. data ));
$. Tmpl_Data (te, this. data, fun, this. template. cache );
}
});
}
});
}) (JQuery );

Test code:
Front-end:Copy codeThe Code is as follows: <% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Tmpl3.aspx. cs" Inherits = "Tmpl3" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd%22>
<Html xmlns = "http://www.w3.org/1999/xhtml%22>
<Head runat = "server">
<Title> </title>
<Scripts src = "script/jquery-1.6.4.js" type = "text/javascript"> </Script>
<Script src = "Script/jquery-jquery-tmpl-07d08cb/jquery. tmpl. js" type = "text/javascript"> </script>
<Script type = "text/javascript" src = "scripts/Remote-Tmpl.js"> </Script>
<Script type = "text/javascript">
; String. format = function (){
Var s = arguments [0];
For (var I = 0; I <arguments. length-1; I ++ ){
Var reg = new RegExp ("\\{" + I + "\\}", "gm ");
S = s. replace (reg, arguments [I + 1]);
}
Return s;
};
Function AjaxDeleteInvoke (id ){
Alert (String. format ("ajaxdeletvovoke: id = {0}", id ));
}
$ (Function (){
$. Tmpl ({url: "TmplTemplate.htm", data: "test = 1" },{ url: "Tmpl3.aspx", data: "ajax = 1"}, function (t, te, da ){
T. appendTo ("# test> tbody ");
$ ("# Test> tbody table"). hide ();
$ ("# Test. detailsImg"). live ("click", function (){
Var state = $ (this). data ("state ");
Var $ tr = $ (this). parent (). parent ();
If (state = "o "){
$ ("Table", $ tr. next (). hide ();
$ (This). data ("state", "c ");
$ (This). attr ("src", "Image/folder_o.png ");
} Else {
$ ("Table", $ tr. next (). show ();
$ (This). data ("state", "o ");
$ (This). attr ("src", "Image/folder_c.png ");
}
});
});
// $ ("# Btntest"). bind ("click", function (){
// $. Tmpl ({url: "TmplTemplate.htm", data: "test = 1" },{ url: "Tmpl3.aspx", data: "ajax = 1"}, function (t, te, da ){
// T. appendTo ("# Table1> tbody ");
// $ ("# Table1> tbody table"). hide ();
// $ ("# Table1. detailsImg"). live ("click", function (){
// Var state = $ (this). data ("state ");
// Var $ tr = $ (this). parent (). parent ();
// If (state = "o "){
// $ ("Table", $ tr. next (). hide ();
// $ (This). data ("state", "c ");
// $ (This). attr ("src", "Image/folder_o.png ");
//} Else {
// $ ("Table", $ tr. next (). show ();
// $ (This). data ("state", "o ");
// $ (This). attr ("src", "Image/folder_c.png ");
//}
//});
//});
//});
Var data = new Array ();
For (var I = 0; I <19; I ++ ){
Data. push (
{
Name: String. format ("Student {0}", I ),
Sex: I % 2 = 0? "Male": "female ",
ID: I,
Class:
[
{
ClassName: String. format ("Class {0}", I ),
Count: (I + 10)/2
},
{
ClassName: String. format ("Class2 {0}", I ),
Count: (I + 20)/2
}
]
});
}
$ ("# Btntest"). bind ("click", function (){
$. Tmpl ({url: "TmplTemplate.htm", data: "test = 1"}, {remote: false, templateData: data}, function (t, te, da ){
T. appendTo ("# Table1> tbody ");
$ ("# Table1> tbody table"). hide ();
$ ("# Table1. detailsImg"). live ("click", function (){
Var state = $ (this). data ("state ");
Var $ tr = $ (this). parent (). parent ();
If (state = "o "){
$ ("Table", $ tr. next (). hide ();
$ (This). data ("state", "c ");
$ (This). attr ("src", "Image/folder_o.png ");
} Else {
$ ("Table", $ tr. next (). show ();
$ (This). data ("state", "o ");
$ (This). attr ("src", "Image/folder_c.png ");
}
});
});
});
})
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div id = "div1">
<Table style = "margin-top: 10; margin-left: 300px;" border = "1" cellpadding = "0" cellspacing = "0"
Id = "test" width = "500">
<Thead>
<Tr style = "text-align: center; font-size: larger; font-weight: bolder;">
<Td>
ID
</Td>
<Td>
Name
</Td>
<Td>
Gender
</Td>
<Td>
Operation
</Td>
</Tr>
</Thead>
<Tbody>
</Tbody>
</Table>
<Hr/>
<P>
Test the cache system (url) </p>
<Input type = "button" id = "btntest" value = "testcache"/>
<Table style = "margin-top: 10; margin-left: 300px;" border = "1" cellpadding = "0" cellspacing = "0"
Id = "Table1" width = "500">
<Thead>
<Tr style = "text-align: center; font-size: larger; font-weight: bolder;">
<Td>
ID
</Td>
<Td>
Name
</Td>
<Td>
Gender
</Td>
<Td>
Operation
</Td>
</Tr>
</Thead>
<Tbody>
</Tbody>
</Table>
</Div>
</Form>
</Body>
</Html>

Background ajax data:Copy codeThe Code is as follows: protected void Page_Load (object sender, EventArgs e)
{
If (Request ["ajax"] = "1 ")
{
Response. Clear ();
Response. ContentType = "application/json ";
System. Text. StringBuilder sb = new System. Text. StringBuilder ("[");
For (int I = 0; I <20; I ++)
{
Sb. AppendFormat (@"{{
"" Name ":" "Student {0 }"",
"" Sex ":" "{1 }"",
"" ID "": {0 },
"" Class "":
[
{{
"" ClassName "": "" Class {0 }"",
"" Count "": {2}
}},
{{
"" ClassName "": "" Class2 {0 }"",
"" Count "": {3}
}}
]
}}, ", I, I % 2 = 0? "Male": "female", (I + 10)/2, (I + 20)/2 );
}
Sb. Remove (sb. Length-1, 1 );
Sb. Append ("]");
Response. Write (sb. ToString ());
Response. Flush ();
Response. Close ();
Response. End ();
}
}

The effect is shown in the previous article:

Download demo

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.