Exemple #1
0
 private static void AppendTree(TagTree tree, jp node)
 {
     foreach (var child in ((jo)node.Value).Properties())
     {
         tree.Add(node.Name, child.Name);
         AppendTree(tree, child);
     }
 }
Exemple #2
0
        public static DataTable ToDataTableFromJson(string json)
        {
            if (String.IsNullOrEmpty(json))
            {
                return(null);
            }
            DataTable result = new DataTable();

            Newtonsoft.Json.Linq.JArray jArray = Newtonsoft.Json.Linq.JArray.Parse(json);
            //Initialize the columns, If you know the row type, replace this
            foreach (var row in jArray)
            {
                foreach (var jToken in row)
                {
                    Newtonsoft.Json.Linq.JProperty jproperty = jToken as Newtonsoft.Json.Linq.JProperty;
                    if (jproperty == null)
                    {
                        continue;
                    }
                    if (result.Columns[jproperty.Name] == null)
                    {
                        result.Columns.Add(jproperty.Name, typeof(string));
                    }
                }
            }
            foreach (var row in jArray)
            {
                var datarow = result.NewRow();
                foreach (var jToken in row)
                {
                    Newtonsoft.Json.Linq.JProperty jProperty = jToken as Newtonsoft.Json.Linq.JProperty;
                    if (jProperty == null)
                    {
                        continue;
                    }
                    datarow[jProperty.Name] = jProperty.Value.ToString();
                }
                result.Rows.Add(datarow);
            }

            return(result);
        }
Exemple #3
0
 public string ToJson()
 {
     Newtonsoft.Json.Linq.JObject res = new Newtonsoft.Json.Linq.JObject();
     res.Add("state", _state);
     if (_state)
     {
         if (_data.Count > 0)
         {
             Newtonsoft.Json.Linq.JObject data = new Newtonsoft.Json.Linq.JObject();
             Newtonsoft.Json.Linq.JProperty propData = new Newtonsoft.Json.Linq.JProperty("data", data);
             res.Add(propData);
             foreach (string key in _data.Keys)
             {
                 data.Add(key, _data[key].ToString());
             }
         }
     }
     else
     {
         res.Add("message", _message ?? "错误");
     }
     return res.ToString();
 }
Exemple #4
0
 public string ToJson()
 {
     Newtonsoft.Json.Linq.JObject res = new Newtonsoft.Json.Linq.JObject();
     res.Add("state", _state);
     if (_state)
     {
         if (_data.Count > 0)
         {
             Newtonsoft.Json.Linq.JObject   data     = new Newtonsoft.Json.Linq.JObject();
             Newtonsoft.Json.Linq.JProperty propData = new Newtonsoft.Json.Linq.JProperty("data", data);
             res.Add(propData);
             foreach (string key in _data.Keys)
             {
                 data.Add(key, _data[key].ToString());
             }
         }
     }
     else
     {
         res.Add("message", _message ?? "错误");
     }
     return(res.ToString());
 }
Exemple #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="treeCode"></param>
        /// <param name="TreeDataSourceId"></param>
        /// <param name="parentDataRowJSON">parentDataRowJSON:父节点datarow的JSON,根节点为空,一般是逐级加载数据时用到</param>
        public List <CPTreeNode> GetTreeData(string treeCode, int TreeDataSourceId, string parentDataRowJSON)
        {
            CPTree tree = this.GetTree(treeCode, false, true);

            if (tree == null)
            {
                return(new List <CPTreeNode>());
            }
            if (string.IsNullOrEmpty(tree.ChkDefaultSelValue) == false)
            {
                tree.ChkDefaultSelValue = CPExpressionHelper.Instance.RunCompile(tree.ChkDefaultSelValue);
            }
            List <string> chkDefaultSelValue = null;

            if (string.IsNullOrEmpty(tree.ChkDefaultSelValue))
            {
                chkDefaultSelValue = new List <string>();
            }
            else
            {
                chkDefaultSelValue = tree.ChkDefaultSelValue.Split(',').ToList();
            }
            List <CPTreeNode> col = new List <CPTreeNode>();

            if (tree.DataLoadType == CPTreeEnum.DataLoadTypeEnum.AllLoad)
            {
                #region 全加载,只会调用一次  且不支持局部刷新
                //
                List <CPTreeDataSource> rootSourceCol = tree.DataSourceCol.Where(c => c.ParentSourceId.Equals(-1)).ToList();
                rootSourceCol.ForEach(t => {
                    string strSql    = t.DataSource;
                    strSql           = CPExpressionHelper.Instance.RunCompile(strSql);
                    DbHelper _helper = new DbHelper(t.DbIns, CPAppContext.CurDbType());
                    DataTable dt     = _helper.ExecuteDataSet(strSql).Tables[0];
                    if (string.IsNullOrEmpty(t.PKField) == false)
                    {
                        if (dt.Columns.Contains(t.PKField) == false)
                        {
                            throw new Exception("数据源SQL中未包括主键字段【" + t.PKField + "】");
                        }
                    }
                    foreach (DataRow dr in dt.Rows)
                    {
                        CPExpressionHelper.Instance.Add(CPTreeExpression.DataRowKey, dr);
                        CPTreeNode node       = new CPTreeNode();
                        node.NodeId           = "Node_" + Guid.NewGuid().ToString("N");
                        node.NodeTitle        = CPExpressionHelper.Instance.RunCompile(t.NodeTitle);
                        node.NodeAttrEx       = CPExpressionHelper.Instance.RunCompile(t.NodeAttrEx);
                        node.NodeIcon         = t.NodeIcon;
                        node.hasChildren      = false;
                        node.TreeDataSourceId = t.Id;
                        node.DataRowJSON      = "";
                        if (string.IsNullOrEmpty(t.PKField) == false)
                        {
                            node.DeleteFieldValue = dr[t.PKField].ToString();
                        }
                        else
                        {
                            node.DeleteFieldValue = "";
                        }
                        if (string.IsNullOrEmpty(t.ChkSelFieldName))
                        {
                            node.ChkSelFieldName = "";
                        }
                        else
                        {
                            node.ChkSelFieldName = t.ChkSelFieldName;
                            if (dt.Columns.Contains(t.ChkSelFieldName) == false)
                            {
                                throw new Exception("数据源SQL中未包括checkbox默认选中字段【" + t.ChkSelFieldName + "】");
                            }
                            node.ChkSelFieldValue = Convert.IsDBNull(dr[t.ChkSelFieldName.Trim()]) ? "" : dr[t.ChkSelFieldName.Trim()].ToString();
                            #region 处理check默认选中
                            if (chkDefaultSelValue.Count > 0)
                            {
                                if (chkDefaultSelValue.Contains(node.ChkSelFieldValue))
                                {
                                    node.Checked = true;
                                }
                                else
                                {
                                    node.Checked = false;
                                }
                            }
                            #endregion
                        }
                        node.items = new List <CPTreeNode>();
                        #region 获取子节点
                        if (t.SourceIsRecursion.Value)
                        {
                            //递归取数据
                            List <CPTreeNode> childCol = this.GetChildData(tree, t.Id, dr, chkDefaultSelValue);
                            if (childCol.Count > 0)
                            {
                                node.hasChildren = true;
                                node.items.AddRange(childCol);
                            }
                        }
                        //取下一级的数据
                        List <CPTreeDataSource> childSourceCol = tree.DataSourceCol.Where(c => c.ParentSourceId.Equals(t.Id)).ToList();
                        childSourceCol.ForEach(child => {
                            List <CPTreeNode> childCol = this.GetChildData(tree, child.Id, dr, chkDefaultSelValue);
                            if (childCol.Count > 0)
                            {
                                node.hasChildren = true;
                                node.items.AddRange(childCol);
                            }
                        });
                        #endregion
                        CPExpressionHelper.Instance.Remove(CPTreeExpression.DataRowKey);
                        col.Add(node);
                    }
                });
                #endregion
            }
            else
            {
                #region 逐级加载
                List <CPTreeDataSource> rootSourceCol = null;
                if (string.IsNullOrEmpty(parentDataRowJSON))
                {
                    rootSourceCol = tree.DataSourceCol.Where(c => c.Id.Equals(TreeDataSourceId)).ToList();
                }
                else
                {
                    rootSourceCol = tree.DataSourceCol.Where(c => c.Id.Equals(TreeDataSourceId)).ToList();
                    if (rootSourceCol[0].SourceIsRecursion.Value == false)
                    {
                        rootSourceCol = tree.DataSourceCol.Where(c => c.ParentSourceId.Equals(TreeDataSourceId)).ToList();
                    }
                    else
                    {
                        rootSourceCol.AddRange(
                            tree.DataSourceCol.Where(c => c.ParentSourceId.Equals(TreeDataSourceId)).ToList()
                            );
                    }
                }
                rootSourceCol.ForEach(t => {
                    string strSql = t.DataSource;
                    if (string.IsNullOrEmpty(parentDataRowJSON) == false)
                    {
                        dynamic formData = JsonConvert.DeserializeObject <dynamic>(parentDataRowJSON);
                        DataTable dtTmp  = new DataTable();
                        foreach (var key in formData)
                        {
                            Newtonsoft.Json.Linq.JProperty jKey = (Newtonsoft.Json.Linq.JProperty)key;
                            DataColumn dc = new DataColumn();
                            dc.ColumnName = jKey.Name;
                            dtTmp.Columns.Add(dc);
                        }
                        DataRow dr = dtTmp.NewRow();
                        foreach (var key in formData)
                        {
                            Newtonsoft.Json.Linq.JProperty jKey = (Newtonsoft.Json.Linq.JProperty)key;
                            try
                            {
                                dr[jKey.Name] = jKey.Value.ToString();
                            }
                            catch (Exception ex)
                            {
                                dr[jKey.Name] = "";
                            }
                        }
                        dtTmp.Rows.Add(dr);
                        CPExpressionHelper.Instance.Add(CPTreeExpression.DataRowKey, dtTmp.Rows[0]);
                        strSql = CPExpressionHelper.Instance.RunCompile(strSql);
                        CPExpressionHelper.Instance.Remove(CPTreeExpression.DataRowKey);
                    }
                    else
                    {
                        strSql = CPExpressionHelper.Instance.RunCompile(strSql);
                    }
                    DbHelper _helper = new DbHelper(t.DbIns, CPAppContext.CurDbType());
                    DataTable dt     = _helper.ExecuteDataSet(strSql).Tables[0];
                    if (string.IsNullOrEmpty(t.PKField) == false)
                    {
                        if (dt.Columns.Contains(t.PKField) == false)
                        {
                            throw new Exception("数据源SQL中未包括主键字段【" + t.PKField + "】");
                        }
                    }
                    foreach (DataRow dr in dt.Rows)
                    {
                        CPExpressionHelper.Instance.Add(CPTreeExpression.DataRowKey, dr);
                        CPTreeNode node       = new CPTreeNode();
                        node.NodeId           = "Node_" + Guid.NewGuid().ToString("N");
                        node.NodeTitle        = CPExpressionHelper.Instance.RunCompile(t.NodeTitle);
                        node.NodeAttrEx       = CPExpressionHelper.Instance.RunCompile(t.NodeAttrEx);
                        node.NodeIcon         = t.NodeIcon;
                        node.hasChildren      = false;
                        node.TreeDataSourceId = t.Id;
                        node.DataRowJSON      = CPUtils.DataRow2Json(dr);
                        if (string.IsNullOrEmpty(t.PKField) == false)
                        {
                            node.DeleteFieldValue = dr[t.PKField].ToString();
                        }
                        else
                        {
                            node.DeleteFieldValue = "";
                        }
                        if (string.IsNullOrEmpty(t.ChkSelFieldName))
                        {
                            node.ChkSelFieldName = "";
                        }
                        else
                        {
                            node.ChkSelFieldName = t.ChkSelFieldName;
                            if (dt.Columns.Contains(t.ChkSelFieldName) == false)
                            {
                                throw new Exception("数据源SQL中未包括checkbox默认选中字段【" + t.ChkSelFieldName + "】");
                            }
                            node.ChkSelFieldValue = Convert.IsDBNull(dr[t.ChkSelFieldName.Trim()]) ? "" : dr[t.ChkSelFieldName.Trim()].ToString();
                            #region 处理check默认选中
                            if (chkDefaultSelValue.Count > 0)
                            {
                                if (chkDefaultSelValue.Contains(node.ChkSelFieldValue))
                                {
                                    node.Checked = true;
                                }
                                else
                                {
                                    node.Checked = false;
                                }
                            }
                            #endregion
                        }
                        node.items = new List <CPTreeNode>();
                        #region 获取子节点
                        if (t.SourceIsRecursion.Value)
                        {
                            //递归取数据
                            int nCount = this.GetChildDataCount(tree, t.Id, dr);
                            if (nCount > 0)
                            {
                                node.hasChildren = true;
                            }
                        }
                        if (node.hasChildren == false)
                        {
                            //看下一级的数据
                            List <CPTreeDataSource> childSourceCol = tree.DataSourceCol.Where(c => c.ParentSourceId.Equals(t.Id)).ToList();
                            childSourceCol.ForEach(child =>
                            {
                                if (node.hasChildren)
                                {
                                    return;
                                }
                                int nCount = this.GetChildDataCount(tree, child.Id, dr);
                                if (nCount > 0)
                                {
                                    node.hasChildren = true;
                                }
                            });
                        }
                        #endregion
                        CPExpressionHelper.Instance.Remove(CPTreeExpression.DataRowKey);
                        col.Add(node);
                    }
                });


                #endregion
            }
            return(col);
        }