如何扩展 jQueryUI datepicker 以接受附加参数?

发布于 2024-12-09 02:57:44 字数 990 浏览 0 评论 0 原文

我喜欢 jQueryUI datepicker 的可定制性足以允许按钮触发显示 datepicker,但不幸的是,它不允许您自定义自己的标记。

如果我有这样的标记:

<span>
    <input type="text" class="datepicker" />
</span>
<span>
    <button class="datepicker-trigger">Open</button>
</span>

为了让它在单击按钮时显示日期选择器,我必须实现以下代码:

$('.datepicker').datepicker({
    showOn:'none'
});
$('.datepicker-trigger').click( function() {
    $('.datepicker').datepicker('show');
});

这是上述代码的 jsFiddle: http://jsfiddle.net/P9Qmu/

这一切都很好,但我真正想做的是传递日期选择器功能一个附加参数,指定一个对象或选择器,指示哪个元素应触发显示。

例如,我真的很希望能够做到这一点:

$('.datepicker').datepicker({
    showOn:'none',
    trigger:'.datepicker-trigger'
});

我知道如何扩展 jQuery 来添加方法和创建插件,但我不确定如何(或者是否可能)修改现有函数中允许的参数。

有谁知道我如何扩展 $.datepicker 来实现我想要做的事情,或者至少为我指明正确的方向?任何帮助将不胜感激。

I like that jQueryUI datepicker is customizable enough to allow for a button to trigger showing the datepicker, but, unfortunately, it doesn't allow you to customize your own markup.

If I have markup like so:

<span>
    <input type="text" class="datepicker" />
</span>
<span>
    <button class="datepicker-trigger">Open</button>
</span>

In order to get it the datepicker to show when clicking the button, I'd have to implement the following code:

$('.datepicker').datepicker({
    showOn:'none'
});
$('.datepicker-trigger').click( function() {
    $('.datepicker').datepicker('show');
});

Here's a jsFiddle of the above code: http://jsfiddle.net/P9Qmu/

This is all well and good, but what I really want to do is pass the datepicker function an additional argument that specifies an object or selector indicating what element should trigger the showing.

For example, I'd really like to be able to do this:

$('.datepicker').datepicker({
    showOn:'none',
    trigger:'.datepicker-trigger'
});

I know how to extend jQuery to add methods and create plugins, but I'm not sure how (or if it's possible) to modify the allowed arguments in an existing function.

Does anyone know how I could extend $.datepicker to achieve what I'm looking to do or at least point me in the right direction? Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烂人 2024-12-16 02:57:44

怎么样:

var __picker = $.fn.datepicker;

$.fn.datepicker = function(options) {
    __picker.apply(this, [options]);
    var $self = this;

    if (options && options.trigger) {
        $(options.trigger).bind("click", function () {
            $self.datepicker("show");
        });
    }
}

用法:

$("#date").datepicker({
    trigger: "#button"
});

$("#date2").datepicker({
    trigger: "#button2"
});

示例: http://jsfiddle.net /gduhm/


或者,使用您自己的 jQuery 插件减少干扰:(

$.widget("ui.datepicker2", {
    _init: function() {
        var $el = this.element;
        $el.datepicker(this.options);

        if (this.options && this.options.trigger) {
            $(this.options.trigger).bind("click", function () {
                $el.datepicker("show");
            });
        }
    }
});

类似的用法,除了使用 datepicker2 或任何您命名的名称)

示例: http://jsfiddle.net/puVap/

How about:

var __picker = $.fn.datepicker;

$.fn.datepicker = function(options) {
    __picker.apply(this, [options]);
    var $self = this;

    if (options && options.trigger) {
        $(options.trigger).bind("click", function () {
            $self.datepicker("show");
        });
    }
}

Usage:

$("#date").datepicker({
    trigger: "#button"
});

$("#date2").datepicker({
    trigger: "#button2"
});

Example: http://jsfiddle.net/gduhm/


Or, less intrusively with your own jQuery plugin:

$.widget("ui.datepicker2", {
    _init: function() {
        var $el = this.element;
        $el.datepicker(this.options);

        if (this.options && this.options.trigger) {
            $(this.options.trigger).bind("click", function () {
                $el.datepicker("show");
            });
        }
    }
});

(Similar usage, except use datepicker2 or whatever you name it)

Example: http://jsfiddle.net/puVap/

岁吢 2024-12-16 02:57:44

您无法扩展,因为日期选择器不使用小部件工厂,因此您无法使用它来扩展它,但您可以重写方法并注入代码。有关 Jquery UI Datepicker 使用的方法的完整列表,请检查 datepicker.jsjquery-ui.js

您可以简单地在自定义 JS 文件中添加该代码,覆盖后您可以像往常一样调用日期选择器

$.datepicker._selectDay_old = $.datepicker._selectDay;
$.datepicker._selectDay = function(id, month, year, td) {
    this._selectDay_old(id, month, year, td);
    // Your code here
    console.log("Injected Custom Method");
}

// Call Datepicker after Injecting code
$("#datepicker").datepicker()

You cannot extend because the datepicker does not use the widget factory and thus you can't use it to extend it but you can override the methods and can inject your code. For complete list of methods that Jquery UI Datepicker uses check datepicker.js or jquery-ui.js

You can simple add that code in your Custom JS file and after overriding you can call datepicker as usual

$.datepicker._selectDay_old = $.datepicker._selectDay;
$.datepicker._selectDay = function(id, month, year, td) {
    this._selectDay_old(id, month, year, td);
    // Your code here
    console.log("Injected Custom Method");
}

// Call Datepicker after Injecting code
$("#datepicker").datepicker()
遮了一弯 2024-12-16 02:57:44

为了以防万一,要定义可调用方法,您必须遵循以下方法名称模式:

_<methodName>Datepicker(target, param1, param2,...,paramN)

如果您需要调用任何需要引用日历实例的内部方法,则必须执行以下操作:

var inst = this._getInst(target);
this._internalMethod(inst);

Just in case, to define a callable method you must follow this pattern for the method name:

_<methodName>Datepicker(target, param1, param2,...,paramN)

and if you need to call any internal method that requires a reference to the calendar instance, you must do:

var inst = this._getInst(target);
this._internalMethod(inst);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文