예제 #1
0
 public void BeforeLoad(ICPFormBeforeLoadEventArgs e)
 {
     if (e.IsEdit)
     {
         e.FormData.Tables[0].Rows[0]["UserPwd"] = "";
     }
 }
예제 #2
0
        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);
        }