コード例 #1
0
        /// <summary>
        /// 获取树格式对象
        /// </summary>
        /// <param name="commandText">sql</param>
        /// <param name="id">ID的字段名</param>
        /// <param name="pid">PID的字段名</param>
        /// <returns></returns>
        public static object ArrayToTreeData(string commandText, string id, string pid)
        {
            var reader = DbHelper.Db.ExecuteReader(commandText);
            var list   = DbReaderToHash(reader);

            return(JSONHelper.ArrayToTreeData(list, id, pid));
        }
コード例 #2
0
        /// <summary>
        /// 获取 ligerGrid(Tree) 所需要的JSON
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string GetGridTreeJSON(this DbContext dbContext, HttpContext context)
        {
            string view = context.Request["view"];

            string where = context.Request["where"];
            string idfield  = context.Request["idfield"];
            string pidfield = context.Request["pidfield"];
            string sql      = "select * from [{0}] where {1}";


            var whereTranslator = new FilterTranslator();
            var dpRule          = new DataPrivilegeRule(dbContext);

            if (!where.IsNullOrEmpty())
            {
                //反序列化Filter Group JSON
                whereTranslator.Group = JSONHelper.FromJson <FilterGroup>(where);
                //合并条件权限规则
                whereTranslator.Group = dpRule.GetRuleGroup(view, whereTranslator.Group);
            }
            else
            {
                whereTranslator.Group = dpRule.GetRuleGroup(view, whereTranslator.Group);
            }
            whereTranslator.Translate();
            where = whereTranslator.CommandText;

            sql = sql.FormatWith(view, where.IsNullOrEmpty() ? " 1=1 " : where);
            //创建command
            var cmd = CreateCommand(dbContext, sql, whereTranslator.Parms.ToArray());

            //使用liger.Data内置的数据适配器预处理command
            //比如对整数类型、日期类型的处理
            dbContext.Db.DbProvider.PrepareCommand(cmd);
            //获取树格式对象的JSON,这个方法首先会执行command,并且对结果进行格式化(转化为树格式)
            List <Hashtable> o = JSONHelper.ArrayToTreeData(cmd, idfield, pidfield) as List <Hashtable>;

            string json = @"{""Rows"":" + JSONHelper.ToJson(o) + @",""Total"":""" + o.Count + @"""}";

            return(json);
        }