当前位置: 代码迷 >> 综合 >> Ext EditGrid 动态加载XML数据
  详细解决方案

Ext EditGrid 动态加载XML数据

热度:70   发布时间:2023-12-08 14:15:55.0
功能上是从点击树节点,然后在新的TabPanel中显示这个Grid,然后异步加载数据
js:
//创建可编辑记录Grid
function createQueryGrid(node)
{//alert(node.id);Ext.Ajax.request({url : 'query.ashx',params : { id : dbid,type : node.id.split('_')[1],//action : 'first',obj : node.text,num : '50',sod : 'store'},method : 'POST',success : function(response) {                    //alert(response.responseText);var obj = Ext.util.JSON.decode(response.responseText);                            var store = new Ext.data.Store(obj.store);var cm = new Ext.grid.ColumnModel({// specify any defaults for each columndefaults: {sortable: true // columns are not sortable by default           },columns:  obj.columns});var grid = new Ext.grid.EditorGridPanel({id: ('grid_query_'+node.id),store: store,cm: cm,renderTo: ('div_query_'+node.id),//width: 600,height: (Ext.getCmp('tab_'+node.id.split('_')[1]+'_'+node.text).getHeight() -30),//autoExpandColumn: 'common', // column with this id will be expanded//title: 'Edit Plants?',frame: true,clicksToEdit: 1,tbar: [{text: '新增记录',handler : function(){// access the Record constructor through the grid's store
//                        var item = grid.getStore().recordType;
//                        var p = new item();
//                        grid.stopEditing();
//                        store.insert(0, p);
//                        grid.startEditing(0, 0);}}]});//alert("toload");store.load(); },failure : function(response, options) {Ext.Msg.alert('错误','操作失败!');}});
}

query.ashx:
<%@ WebHandler Language="C#" Class="query" %>using System;
using System.Web;
using System.Data;
using System.Xml;
using SQLite;
using System.Data.SQLite;public class query : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{public void ProcessRequest(HttpContext context){        string action = context.Request.Params["action"];  //第一次还是第二次打开表string dbid = context.Request.Params["id"];         //数据库IDstring type = context.Request.Params["type"];       //类型string obj = context.Request.Params["obj"];         //对象string firstShowRows = context.Request.Params["num"]; //第一次打开返回行数string storeOrdata = context.Request.Params["sod"];       //查询表结构或表数据if (!string.IsNullOrEmpty(dbid) && !string.IsNullOrEmpty(type) && !string.IsNullOrEmpty(obj)){if (type.Equals("table") || type.Equals("view")){DataTable dtinfo = SqliteHelper.ExecuteDataSet(AppContainer.AppDBConnection, MapSql.GetMapSql.Sqlmappings["sql.usersdatabase.selectById"], new SQLiteParameter[] {new SQLiteParameter("@id",dbid)}).Tables[0];if (storeOrdata.Equals("store"))      //数据结构{DataSet ds = SqliteHelper.ExecuteDataSet(AppContainer.getUserDBConnection(dtinfo.Rows[0]["dbFile"].ToString()), "PRAGMA table_info(" + obj + ")", null);if (ds.Tables.Count > 0){string sc = CreateStoreAndColumns(ds.Tables[0],context.Request);context.Response.Write(sc);}}else if (storeOrdata.Equals("data")) {context.Response.ContentType = "text/xml";  //指定返回XML格式string sql = "select * from " + obj + " where rowid ";if (action.Equals("first")){sql += "<=" + firstShowRows;}else if (action.Equals("next")){sql += ">" + firstShowRows;}DataSet ds = SqliteHelper.ExecuteDataSet(AppContainer.getUserDBConnection(dtinfo.Rows[0]["dbFile"].ToString()), sql, null);if (ds.Tables.Count > 0){//组织XML文档DataTable dt = ds.Tables[0];XmlDocument document = new XmlDocument();XmlNode root = document.CreateNode(XmlNodeType.Element, "root", "");foreach (DataRow row in dt.Rows){XmlNode item = document.CreateNode(XmlNodeType.Element, "item", "");foreach (DataColumn dc in dt.Columns){XmlNode field = document.CreateNode(XmlNodeType.Element, dc.ColumnName, "");field.InnerText = row[dc].ToString();item.AppendChild(field);}root.AppendChild(item);}document.AppendChild(root);                        context.Response.Write(document.OuterXml);//输出XML文档}}}}context.Response.End();}/// <summary>/// 返回Store及columnModel的Json/// </summary>/// <param name="dt"></param>/// <param name="request"></param>/// <returns></returns>private static string CreateStoreAndColumns(DataTable dt,HttpRequest request){//结织返回数据集System.Text.StringBuilder tables = new System.Text.StringBuilder("{");//表头tables.Append("store:{autoDestroy: true,url: \"query.ashx?id=" + request.Params["id"] + "&action=first&type=" + request.Params["type"] + "&obj=" + request.Params["obj"] + "&num=" + request.Params["num"] + "&sod=data\", reader: new Ext.data.XmlReader({record: 'item',fields:[{");for (int i = 0; i < dt.Rows.Count; i++){tables.Append("name:\"" + dt.Rows[i]["name"].ToString() + "\"");if (i < dt.Rows.Count - 1){tables.Append("},{");}}tables.Append("}]})},");//列                                tables.Append("columns:[{");for (int i = 0; i < dt.Rows.Count; i++){tables.Append("id:\"" + dt.Rows[i]["name"].ToString() + "\",");tables.Append("header:\"" + dt.Rows[i]["name"].ToString() + "\",");tables.Append("dataIndex:\"" + dt.Rows[i]["name"].ToString() + "\"");if (i < dt.Rows.Count - 1){tables.Append("},{");}}tables.Append("}]");tables.Append("}");return tables.ToString();}public bool IsReusable{get{return false;}}}