yii2.0 时间区间查询二

9 篇文章 0 订阅

上次的思路感觉不是很合理,这次做了一些修改
这是上篇文章的链接: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上的时间显示在搜索框里。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值