jQuery購物車插件jsorder用法(支持后臺處理程序直接轉(zhuǎn)換成DataTable處理)
本文實例講述了jQuery購物車插件jsorder用法。分享給大家供大家參考,具體如下:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"/> <title></title> <script type="text/javascript" src="../js/jquery-1.9.1.min.js" ></script> <link href="./demo.css" rel="stylesheet"/> <link href="../css/order.css" rel="stylesheet"/> <script type="text/javascript" src="../js/cookie.js" ></script> <script type="text/javascript" src="../js/jsorder.1.1.js" ></script> </head> <body> <h1>JSORDER 案例</h1> <table><tr><td colspan="3" align="left">案例一:我的菜單(點擊菜名即可加入菜單)</td></tr><tr> <td class="jsorderadd" id="80001" productid="80001" price="12" jsordername="紅燒豆腐">紅燒豆腐 12元</td> <td class="jsorderadd" id="80002" productid="80002" price="32" jsordername="毛血旺">毛血旺 32元</td> <td class="jsorderadd" id="80003" productid="80003" price="18" jsordername="套餐:京醬肉絲+2米飯 18元">套餐:京醬肉絲+2米飯 18元</td></tr></table> <div id="result"></div> </body> </html> <script type="text/javascript"> //jsorder配置 $.fn.jsorder.defaults = { staticname: 'jsorder', jsorderclass: 'jsorder', savecookie: true, cookiename: 'jsorder', numpre: 'no_', jsorderpre: 'jsorder_', jsorderspanpre: 'jsorderspan_', pricefiled: 'price', namefiled: 'jsordername', leftdemo: '我的菜單', subbuttom: '', //addbuttom : 'a.jsorderadd', addbuttom: 'td.jsorderadd', nomessage: '你今天的食譜是還空的', dosubmit: function (data) { alert(JSON.stringify(data)); //$("#result").html("json內(nèi)容:" + JSON.stringify(data)).css('background', '#e0e0e0'); jsonAjax("ShoppingCar.ashx", JSON.stringify(data), "text", getsuccess); } }; $("body").jsorder(); function jsonAjax(url, param, datat, callback) { $.ajax({ type: "post", url: url, data: param, dataType: datat, success: callback, error: function () { jQuery.fn.mBox({ message: '恢復(fù)失敗' }); } }); }; function getsuccess(o) { //alert(o); //成功后操作 } </script>
<%@ WebHandler Language="C#" Class="ShoppingCar" %> using System; using System.Web; using System.Data; using System.Web.Script.Serialization; using System.Collections.Generic; using System.Collections; using System.IO; public class ShoppingCar : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; StreamReader reader = new StreamReader(context.Request.InputStream); string jsonString = HttpUtility.UrlDecode(reader.ReadToEnd()); if (MSCL.Until.IsNullOrDBNull(jsonString)) { context.Response.Write("error"); } else { jsonString = "{\"goods\": [" + jsonString + "]}"; DataSet ds = JsonToDataSet(jsonString); //獲取的購物車商品列表 context.Response.Write("ok"); } context.Response.End(); } #region 解析Json成DataTable /// <summary> /// 解析Json成DataTable /// </summary> /// <param name="Json">Json字符串</param> /// <returns></returns> public static DataSet JsonToDataSet(string Json) { try { DataSet ds = new DataSet(); DataTable dt = new DataTable("shoppingcar"); JavaScriptSerializer JSS = new JavaScriptSerializer(); object obj = JSS.DeserializeObject(Json); Dictionary<string, object> datajson = (Dictionary<string, object>)obj; foreach (var item in datajson) { object[] rows = (object[])item.Value; foreach (var row in rows) { Dictionary<string, object> valneed = (Dictionary<string, object>)row; foreach (var needrow in valneed.Values) { #region Dictionary<string, object> val = (Dictionary<string, object>)needrow; DataRow dr = dt.NewRow(); foreach (KeyValuePair<string, object> sss in val) { if (!dt.Columns.Contains(sss.Key)) { dt.Columns.Add(sss.Key.ToString()); dr[sss.Key] = sss.Value; } else dr[sss.Key] = sss.Value; } dt.Rows.Add(dr); #endregion } } } ds.Tables.Add(dt); return ds; } catch { return null; } } #endregion public bool IsReusable { get { return false; } } }
<!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> <title>讀取本地購物車Cookie</title> <script type="text/javascript" src="../js/jquery-1.9.1.min.js" ></script> <link href="./demo.css" rel="stylesheet"/> <link href="../css/order.css" rel="stylesheet"/> <script type="text/javascript" src="../js/cookie.js" ></script> <script type="text/javascript" src="../js/jsorder.1.1.js" ></script> <script type="text/javascript"> //初始化配置 var staticname = 'jsorder'; var jsorderpre = 'jsorder_'; var html = ""; $(function () { if ($.cookie(staticname) != null && $.cookie(staticname) != '{}') { $("#list").html(""); initdata = eval('(' + $.cookie(staticname) + ')'); //序列化成數(shù)組 $("body").data(staticname, initdata); //alert(JSON.stringify(initdata)); $.each(initdata, function (index, item) { //循環(huán)獲取數(shù)據(jù) var Id = initdata[index]["productid"]; var Name = initdata[index]["name"]; var Price = initdata[index]["price"]; var Count = initdata[index]["count"]; var innerhtml = "<li id='" + jsorderpre + Id + "'>"; innerhtml += Id + "--" + Name + "--" + Price + " "; innerhtml += "<a href='javascript:void(0)' style='text-decoration:none;' onclick='subnum(" + Id + ")'> - </a><span id='count" + Id + "' >" + Count; innerhtml += "</span><a href='javascript:void(0)' style='text-decoration:none;' onclick='addnum(" + Id + ")'> + </a>"; innerhtml += "</li>" html += innerhtml; }); $("#list").append(html); } }); function subnum(id) { var datejsorder = $("body").data(staticname); datejsorder[id]['count'] -= 1; if (datejsorder[id]['count'] > 0) { $("#count" + id).html(datejsorder[id]['count']); } else { $("#" + jsorderpre + id).remove(); delete datejsorder[id]; //del json keyValue } savecookie(datejsorder); } function addnum(id, count) { var datejsorder = $("body").data(staticname); datejsorder[id]['count'] += 1; $("#count" + id).html(datejsorder[id]['count']); savecookie(datejsorder); } function savecookie(data) { var date = new Date(); date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000)); $.cookie(staticname, JSON.stringify(data), { path: '/', expires: date }); } function dosubmit() { var datejsorder = $("body").data(staticname); alert(JSON.stringify(datejsorder)); //$("#result").html("json內(nèi)容:" + JSON.stringify(data)).css('background', '#e0e0e0'); jsonAjax("ShoppingCar.ashx", JSON.stringify(datejsorder), "text", getsuccess); } function getsuccess(o) { //alert(o); //成功后操作 } function jsonAjax(url, param, datat, callback) { $.ajax({ type: "post", url: url, data: param, dataType: datat, success: callback, error: function () { jQuery.fn.mBox({ message: '恢復(fù)失敗' }); } }); }; </script> </head> <body> <div> <ul id="list"> <li>購物車?yán)飼簾o商品</li> </ul> <input type="button" value="確定,下一步" onclick="dosubmit();" /> </div> </body> </html>
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jquery中Ajax用法總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》、《jQuery動畫與特效用法總結(jié)》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計有所幫助。
- Spring Data Jpa+SpringMVC+Jquery.pagination.js實現(xiàn)分頁示例
- jQuery.datatables.js插件用法及api實例詳解
- jQuery ajax dataType值為text json探索分享
- 當(dāng)自定義數(shù)據(jù)屬性為json格式字符串時jQuery的data api問題探討
- Python requests.post方法中data與json參數(shù)區(qū)別詳解
- JS中FormData類實現(xiàn)文件上傳
- thinkphp5 + ajax 使用formdata提交數(shù)據(jù)(包括文件上傳) 后臺返回json完整實例
- JS JQuery獲取data-*屬性值方法解析
相關(guān)文章
基于DOM節(jié)點刪除之empty和remove的區(qū)別(詳解)
下面小編就為大家?guī)硪黄贒OM節(jié)點刪除之empty和remove的區(qū)別(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09jQuery.extend 與 jQuery.fn.extend的用法及區(qū)別實例分析
這篇文章主要介紹了jQuery.extend 與 jQuery.fn.extend的用法及區(qū)別,結(jié)合實例形式分析了jQuery.extend與jQuery.fn.extend的功能、使用方法及區(qū)別,需要的朋友可以參考下2018-07-07jquery <li>標(biāo)簽 隔若干行加空白或者加虛線的方法
下面小編就為大家?guī)硪黄猨query <li>標(biāo)簽 隔若干行加空白或者加虛線的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12基于jQuery實現(xiàn)帶動畫效果超炫酷的彈出對話框(附源碼下載)
這是一款基于jQuery的彈出對話框插件,這個jQuery對話框插件的最大特點是彈出和關(guān)閉都帶有非常炫酷的動畫特效,需要的朋友參考下吧2016-02-02