基于jquery ajax html 模板 的母子表的子表table增删操作.记一下,免得重复劳动.既简洁又容易扩展.
来源:互联网 发布:济南淘宝运营助理招聘 编辑:程序博客网 时间:2024/04/29 23:32
<!-- 扩展属性--> <div tabpanel="propetytabpanel" class="form_table_input"> <table id="tablist" class="datatable" border="1" cellspacing="0" bordercolorlight="#dddddd" bordercolordark="#ffffff" cellpadding="0" width="100%"> <tbody> <tr> <th width="20%"> 属性名称 </th> <th width="20%"> 英文属性名称 </th> <th width="20%"> 排序 </th> <th width="10%"> 操作 </th> </tr> </tbody> <tbody id="ExtendPropetyContent"> </tbody> <tbody id="ExtendPropetyContentCcontentTemple" style="display:none;"> <tr DataId="{ID}"> <td> {PropertyName} </td> <td> {PropertyEnglishName} </td> <td> <input type="text" class="Sort" value="{Sort}" /> </td> <td> <a href="javascript:DelExtendPropety({ID})" >删除</a> </td> </tr> </tbody> </table> <script> var template = JQ("#ExtendPropetyContentCcontentTemple").html(); var $content = JQ("#ExtendPropetyContent"); var ProductTypeID = JQ("#ProductTypeID").val(); var Sort = 0; function DelExtendPropety(id) { if (confirm('确定要永久删除该信息吗?删除后将不能被恢复!')) { var param = "Option=delpropety&ProductTypeID=" + ProductTypeID + "&id=" + id; var options = { method: 'post', parameters: param, onComplete: function (transport) { var retv = transport.responseText; if (retv.indexOf("成功") > 0) { $content.find("*[DataId=" + id + "]").remove(); } else { alert(retv); } } } } else { return false; } new Ajax.Request('product_type_edit_ajax.ashx', options); } function AddExtendPropety(rowdata) { var t = template; //自动排序 rowdata.Sort = Sort++; for (var p in rowdata) { var reg = new RegExp("{" + p + "}","ig"); //只有正则表达式才替换全部 t = t.replace(reg, rowdata[p]); } //第一个元素上绑定数据 var c = JQ(t); var rowhtml = c.first().data(rowdata)// .click(function () {// alert(JQ(this).data().ID);// }); $content.append(c); } //加载扩展属性 function loadExtendPropety() { JQ.ajax({ type: "POST", dataType: "json", url: "product_type_edit_ajax.ashx", data: { "ProductTypeID": ProductTypeID, "Option": "GetExtendPropety" }, success: function (data) { for (var i = 0; i < data.length; i++) { AddExtendPropety(data[i]); } } }); } //加载数据 loadExtendPropety(); function showFuHaoWindow() { var returnvalue = window.showModalDialog('product_existFuHao.aspx?propertyId=' + ProductTypeID, null, 'dialogheight=600; dialogwidth=800; edge=Raised; center=Yes; resizable=Yes; status=Yes;'); for (var i = 0; i < returnvalue.length; i++) { AddExtendPropety({ "PropertyName": returnvalue[i],"ID":0 ,"PropertyEnglishName":""}); } } //把系列扩展属性拼装成起来传到后台 function joinExtendProperty() { var datas = []; $content.children().each(function () { var d = JQ(this).data(); d.PropertyValue = ""; d.TypeID = ProductTypeID; //这些是业务操作可根据需要修改 d.Sort = JQ(this).find("input.Sort").val(); //更新可输入项 datas.push(d); }); var json = JQ.toJSON(datas); json = eval(json); //这句话是toJSON返回的字符串把 " 换成 \" 了所以要转换一次 JQ("#extendpropty").val(json); } </script> <br /> <a class="inputbutton_a" target="_blank" onclick="javascript:showFuHaoWindow()">添加符号属性</a> <br /> <input type="hidden" value="" id="extendpropty" name="extendpropty" /> </div>
product_type_edit_ajax.ashx
后台代码
<%@ WebHandler Language="C#" Class="product_type_edit_ajax" %>using System;using System.Web;using System.Web.SessionState;public class product_type_edit_ajax : IHttpHandler, IRequiresSessionState{ public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; if (Request["Option"] != string.Empty ) { string Option = context.Request.Form["Option"].Trim(); int propertyId = PageRequest.GetFormInt("ProductTypeID"); if (Option == "delpropety") { int id = PageRequest.GetFormInt("id"); context.Response.Write( delpropety(id, propertyId)); } else if (Option == "GetExtendPropety") { context.Response.Write(GetPropertyList(propertyId)); } context.Response.End(); return; } } /// <summary> /// 获取产品系列属性 的Html /// </summary> /// <returns></returns> protected string GetPropertyList(int productTypeId) { //Shop.BLL.Product.ProductType bll = new Shop.BLL.Product.ProductType(); var dataPage = new List<User>();//bll.GetProductPropertyAllByWhere(productTypeId,"1=1"); //JavaScriptSerializer类在System.Web.Extensions.dll中,注意添加这个引用 System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); //JSON序列化 string result = serializer.Serialize(dataPage); return result; //Console.WriteLine("使用JavaScriptSerializer序列化后的结果:{0},长度:{1}", result, result.Length); //JSON反序列化 //user = serializer.Deserialize<User>(result); //Console.WriteLine("使用JavaScriptSerializer反序列化后的结果:UserId:{0},UserName:{1},CreateDate:{2},Priority:{3}", user.UserId, user.UserName, user.CreateDate, user.Priority); } /// <summary> /// 删除产品类型的属性 扩展属性(参数啥的) /// </summary> /// <param name="id"></param> /// <param name="propertyId"></param> private string delpropety(int id, int propertyId) {/*删除操作*/ return "删除成功"; } public bool IsReusable { get { return false; } }}
还有一段后台保存的代码
/// <summary> /// 保存扩展属性 /// </summary> /// <param name="productTypeId">产品系列ID</param> private void SaveExtendPropty(int productTypeId) { string json =Request["extendpropty"]; System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); //JSON反序列化 产品系列对应的扩展属性 var extendProperty = serializer.Deserialize<List<Model.Product.ProductTypeProperty>>(json); if (extendProperty == null) return; Shop.BLL.Product.ProductType bll = new Shop.BLL.Product.ProductType(); foreach (Model.Product.ProductTypeProperty extend in extendProperty) { if (extend.ID > 0) { bll.UpdateProductProperty(extend,extend.TypeID); } else { bll.AddProductProperty(extend); } } //Console.WriteLine("使用JavaScriptSerializer反序列化后的结果:UserId:{0},UserName:{1},CreateDate:{2},Priority:{3}", user.UserId, user.UserName, user.CreateDate, user.Priority); }
我这里的Jquery简写成JQ 如果你需要的话可以改成$
另外这里的JQ.toJSON()这个函数是来自与一个开源库 jquery.json-2.2.js
这个代码主要是实现,主表附带子表这种情况,
需要保存前就编辑的情况... 主表保存的时候,子表一并保存..主表不保存,子表也不保存..
一对多关系的数据编辑界面适合使用这个代码..
- 基于jquery ajax html 模板 的母子表的子表table增删操作.记一下,免得重复劳动.既简洁又容易扩展.
- 基于jQuery的AJAX请求Table显示
- 尽可能的降低重复劳动——模板策略
- 记一下。。某大牛的blog。。免得搞丢了
- 简单的SQL语句记一下,免得自己忘记
- 基于jQuery的AJAX和JSON实现纯html数据模板
- 基于jQuery的AJAX和JSON实现纯html数据模板
- 基于jQuery的AJAX和JSON实现纯html数据模板
- 基于jQuery的AJAX和JSON实现纯html数据模板
- 基于jQuery的AJAX和JSON实现纯html数据模板
- 基于jQuery的AJAX和JSON实现纯html数据模板
- jquery的ajax扩展
- jQuery实例--Ajax信息的增删改查等操作
- jQuery简洁大方的登录页面模板
- jquery ajax调用既支持json又支持html通信的经典代码
- InfoPath中重复表的操作+repeat+table+operation
- 基于jquery扩展漂亮的分页控件(ajax)
- jquery 操作table tr的例子,以及一个ajax实例。
- 波特率与比特率
- linux统计命令总结
- 自由落体
- 转:Chirp用音频做病毒传播 或引发新一轮山寨热潮
- linux下的apache安装(不支持ssl)
- 基于jquery ajax html 模板 的母子表的子表table增删操作.记一下,免得重复劳动.既简洁又容易扩展.
- Thinning
- 读书笔记整理六:B树索引
- 读取-陆态网络-网络工程-点之记 特定内容
- ProgressDialog用作的等待操作的对话框
- linux下的mysql安装(64位无需编译版)
- 也许,未来需要重新规划
- 学习iOS开发,你不得不知道的6个概念
- Skeletonization