如何将方法应用于AirDatepicker在我的自定义js文件中?

huangapple go评论62阅读模式
英文:

How to apply method to AirDatepicker in my custom js file?

问题

我在app.js文件中定义了一个AirDatepicker v3.3.5,如下:

window.dateRangePicker = new AirDatepicker('.date-range', {
    range: true,
    maxDate: new Date(),
    multipleDatesSeparator: ' - '
});

在https://air-datepicker.com/examples文档中查看,
我需要在我的custom form.js中添加onSelect和update方法,如下:

dpMin = new AirDatepicker('#el1', {
    onSelect({date}) {
        dpMax.update({
            minDate: date
        })
    }
})

但是我失败了,因为我尝试了以下代码:

window.dateRangePicker.onSelect({date}) {
    ...
};

但是我收到了错误消息:

期望新行或分号

哪种语法是有效的?

英文:

I have a AirDatepicker v3.3.5 defined in app.js file as :

window.dateRangePicker = new AirDatepicker('.date-range', {
    range: true,
    maxDate: new Date(),
    multipleDatesSeparator: ' - '
});

Looking at https://air-datepicker.com/examples docs
I need in my custom form.js to add to apply onSelect and update methods like

dpMin = new AirDatepicker('#el1', {
    onSelect({date}) {
        dpMax.update({
            minDate: date
        })
    }
})

But I failed it, as I tried :

window.dateRangePicker.onSelect({date}) {
    ...

};

But I got error :

Expecting new line or semicolon

Which syntax is valid ?

答案1

得分: 1

根据评论中提到的内容,文档表示我们可以在初始化时将onSelect方法传递给选项,但在创建的对象本身上并没有onSelect方法。

当我检查并跟踪分配的对象时,我发现我们可以利用作为创建对象的一部分公开的函数。我们可以在初始化后分配该方法,如下所示。

您可以尝试类似以下的内容。

window.dateRangePicker.opts.onSelect = ({ date }) => {
  console.log(date);
}
英文:

As mentioned in the comments, Document says we can pass the onSelect method inside the options at the time of initialisation but they don't have onSelect method on that created object itself.

When i checked and traced the assigned object i find that we can utilise the functions exposed as part of created object. We can assign the method after initialisation like below.

You can try something like this.

window.dateRangePicker.opts.onSelect = ({date}) => {
  console.log(date);
}

huangapple
  • 本文由 发表于 2023年7月11日 13:14:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定