Ext EditGrid 动态加载XML数据

功能上是从点击树节点,然后在新的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 column
                defaults: {
                    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"];         //数据库ID
        string 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;
        }
    }


}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值