简体   繁体   English

单击按钮后保留(保留)jQuery DateRangePicker 的选定日期

[英]Retain (Preserve) Selected date of jQuery DateRangePicker after Button Clicked

DateRangePicker 和“搜索”按钮

I have a daterangepicker and a 'search' button,我有一个 daterangepicker 和一个“搜索”按钮,

I set the default date as today.我将默认日期设置为今天。

But when the user has chosen the date range, and the 'search' button is clicked, the page refresh but the daterangepicker remain the default date.但是,当用户选择了日期范围并单击“搜索”按钮时,页面会刷新,但 daterangepicker 仍然是默认日期。

Expectation outcome:预期结果:

If the user chose the daterange of 17 March 2021 - 20 March 2021, the daterangepicker should show 17 March 2021 - 20 March 2021 instead of the default date, 17 March 2021 - 17 March 2021.如果用户选择 2021 年 3 月 17 日 - 2021 年 3 月 20 日的日期范围,则日期范围选择器应显示 2021 年 3 月 17 日 - 2021 年 3 月 20 日,而不是默认日期 2021 年 3 月 17 日 - 2021 年 3 月 17 日。

My daterangepicker code:我的日期范围选择器代码:

                                                        <script type="text/javascript">
                                                    $(document).ready(function() {
                                                        
                                                        var start = moment();
                                                        var end = moment();

                                                $('#reportrange').daterangepicker({
                                                    startDate: start,
                                                    endDate: end,
                                                    ranges: {
                                                    'Today': [moment(), moment()],
                                                    'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                                                    'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                                                    'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                                                    'This Month': [moment().startOf('month'), moment().endOf('month')],
                                                    'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
                                                    }
                                                }, cb);

                                                function cb(start, end) {
                                                    $('#reportrange span').html(start.format('DD MMMM YYYY') + ' - ' + end.format('DD MMMM YYYY'));
                                                    $('#to').val(start.format('YYYY-MM-DD'));
                                                    $('#from').val(end.format('YYYY-MM-DD'));       
                                                }

                                                cb(start, end);
                                            
                                                
                                            });
                                             </script>

My 'search' button:我的“搜索”按钮:

<input class="btn btn-info btn-block" type="submit" name="btnTotal" id="btnTotal" value="Search Total"/>

Please help on this, thanks in advance.请对此提供帮助,在此先感谢。

You can use localStorage ( https://developer.mozilla.org/fr/docs/Web/API/Window/localStorage ) or sessionStorage ( https://developer.mozilla.org/fr/docs/Web/API/Window/sessionStorage ) to keep your string in memory by detecting change.您可以使用 localStorage ( https://developer.mozilla.org/fr/docs/Web/API/Window/localStorage ) 或 sessionStorage ( https://developer.mozilla.org/fr/docs/Web/API/Window/ sessionStorage )通过检测更改将您的字符串保存在 memory 中。 In your case sessionStorage would probably be better.在你的情况下 sessionStorage 可能会更好。

$('#reportrange').change(function(){
    sessionStorage.setItem('dateRange', $(this).val());
});

And after that you just need to update the value when you load the page之后,您只需要在加载页面时更新值

$(function(){
   if(sessionStorage.getItem('dateRange') != null){
       $('#reportrange').val(sessionStorage.getItem('dateRange'));
   }
});

I use JQuery but you can do it with simple Javascript.我使用 JQuery 但您可以使用简单的 Javascript 来完成。

Another solution would be to never reload page and just to update the container you use to display datas.另一种解决方案是永远不要重新加载页面,而只是更新用于显示数据的容器。 ( using Ajax for example ) (以 Ajax 为例)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM