MiniUi綁定mini-combobox下拉框


一:最先開始后台使用json字符串綁定combobox

[{"id":1,"value":"是","text":"是"},{"id":0,"value":"否","text":"否"}]

然后我忘記json字符串的格式了,id屬性沒有加"" ,combobox一直綁定不上數據,而且請注意text屬性是combobox的顯示值,value屬性不是顯示值

二:combobox的前端界面是

<input id="InUse" class="mini-combobox" url="@Url.Action("GetInUse")" style="width:150px;" textfield="text"   shownullitem="true" allowinput="true" />

而action里返回JsonResult或者string格式都可以

public JsonResult GetInUse()
        {
            List<JsonData> list = new List<JsonData>();
            list.Add(new JsonData() { id = 1, text = "" });
            list.Add(new JsonData() { id = 0, text = "" });

            return Json(list, JsonRequestBehavior.AllowGet);//這里使用的是get請求
        }

        public string GetInUse()
        {
            List<JsonData> list = new List<JsonData>();
            list.Add(new JsonData() { id = 1, text = "" });
            list.Add(new JsonData() { id = 0, text = "" });

            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            return  jsonSerializer.Serialize(list);
        }

三:除了使用MVC提供的 url="@Url.Action("GetInUse")" 的方式,還可以在頁面加載的時候使用JavaScript為下拉框賦值

<script type="text/javascript">

    //方法一
    var InUse = mini.get("InUse");
    $.ajax({
        url: '@Url.Action("GetInUse")',
        type: 'get',
        contentType: "application/json",
        success: function (jsonData) {
            if (jsonData) {
                InUse.load(jsonData);
            }
        }
    });
    //方法二
    $(document).ready(function () {
        var jsonData = [{ 'id': '1', 'text': '' }, { 'id': '0', 'text': '' }];
        mini.get("InUse").load(jsonData);
    })
</script>

 四:使用miniui直接在input標簽的date屬性里設置json數據(過了很多天后補的一種方法,未測試,如果json格式不對,嘗試給id,text屬性加單引號)

<input id="InUse" name="InUse" class="mini-combobox" style="width: 200px;" textfield="text"
               valuefield="id" emptytext="請選擇..."
               allowinput="false" shownullitem="true" nullitemtext="請選擇..."
               data="[{id:'1',text:'是'},{id:'0',text:'否'}]" />

 五:action里讀取枚舉數據

public JsonResult GetStatusEnum(bool isAll)
        {
            try
            {
                var list = CommonUtil.GetEnumList<PlanTypeEnum>();
                var returnList = list.Select(item => new DictionaryEntity
                {
                    Key = item.Key,
                    Value = item.Value
                }).ToList();
                if (isAll)
                {
                    returnList.Insert(0, new DictionaryEntity { Key = -1, Value = "" });
                }
                return Json(returnList, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
/// <summary>
        /// 把枚舉描述和值規則拼接字符串
        /// </summary>
        /// <typeparam name="T">枚舉</typeparam>
        /// <returns>枚舉值,枚舉描述;枚舉值,枚舉描述;枚舉值,枚舉描述</returns>
        public static IList<DictionaryEntry> GetEnumList<T>()
        {
            List<DictionaryEntry> list = new List<DictionaryEntry>();
            Dictionary<int, string> dic = GetDictionary(typeof(T));
            DictionaryEntry entity;
            foreach (var key in dic.Keys)
            {
                entity = new DictionaryEntry();
                entity.Key = key;
                entity.Value = dic[key];
                list.Add(entity);
            }
            return list;
        }
/// <summary>
        /// 獲取枚舉值、描述列表
        /// </summary>
        /// <param name="enumType">枚舉類型</param>                
        /// <returns>
        /// 返回枚舉值、描述列表
        /// </returns>
        private static Dictionary<int, string> GetDictionary(Type enumType)
        {
            Dictionary<int, string> dic = new Dictionary<int, string>();
            foreach (int enumValue in Enum.GetValues(enumType))
            {
                dic.Add(enumValue, string.Empty);
                FieldInfo fieldinfo = enumType.GetField(Enum.GetName(enumType, enumValue));
                if (fieldinfo == null)
                {
                    return null;
                }
                Object[] objs = fieldinfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (objs.Length != 0)
                {
                    DescriptionAttribute da = (DescriptionAttribute)objs[0];
                    dic[enumValue] = da.Description;
                }
            }
            return dic;
        }

前台界面

//獲取Combobox控件值,統一調用
function GetCombobox(ctrlId, url, isAll, isAsync, isSelect, callback) {
    /// <signature>
    ///<summary>加載下拉框控件</summary>
    ///<param name="ctrlId" type="String">要綁定數據的控件ID</param>
    ///<param name="url" type="String">數據請求地址ID</param>
    ///<param name="isAll" type="String">是否包含全部標識</param>
    ///<param name="isAsync" type="String">是否異步</param>
    ///<param name="isSelect" type="String">是否選中</param>
    ///<param name="callback" type="function">回調函數</param>
    /// </signature>
    if (isAsync == undefined)
        isAsync = false;
    if (isAll == undefined)
        isAll = false;
    var cbox = mini.get(ctrlId);
    $.ajax({
        url: url,
        type: "post",
        async: isAsync,
        data: { isAll: isAll },
        success: function (text) {
            if (cbox != undefined) {
                cbox.setData(text);
                if (isSelect === undefined || isSelect === true) {
                    cbox.select(0);
                }
                //cbox.doValueChanged();
            } else {
                alert('獲取下拉框對象為空');
            }
            if (callback) callback();
        },
        error: function (text) {
            var jsontext = mini.decode(text.responseText);
            alert(jsontext.Message);
            return;
        }
    });
}
$(function () {
            //計划類型
            GetCombobox('PlanTypeId', '@Url.Action("GetPlanTypeEnum")', false);
        })
計划類型:
<input id="PlanTypeId" name="PlanTypeId" class="mini-combobox comboboxWidth" style="width: 130px" valuefield="Key" textfield="Value" onblur="checkComboboxblur('PlanTypeId')" allowinput="true" valuefromselect="true" required="true" />

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM