上次的思路感觉不是很合理,这次做了一些修改
这是上篇文章的链接:https://blog.csdn.net/weixin_41987365/article/details/106199859
这次修改后,可以直接在时间搜索框选择日期搜索,比上次的好很多。
补充:才发现这个是结合了bootstrap才能使用的,需要引入bootstrap.css和moment.js 否则会报错
如图所示,可以直接选择日期区间。
js
$(function(){
$("input[name='CustomerSearch[ctime]']").daterangepicker({
startDate : moment(),
endDate : moment(),
maxDate : moment(),
showWeekNumbers : true,
timePicker : true,
timePicker24Hour: true,
timePickerSeconds: true,
showCustomRangeLabel:true,
opens: "left",
ranges : {
'最近1小时': [moment().subtract(1,'hours'), moment()],
'今日': [moment().startOf('day'), moment()],
'昨日': [moment().subtract(1,'days').startOf('day'), moment().subtract(1,'days').endOf('day')],
'最近7日': [moment().subtract(6,'days'), moment()],
'最近30日': [moment().subtract(29,'days'), moment()],
'本月': [moment().startOf("month"),moment().endOf("month")],
'上个月': [moment().subtract(1,"month").startOf("month"),moment().subtract(1,"month").endOf("month")]
},
locale : {
format: "YYYY-MM-DD",
separator: " 至 ",
applyLabel: "确认",
cancelLabel:"取消",
fromLabel: "起始时间",
toLabel: "结束时间",
customRangeLabel : "自定义时间",
daysOfWeek : [
"一","二","三","四","五","六","日",
],
monthNames : [
"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月",
]
},
});
var ctime_params = getTimeParams();
if(ctime_params){
$("input[name='CustomerSearch[ctime]']").val(ctime_params);
}else{
$("input[name='CustomerSearch[ctime]']").val('');
}
$("input[name='CustomerSearch[ctime]']").parent().css("position", "relative")
$("input[name='CustomerSearch[ctime]']").parent().append('<i id="clear_time" title="清除时间" class="glyphicon glyphicon-calendar" style="position: absolute;right: 14px;top: 18px;color: #909090;cursor: pointer;"></i>')
$("body").on("click", "#clear_time", function(){
$("input[name='CustomerSearch[ctime]']").val('');
var e = jQuery.Event("keydown");
e.keyCode = 13;
$("input[name='CustomerSearch[ctime]']").trigger(e);
});
});
function getTimeParams()
{
var query = window.location.search.substring(1);
var query_str = decodeURIComponent(query);
var ctime_index = query_str.indexOf("CustomerSearch[ctime]");
var ctime_index_end = query_str.indexOf("&CustomerSearch[account_status]");
if(ctime_index > 0){
var ctime_str = query_str.substring(ctime_index+22, ctime_index_end);
ctime_str = ctime_str.replace(/\+/g, " ");
return ctime_str;
}else{
return(false);
}
}
最后这个方法就是截取url上的时间显示在搜索框里。