【问题标题】:How to show daterangepicker all time?如何一直显示日期范围选择器?
【发布时间】:2017-01-22 08:10:24
【问题描述】:

我需要一直显示daterangepicker - 而不是像默认的弹出窗口。有可能吗?

【问题讨论】:

    标签: datepicker daterangepicker bootstrap-daterangepicker


    【解决方案1】:

    你可以做类似的事情 -

    ranges: {
               'All time':[null,null],
               ....
    

    编辑你的

    daterangepicker.js

    在第 1532 行

    updateElement: function() {
                if (this.element.is('input') && this.autoUpdateInput) {
                    var newValue = this.startDate.format(this.locale.format);
                    if (!this.singleDatePicker) {
                        newValue += this.locale.separator + this.endDate.format(this.locale.format);
                    }
                    if(newValue == "Invalid date - Invalid date"){
                        this.element.val("All Time").trigger('change');
                    }
                    else if (newValue !== this.element.val()) {
                        this.element.val(newValue).trigger('change');
                    }
                }
            }
    

    如果有任何疑问,请告诉我。

    【讨论】:

    • 编辑第三方源文件通常是个坏主意。
    【解决方案2】:

    我的解决方案是

    $(function() {
    
        var start = moment('1970-01-01');
        var end = moment();
    
        function cb(start, end) {
            if(start.format('DD/MM/YYYY') == '01/01/1970') {
                $('#reportrange span').html('All time');
            }else {
                $('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
            }
        }
    
        $('#reportrange').daterangepicker({
            startDate: start,
            endDate: end,
            ranges: {
               'All time': [moment('1970-01-01'), moment()],
               '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);
    
        cb(start, end);
    
    });
    

    【讨论】:

    • 我会使用'All Time': [null, null],然后在cb() 中使用start.isValid()end.isValid() 检查日期。
    • @cheack [null, null] 有效吗?似乎它在插件的内部函数中引发错误,如clickRangecalculateChosenLabel
    【解决方案3】:
    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')],
                    'This Year': [moment().startOf('year'), moment()],
                    'Last Year': [moment().subtract(1, 'year').add(1,'day'), moment()],
                    'All Time': ['01/01/2018', moment()],
                }
    

    【讨论】:

      【解决方案4】:
      ranges: {
          'Today': [moment(), moment()],
          'This Week': [moment().startOf('week'), moment()],
          'This Month': [moment().startOf('month'), moment().endOf('month')],
          'All Time': [null, null]
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-19
        • 1970-01-01
        • 2020-06-11
        • 1970-01-01
        • 2017-05-15
        • 1970-01-01
        相关资源
        最近更新 更多