Ejemplo n.º 1
0
        /// <summary>
        /// 将数据的List转化为下拉菜单数据List
        /// </summary>
        /// <typeparam name="T">源数据类</typeparam>
        /// <param name="self">源数据List</param>
        /// <param name="textField">指向text字段的表达式</param>
        /// <param name="valueField">指向value字段的表达式</param>
        /// <param name="selectedCondition">默认被选中的条件</param>
        /// <returns>下拉菜单数据List</returns>
        public static List <ComboSelectListItem> ToListItems <T>(this List <T> self
                                                                 , Expression <Func <T, object> > textField
                                                                 , Expression <Func <T, object> > valueField
                                                                 , Expression <Func <T, bool> > selectedCondition = null)
        {
            var rv = new List <ComboSelectListItem>();

            if (self != null)
            {
                //循环列表中的数据
                foreach (var item in self)
                {
                    //获取textField的值作为text
                    string text = textField.Compile().Invoke(item).ToString();
                    //获取valueField的值作为value
                    string value = valueField.Compile().Invoke(item).ToString();
                    //添加到下拉菜单List中
                    ComboSelectListItem li = new ComboSelectListItem
                    {
                        Text  = text,
                        Value = value
                    };
                    //如果有默认选择的条件,则将当前数据带入到判断表达式中,如果返回true,则将下拉数据的selected属性设为true
                    if (selectedCondition != null)
                    {
                        if (selectedCondition.Compile().Invoke(item))
                        {
                            li.Selected = true;
                        }
                    }
                    rv.Add(li);
                }
            }
            return(rv);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 将枚举类型转化为下拉列表
        /// </summary>
        /// <param name="self">枚举类型</param>
        /// <param name="value">value</param>
        /// <param name="pleaseSelect">pleaseSelect</param>
        /// <returns>下拉菜单数据列表</returns>
        public static List <ComboSelectListItem> ToListItems(this Type self, object value = null)
        {
            string[] names  = null;
            Array    values = null;

            //如果是枚举
            if (self.IsEnum)
            {
                names  = Enum.GetNames(self);
                values = Enum.GetValues(self);
            }
            //如果是nullable的枚举
            if (self.IsGenericType && self.GenericTypeArguments[0].IsEnum)
            {
                names  = Enum.GetNames(self.GenericTypeArguments[0]);
                values = Enum.GetValues(self.GenericTypeArguments[0]);
            }
            //生成下拉菜单数据
            List <ComboSelectListItem> rv = new List <ComboSelectListItem>();

            if (names != null)
            {
                for (int i = 0; i < names.Length; i++)
                {
                    var name    = names[i];
                    var newitem = new ComboSelectListItem {
                        Text = PropertyHelper.GetEnumDisplayName(self, name), Value = ((int)values.GetValue(i)).ToString()
                    };
                    if (value is string)
                    {
                        if (value != null)
                        {
                            string v  = value.ToString();
                            var    vs = v.Split(',');
                            foreach (var item in vs)
                            {
                                if (item != null && (newitem.Value.ToString() == item.ToString()) || name == item.ToString())
                                {
                                    newitem.Selected = true;
                                    break;
                                }
                            }
                        }
                    }
                    else if (value is IEnumerable it)
                    {
                        foreach (var item in it)
                        {
                            if (item != null && name == item.ToString())
                            {
                                newitem.Selected = true;
                                break;
                            }
                        }
                    }
                    else
                    {
                        if (value != null && name == value.ToString())
                        {
                            newitem.Selected = true;
                        }
                    }
                    rv.Add(newitem);
                }
            }
            return(rv);
        }