Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        private List <CPTreeNode> GetChildData(CPTree tree, int TreeDataSourceId, DataRow parentDR, List <string> chkDefaultSelValue)
        {
            List <CPTreeNode>       col       = new List <CPTreeNode>();
            List <CPTreeDataSource> SourceCol = tree.DataSourceCol.Where(c => c.Id.Equals(TreeDataSourceId)).ToList();

            SourceCol.ForEach(t => {
                string strSql = t.DataSource;
                CPExpressionHelper.Instance.Add(CPTreeExpression.DataRowKey, parentDR);
                strSql = CPExpressionHelper.Instance.RunCompile(strSql);
                CPExpressionHelper.Instance.Remove(CPTreeExpression.DataRowKey);
                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;
                    if (string.IsNullOrEmpty(t.PKField) == false)
                    {
                        node.DeleteFieldValue = dr[t.PKField].ToString();
                    }
                    else
                    {
                        node.DeleteFieldValue = "";
                    }
                    node.DataRowJSON = "";
                    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);
                }
            });
            return(col);
        }