/// <summary> /// 如果列表有编辑列,获取编辑列类型是下拉列表的数据源 /// </summary> /// <param name="gridObj"></param> /// <returns></returns> public Dictionary <string, string> ReadListDataAndFormat(CPGrid gridObj) { Dictionary <string, string> col = new Dictionary <string, string>(); gridObj.ColumnCol.ForEach(t => { if (t.ColumnType == CPGridEnum.ColumnTypeEnum.DropDownListEditor) { if (string.IsNullOrEmpty(t.FieldEnumDataIns)) { throw new Exception("字段" + t.FieldName + "配置成下拉列表编辑列,但未配置下拉列表数据源数据库实例"); } if (string.IsNullOrEmpty(t.FieldEnumDataSource)) { throw new Exception("字段" + t.FieldName + "配置成下拉列表编辑列,但未配置下拉列表数据源"); } DbHelper dbHelper = new DbHelper(t.FieldEnumDataIns, CPAppContext.CurDbType()); string strSql = t.FieldEnumDataSource; try { strSql = CPExpressionHelper.Instance.RunCompile(strSql); DataTable dt = dbHelper.ExecuteDataTable(strSql); DataTable dtNew = new DataTable(); dtNew.Columns.Add(new DataColumn() { ColumnName = "textEx", DataType = typeof(string) }); dtNew.Columns.Add(new DataColumn() { ColumnName = "valueEx", DataType = typeof(string) }); foreach (DataRow dr in dt.Rows) { DataRow newDR = dtNew.NewRow(); newDR["textEx"] = dr[0]; if (dt.Columns[1].DataType == Type.GetType("System.Boolean")) { newDR["valueEx"] = dr[1].ToString().Trim().ToLower(); } else { newDR["valueEx"] = dr[1]; } dtNew.Rows.Add(newDR); } col.Add("Column" + t.Id.ToString(), CPUtils.DataTable2Json(dtNew)); } catch (Exception ex) { throw new Exception("执行sql语句:" + strSql + "出错,详细信息为:" + ex.Message); } } }); return(col); }
public GetGridDataReturn GetGridData(string GridCode, int CurUserId, string CurUserIden, int page, int pageSize, string OtherCondition) { base.SetHeader(); GridCode = CPAppContext.FormatSqlPara(GridCode); CurUserIden = CPAppContext.FormatSqlPara(CurUserIden); //OtherCondition = CPAppContext.FormatSqlPara(OtherCondition); if (OtherCondition != null) { OtherCondition = System.Web.HttpUtility.UrlDecode(OtherCondition); OtherCondition = OtherCondition.Replace("@", "%"); } GetGridDataReturn re = new GetGridDataReturn(); try { CPGrid Grid = CPGridEngine.Instance(CurUserId).GetGrid(GridCode, true, true); string orderBy = ""; #region 获取排序字段 List <string> querys = CPAppContext.GetHttpContext().Request.Query.Keys.Where(t => t.StartsWith("sort[")).ToList(); var queryCount = querys.Count(m => m.EndsWith("[field]")); for (int i = 0; i < queryCount; i++) { //请查询字段和对应的值存储在一个字典中 string field = CPAppContext.QueryString <string>("sort[" + i + "][field]"); field = field.Replace("Column", ""); List <CPGridColumn> colTmp = Grid.ColumnCol.Where(c => c.Id.Equals(int.Parse(field))).ToList(); if (colTmp.Count > 0) { field = colTmp[0].FieldName; } string fieldValue = CPAppContext.QueryString <string>("sort[" + i + "][dir]"); if (string.IsNullOrEmpty(orderBy)) { orderBy = field + " " + fieldValue; } else { orderBy += "," + field + " " + fieldValue; } } #endregion if (this.CheckUserIden(CurUserId, CurUserIden) == false) { re.Result = false; re.ErrorMsg = "系统检测到非法获取数据,请传入正确的用户会话Key与用户Id参数!"; return(re); } try { int recordSize = 0; DataTable dt = CPGridEngine.Instance(CurUserId).ReadDataAndFormat(Grid, page, pageSize, OtherCondition, orderBy, out recordSize); re.RecordSize = recordSize; re.DataJson = CPUtils.DataTable2Json(dt); re.Result = true; } catch (Exception ex) { re.Result = false; re.ErrorMsg = ex.Message.ToString(); } return(re); } catch (Exception ex) { re.Result = false; re.ErrorMsg = ex.Message.ToString(); return(re); } }
public string GetFormDataJSON(CPForm form, List <CPFormChildTable> childTableCol, List <CPFormField> fieldCol, List <CPFormFieldInit> initValueCol, string pkValue, CPFormUseScene useScene) { string sJSON = ""; Dictionary <string, string> col = new Dictionary <string, string>(); DataSet ds = this.GetFormData(form, childTableCol, fieldCol, pkValue); if (string.IsNullOrEmpty(pkValue) == false) { #region 数据 if (childTableCol != null && childTableCol.Count > 0) { childTableCol.ForEach(t => { if (ds.Tables[t.TableName].Rows.Count <= 0) { DataRow drChild = ds.Tables[t.TableName].NewRow(); foreach (DataColumn dc in ds.Tables[t.TableName].Columns) { drChild[dc.ColumnName] = DBNull.Value; } ds.Tables[t.TableName].Rows.Add(drChild); } }); } #endregion } else { #region 无数据,新增 ,自动给每个表加一条空的数据 DataRow drMain = ds.Tables[form.MainTableName].NewRow(); foreach (DataColumn dc in ds.Tables[form.MainTableName].Columns) { drMain[dc.ColumnName] = DBNull.Value; } ds.Tables[form.MainTableName].Rows.Add(drMain); if (childTableCol != null && childTableCol.Count > 0) { childTableCol.ForEach(t => { DataRow drChild = ds.Tables[t.TableName].NewRow(); foreach (DataColumn dc in ds.Tables[t.TableName].Columns) { drChild[dc.ColumnName] = DBNull.Value; } ds.Tables[t.TableName].Rows.Add(drChild); }); } #endregion } //加入初始化的代码 ds = this.InitFieldValue(form, childTableCol, fieldCol, initValueCol, ds, pkValue); if (string.IsNullOrEmpty(useScene.FormLoadHandler) == false) { string[] sArray = useScene.FormLoadHandler.Split(';'); for (int i = 0; i < sArray.Length; i++) { if (string.IsNullOrEmpty(sArray[i])) { continue; } try { ICPFormBeforeLoadEventArgs e = new ICPFormBeforeLoadEventArgs(form, pkValue, ds); ICPFormBeforeLoad inter = Activator.CreateInstance(Type.GetType(sArray[i])) as ICPFormBeforeLoad; inter.BeforeLoad(e); } catch (Exception ex) { throw new Exception("调用表单加载前扩展二次开发方法【" + sArray[i] + "】时出错,错误信息如下 :" + ex.Message); } } } foreach (DataTable dt in ds.Tables) { col.Add(dt.TableName, CPUtils.DataTable2Json2(dt)); } #region 获取所有下拉列表的数据源 fieldCol.ForEach(t => { if (t.ControlType == CPFormEnum.ControlTypeEnum.DropDownList || t.ControlType == CPFormEnum.ControlTypeEnum.Radio || t.ControlType == CPFormEnum.ControlTypeEnum.CheckBox ) { if (string.IsNullOrEmpty(t.ListDbIns)) { throw new Exception("字段[" + t.FieldName + "]配置成下拉列表或复选框或单选,但未配置数据源数据库链接实例"); } DbHelper _helper = new DbHelper(t.ListDbIns, CPAppContext.CurDbType()); string s = t.ListSql; s = CPExpressionHelper.Instance.RunCompile(s); if (string.IsNullOrEmpty(s)) { throw new Exception("字段[" + t.FieldName + "]配置成下拉列表或复选框或单选,但未配置数据源SQL语句"); } DataTable dt = _helper.ExecuteDataSet(s).Tables[0]; DataTable dtNew = new DataTable(); dtNew.Columns.Add(new DataColumn() { ColumnName = "textEx", DataType = typeof(string) }); dtNew.Columns.Add(new DataColumn() { ColumnName = "valueEx", DataType = typeof(string) }); dtNew.Columns.Add(new DataColumn() { ColumnName = "listRelateEx", DataType = typeof(string) }); if (t.ControlType == CPFormEnum.ControlTypeEnum.DropDownList) { DataRow newDR0 = dtNew.NewRow(); newDR0["textEx"] = "==请选择=="; newDR0["valueEx"] = ""; newDR0["listRelateEx"] = ""; dtNew.Rows.Add(newDR0); } foreach (DataRow dr in dt.Rows) { DataRow newDR = dtNew.NewRow(); newDR["textEx"] = dr[0]; if (dt.Columns[1].DataType == Type.GetType("System.Boolean")) { newDR["valueEx"] = dr[1].ToString().Trim().ToLower(); } else { newDR["valueEx"] = dr[1]; } newDR["listRelateEx"] = ""; if (string.IsNullOrEmpty(t.ListRelateTargetField) == false) { try { if (dt.Columns[2].DataType == Type.GetType("System.Boolean")) { newDR["listRelateEx"] = dr[2].ToString().Trim().ToLower(); } else { newDR["listRelateEx"] = dr[2]; } } catch (Exception ex) { throw new Exception("字段[" + t.FieldName + "]配置成下拉列表联动,请确保数据源第三个字段值为联动值,配置的SQL为:" + s + "详细信息如下:" + ex.Message.ToString()); } } dtNew.Rows.Add(newDR); } col.Add(t.TableName + "_" + t.FieldName, CPUtils.DataTable2Json2(dtNew)); } else if (t.ControlType == CPFormEnum.ControlTypeEnum.Combox ) { DbHelper _helper = new DbHelper(t.ListDbIns, CPAppContext.CurDbType()); string s = t.ListSql; s = CPExpressionHelper.Instance.RunCompile(s); DataTable dt = _helper.ExecuteDataSet(s).Tables[0]; DataTable dtNew = new DataTable(); dtNew.Columns.Add(new DataColumn() { ColumnName = "textEx", DataType = typeof(string) }); DataRow newDR0 = dtNew.NewRow(); newDR0["textEx"] = ""; dtNew.Rows.Add(newDR0); foreach (DataRow dr in dt.Rows) { DataRow newDR = dtNew.NewRow(); if (dt.Columns[0].DataType == Type.GetType("System.Boolean")) { newDR["textEx"] = dr[0].ToString().Trim().ToLower(); } else { newDR["textEx"] = dr[0]; } dtNew.Rows.Add(newDR); } col.Add(t.TableName + "_" + t.FieldName, CPUtils.DataTable2Json2(dtNew)); } }); #endregion #region 如果没有拓展表,则自动构建一个隐藏的下拉列表,用来解决没有ng-repeat,不能执行ngRepeatFinished事件的问题 if (form.ChildTableCol.Count <= 0) { DataTable dtNew = new DataTable(); dtNew.Columns.Add(new DataColumn() { ColumnName = "textEx", DataType = typeof(string) }); dtNew.Columns.Add(new DataColumn() { ColumnName = "valueEx", DataType = typeof(string) }); col.Add("CPFormTmpHideSelectTable", CPUtils.DataTable2Json2(dtNew)); } #endregion sJSON = JsonConvert.SerializeObject(col); return(sJSON); }
/// <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); }