1、 日期搜索:ui
<div class="layui-inline layui-inline-31"> <label class="layui-form-label">建立時間</label> <div class="layui-input-block"> <input type="text" class="layui-input" id="createTime-laydate-range" autocomplete="off" placeholder=" - "> <input type="hidden" name="createTimeStart"> <input type="hidden" name="createTimeEnd"> </div> </div>
若在當前頁的搜索:this
admin.renderDateForSearch("createTime")
如果呼出頁面的搜索:spa
var search_field = { }; top.layui.admin.popupRight({ id: 'LAY_workTaskLogPopupLayer' ,area: '350px' ,success: function(layero,index){ top.layui.view(this.id).render('task/workTaskLogSearch',$.extend(search_field,{ logTypeArr:logTypeArr, statusArr:statusArr, })).done( function () { top.layui.form.render(); admin.renderDateForSearch("createTime",search_field); } ); } });
自定義封裝方法: admin.renderDateForSearch:code
/** * 用於日期範圍的搜索 * cjianquan 2020/3/19 * @param name 搜索的表單值名稱,即對應後臺Bean的屬性 * @param search_field 搜索字段 * @param type 日期類型 */ admin.renderDateForSearch = function(name,search_field,type){ var val = ''; type = type?type:'date'; if(search_field && search_field[name+"Start"] && search_field[name+"End"]){ val+=search_field[name+"Start"]+" ~ "+search_field[name+"End"]; } var layui_laydate = search_field?top.layui.laydate:layui.laydate; var layui$ = search_field?top.layui.$:layui.$; layui_laydate.render({ elem: '#'+name+'-laydate-range' , type:type, trigger: 'click', range: '~', value:val, done: function(value, date, endDate){ if(!value){ layui$(".layui-form-item [name="+name+"Start]").val(null); layui$(".layui-form-item [name="+name+"End]").val(null); if(search_field){ search_field[name+"Start"] = ''; search_field[name+"End"] = ''; } return; } var dateStr1= value.split(" ~ ")[0] var dateStr2= value.split(" ~ ")[1] layui$(".layui-form-item [name="+name+"Start]").val(dateStr1); layui$(".layui-form-item [name="+name+"End]").val(dateStr2); if(search_field){ search_field[name+"Start"] = dateStr1; search_field[name+"End"] = dateStr2; } } }); }
2、單日期選擇:orm
<div class="layui-form-item layui-form-item-45" > <label class="layui-form-label">建立時間<span style="color:red">*</span></label> <div class="layui-input-block"> <input type="text" class="layui-input" name="createTime" id="createTime-laydate" autocomplete="off" placeholder=""> </div> </div>
admin.renderDate("createTime",formData);
自定義封裝方法:admin.renderDateinput
/** * 日期選擇的封裝 * cjianquan 2020/3/20 * @param key 日期元素的ID(前綴) * @param formData 表單數據 * @param type * @param defaultDate 默認日期 */ admin.renderDate = function (key,formData,type,defaultDate) { defaultDate = defaultDate?defaultDate:null; type = type?type:'date'; var format = 'yyyy-MM-dd'; if(type=='year'){ format = 'yyyy'; }else if(type=='month'){ format = 'yyyy-MM'; }else if(type=='time'){ format = 'HH:mm:ss'; }else if(type=='datetime'){ format = 'yyyy-MM-dd HH:mm:ss'; } format = format?format:'yyyy-MM-dd'; var date = formData?(formData[key]?formData[key]:defaultDate):defaultDate; if(date){ var dateStr = layui.util.toDateString(date,format) layui.laydate.render({ elem: '#'+key+'-laydate', trigger: 'click', type: type, value:dateStr }); }else{ layui.laydate.render({ elem: '#'+key+'-laydate', trigger: 'click', type: type, }); } }
說明:
當同一個頁面中,多個laydate, 會有選擇日期時 閃爍消失的現象:
添加屬性:it
trigger: 'click',
便可。。。io