public static jqGridData TojqGridData(this System.Dynamic.ExpandoObject obj, int[] widths = null, string[] aligns = null, bool act = false, List <FormateTitle> titles = null) { var jqgridParams = new jqGridData(); var tTarget = obj as System.Dynamic.IDynamicMetaObjectProvider; if (tTarget != null) { bool flag = false; int len = 0; if (widths != null && widths.Length > 0) { len = widths.Length; flag = true; } int widthIndex = 0; int?widthParam = 200; bool alignFlag = false; int alignLen = 0; if (aligns != null && aligns.Length > 0) { alignLen = aligns.Length; alignFlag = true; } int alignIndex = 0; string alignParam = "left"; if (act) { //jqgridParams.colNames.Add("act".formateTitle(titles)); jqgridParams.colNames.Add("執行動作"); jqgridParams.colModel.Add(new jqGridColModel() { name = "act", index = "act", width = flag ? (len > widthIndex ? widths[widthIndex] : 100) : 100, sortable = false }); widthIndex += 1; } var names = tTarget.GetMetaObject(System.Linq.Expressions.Expression.Constant(tTarget)).GetDynamicMemberNames(); foreach (string name in names) { var str = name; jqgridParams.colModel.Add(new jqGridColModel() { name = str, index = str, width = flag ? (len > widthIndex ? widths[widthIndex] : widthParam) : widthParam, align = alignFlag ? (alignLen > alignIndex ? aligns[alignIndex] : alignParam) : alignParam, }); jqgridParams.colNames.Add(str.formateTitle(titles)); widthIndex += 1; alignIndex += 1; } } return(jqgridParams); }
public static jqGridData <T> TojqGridData <T>(this T cls, int[] widths = null, bool act = false, List <FormateTitle> titles = null) where T : class { var obj = cls.GetType(); if (!obj.IsClass) { return(new jqGridData <T>()); } var jqgridParams = new jqGridData <T>(); bool flag = false; int len = 0; if (widths != null && widths.Length > 0) { len = widths.Length; flag = true; } int widthIndex = 0; int?widthParam = null; if (act) { jqgridParams.colNames.Add("act".formateTitle(titles)); jqgridParams.colModel.Add(new jqGridColModel() { name = "act", index = "act", width = 100 }); } obj.GetProperties() .ToList().ForEach(x => { var str = x.Name; jqgridParams.colModel.Add(new jqGridColModel() { name = str, index = str, width = flag ? (len > widthIndex ? widths[widthIndex] : widthParam) : widthParam }); var des = x.GetCustomAttributes(typeof(DescriptionAttribute), false); str = des.Length > 0 ? ((DescriptionAttribute)des[0]).Description : x.Name; jqgridParams.colNames.Add(str.formateTitle(titles)); widthIndex += 1; }); return(jqgridParams); }