【问题标题】:How to select different dates in date range with the same value in ngx-daterangepicker-material?如何在 ngx-daterangepicker-material 中选择具有相同值的日期范围内的不同日期?
【发布时间】:2021-07-26 22:00:41
【问题描述】:

我有两个预定义的日期范围,其中包含“当前月份”和“当前季度”等值。目前,这两个范围代表相同的日期值(因为刚刚开始的季度和月份也是如此)。当我想选择“当前季度”时,它会自动选择“当前月份”。无法选择“当前季度”,该按钮未激活。 它看起来不像是用户友好的行为。 是否有任何配置允许用户选择具有相同值的不同日期范围?

【问题讨论】:

  • “当前月份”和“当前季度”的结束日期不应该不同,使它们成为唯一且可选择的范围吗?如果您为日期范围选择器提供代码,可能会更容易提供帮助
  • 其实结束日期也是一样的。因为结束日期是今天。实际上,代码中没有任何具体内容,该问题在所有代码变体中重现。顺便说一句,我使用的是 ngx-daterangepicker-material 的 4.0.1 版本
  • 一个月和一个季度的开始和结束日期可能相同吗?除非您的意思是“本季度至今”和“本月至今”?
  • 是的,你说得对。我的意思是“季度至今”和“本月至今”

标签: angular datepicker bootstrap-datepicker ngx-daterangepicker-material


【解决方案1】:

不幸的是,ngx-daterangepicker-material 的设计目的不是允许具有完全相同日期的多个范围。我能够重现您在此处看到的症状: Stackblitz

看看你正在使用的包的代码,你会在 calculateChosenLabel() 方法中看到:


    calculateChosenLabel () {
            if (!this.locale || !this.locale.separator) {
                this._buildLocale();
            }
            let customRange = true;
            let i = 0;
            if (this.rangesArray.length > 0) {
                for (const range in this.ranges) {
                    if (this.ranges[range]) {
                        if (this.timePicker) {
                            const format = this.timePickerSeconds ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD HH:mm';
                            // ignore times when comparing dates if time picker seconds is not enabled
                          if (this.startDate.format(format) === this.ranges[range][0].format(format)
                            && this.endDate.format(format) === this.ranges[range][1].format(format)) {
                                customRange = false;
                                this.chosenRange = this.rangesArray[i];
                                break;
                            }
                        } else {
                            // ignore times when comparing dates if time picker is not enabled
                            if (this.startDate.format('YYYY-MM-DD') === this.ranges[range][0].format('YYYY-MM-DD')
                              && this.endDate.format('YYYY-MM-DD') === this.ranges[range][1].format('YYYY-MM-DD')) {
                                customRange = false;
                                this.chosenRange = this.rangesArray[i];
                                break;
                            }
                        }
                        i++;
                    }
                }

那 1. 它遍历您指定的范围,并匹配针对范围选择的开始日期和结束日期。

而且 2. 除非您显示时间选择器,否则它会忽略时间值,因此即使尝试使用时间偏移来破解它也行不通

目前没有标志或选项可以覆盖它。

看起来您最好的选择是 Open a new issue 请求使用唯一的范围标识符,而不仅仅是比较日期

或者导入项目的源代码,根据自己的目的进行修改

【讨论】:

  • 谢谢!它帮助了我。我有一个临时解决方案,这很丑陋。我将打开一个新问题
猜你喜欢
  • 2020-04-26
  • 1970-01-01
  • 2019-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-05
相关资源
最近更新 更多