EXT framework Learning (4) xtemplate and cookies

Source: Internet
Author: User
After learning the previous topics, I want to learn something cool now.
This time, we will learn how to use custom templates in ext to display data, learn how to use ajax to read and manipulate the returned data in the background, and learn how to set and read cookies.

First, let's take a look Code VaR TPL =   New Ext. xtemplate (
' <TPL for = "."> ' ,
' <Div class = "Thumb-wrap" id = "{itemname}"> ' ,
' <Div class = "Thumb"> <a href = "purchase/itemdetail. aspx? Itemid = {Itemid} "> </a> </div> ' ,
' <SPAN class = "X-editable"> {itemname} </span> & nbsp; ' ,
' <SPAN class = "X-editable"> {unitprice}. 00 RMB </span> & nbsp; ' ,
' <SPAN class = "X-editable"> {categoryid} </span> & nbsp; ' ,
' <A itemname = "{itemname}" Itemid = "{Itemid}" picturepath = "{picturepath}" unitprice = "{unitprice}" href = "purchase/shoppingcart. aspx? Itemid = {Itemid} "onclick =" document. additem (this); "target =" _ blank "> purchase <A/> ' ,
' & Nbsp; <a href = "#"> Add to favorites <A/> ' , ' </Div> ' , ' </TPL> ' ,
' <Div class = "X-clear"> </div> ' );

Here, we have defined an xtemplate. We can see that this is pure HTML. In this way, we can write it freely here. you only need to use {} to read the data fields in the store of dataview where the xtemplate is located. Good? Here is a tip. if you want to write some parameters to The onclick event, you may need some escape characters. If you are too lazy, you can also see the <A>, this is the document I wrote. additem (this), write a method in this way, and then define some attributes you need in <A>, such as itemname = {itemname }, then you can reference it in the custom method. The method here is defined as follows: Document. additem = Function (OBJ) {
Common. inclucart (obj. getattribute (
' Itemid ' ), 1 , OBJ
. Getattribute (
' Itemname ' ), OBJ
. Getattribute (
' Picturepath ' ), OBJ
. Getattribute (
' Unitprice ' ))
If (Ext. state. Manager. Get ( " Customerid " ) ! =   " 00000000-0000-0000-0000-000000000000 " )
&& (Ext. state. Manager. Get ( " Customerid " ) ! = Undefined )) {
// If you have logged in, you need to save the user's shopping cart to the database.
Ext. Ajax. Request ( {
URL:
' ../Handler/addcartitemhandler. ashx? Itemid = '
+ Getattribute ( ' Itemid ' ) +   ' & Shoppingcartid = '
+ Ext. state. Manager. Get ( " Customerid " ),
Success: function (response)
{
Alert (
"Shopping Cart added!")
}

}
)
}

}
;

The document here is to avoid failing to find this method, because the additem was undefined before, and now I don't know why. Just add the document. reference. You can tell me what you know ~~~~ Thanks !~ Getattribute (itemname) is a <A> method that can be referenced to <A> Custom Attributes. It seems to be a simple HTML, but I didn't understand it before. This is very useful, haha common. the shopping cart was written by Comrade codenong and added a commodity to the shopping cart. Ext. state. Manager. Get (customerid) is used to obtain the cookie value. Because the value is guid, it is determined .....
Next, use Ext. Ajax. Request to send it back to the background for processing, and add this item to the user's shopping cart (because the user has logged in, it must be saved to the database ).
By the way, the Ext. state. Manager. Set Method has two parameters: Cookie name, value, and set (name, value.

Finally, use panel to reference this xtemplate. The Code is as follows: VaR item1panel =   New Ext. Panel ( {
ID:
' Images-View ' ,
Frame:
True ,
Autoheight:
True ,
Collapsible:
True ,
Layout:
' Column ' ,
Title:
' Latest automotive products ' ,

Items:
New Ext. dataview ( {
Store: store,
TPL: TPL,
Autoheight:
True ,
Multiselect:
True ,
Overclass:
' X-View-over ' ,
Itemselector:
' Div. Thumb-wrap ' ,
Emptytext:
' No images to display '
}
)
}
);

TPL is the previously defined xtemplate,
Store is defined as follows: VaR store =   New Ext. Data. Store ( {
URL:
' Handler/handler. ashx ' ,
Reader:
New Ext. Data. jsonreader ( {
ID:
' Itemid ' ,
Fields :[
{
Name:
'Itemid'
}
, {
Name:
'Unitprice'
}
, {
Name:
'Itemname'
}
, {
Name:
'Categoryid'
}
, {
Name:
'Picturepath'
}
]
}
)
}
);

the simple example is complete. Now there is another problem: in ' purchase 'here, why can't I click the page I want to go to? I can only open the page by right-clicking -->, which is too painful. Do you know why? Tell me, I'm crying.

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.