.net使用layui框架下。绑定搜索下拉框-模糊查询

网上找了些,没一个全的,自己总结了下

页面部分:

<select id="selManageUserID" lay-search>                                                                 
                                </select>

JS部分:

$(function () {
            layui.use(['layer', 'form', 'laypage', 'laydate'], function () {
                layer = layui.layer;
                form = layui.form;
                laydate = layui.laydate;
                laydate.render({
                    elem: '#data'
                });
                form.on('select(depts)', function (data) {
                    var num = $("#depts").val();
                    GetGroups(num);
                });
            });
            LayuiCreateSelect("selManageUserID",<%=planInfo!=null?planInfo.ManageUser:"请选择" %>);//开始绑定搜索下拉框。传来的默认选中的选项。ManageUser是后台存的value值
          
           
        });

function LayuiCreateSelect(selectId, value) {//value  设置加载完成时所选定的值
            //数据请求
            $.ajax({
                type: "post",
                url: "AddReservePlan.aspx?act=GetManageUser",
                dataType: "json",
                //data: { search_date: search_date, groupName: groupName, DepartmentClassID: DepartmentClassID },
                success: function (r) {
                    //判断id是否有"#"
                    if (selectId.indexOf('#') != 0) {
                        selectId = '#' + selectId;
                    }
                    $(selectId).empty();//清空该元素
                    //创建option
                    var arr = r.list.split("|");
                    arr.forEach(function (item, index) {
                        $(selectId).append(item);
                    })
                    //使用layui下拉框的必要代码
                    layui.use('form', function () {
                        var form = layui.form;
                        //设置选中值
                        if (value != undefined && value != null && value != '') {
                            $(selectId).val(value);
                        }
                        form.render();//重载表单
                    });
                }, complete: function (r) {
                    layer.close(loadid);
                }
            });
        }

代码部分:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckLoginInfo();//检查登录
                if (!string.IsNullOrEmpty(Request["act"]))
                {
                    act = Request.QueryString["act"];
                }
                switch (act)
                {
                    case "GetManageUser":
                        GetManageUser();
                        break;
                    case "GetGroupList":
                        GetGroupList();
                        break;
                    case "AddPlan":
                        AddPlan();
                        break;
                    default:
                        PageLoad();
                        break;
                }
            }
        }


/// <summary>
        /// 获取预约人列表
        /// </summary>
        private void GetManageUser()
        {
            BLL.MyUser_YuyueBLL yuyueBLL = new BLL.MyUser_YuyueBLL();
            DataTable dtyuyue = yuyueBLL.GetList02("").Tables[0];
            StringBuilder sbyuyue = new StringBuilder();
            for (int i = 0; i < dtyuyue.Rows.Count; i++)
            {
                sbyuyue.Append("<option value='"+ dtyuyue.Rows[i]["userID"].ToString() + "'>"+ dtyuyue.Rows[i]["userName"].ToString() +"-"+ dtyuyue.Rows[i]["groupName"].ToString() + "</option>|");
            }
            string resultJson = JsonHelper.ObjectToJSON(sbyuyue);
            var rsp_obj = new
            {
                code = 200,
                list = sbyuyue.ToString().Substring(0, sbyuyue.ToString().Length)
            };
            Response.Write(JsonConvert.SerializeObject(rsp_obj));
            Response.End();
        }

 

posted @ 2020-06-09 16:18  离。  阅读(209)  评论(0编辑  收藏