Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I want to customize a jQuery datapicker in Drupal 7. I found code to set an event:

(function ($) {
Drupal.behaviors.only_monday = {
  attach: function (context, settings) {
   // $( ".selector" ).datepicker( "option", "showWeek", true );
   $.datepicker._defaults.beforeShowDay = onlyMonday;
    function onlyMonday ( date ) {
      if ( date.getDay() == 1 ) {
        return [true, 'ui-datepicker-days-cell'];
      }
      else {
        return [false, 'ui.datepicker-unselectable'];
      }
    }
  }
};
}(jQuery));

This works great, except for commented line. According to the jQuery docs that line (added by myself) should make visible the numbers of the weeks:

$( ".selector" ).datepicker( "option", "showWeek", true );

The context from within Drupal is probably different. But I have no knowledge of js :-(

In what way does this code for setting an option has to be adjusted ?

share|improve this question
Have you had a look at date module ? drupal.org/project/date (it has a jquery datepicker) – redben Nov 28 '11 at 13:38

migrated from stackoverflow.com Aug 5 '11 at 22:26

1 Answer

Your are working within a Drupal behavior that could be reapplied several times on the DOM tree, in order to avoid processing the same element several times you should limit the $($selector) call to the passed-ind `context and use a marker CSS class for already processed class. Something like

$('.selector:not(.only-monday-processed)').addClass('only-monday-processe').doSomethingwithTheElements();

To defines then set the datepicker options from Drupal server-side PHP, you need to store the selector(s) and options in the settings object received by your behavior attach function using the drupal_add_js() function or the #attached property of your form/render array element. Then your function can use the selector to apply the option to the element. Something like

In your module code, store the selector and options in settings:

drupal_add_js(array('myModule' => array('.selector' => array('showWeek' => TRUE))), 'setting');

In your JavaScript, for each stored selector, apply the datepicker options:

(function ($) {
  Drupal.behaviors.only_monday = {
    attach: function (context, settings) {
      var selector;
      if (typeof settings.myModule === 'object') {
        for (selector in settings.myModule) {
          if (settings.myModule.hasOwnProperty(selector)) {
            $(selector, context).filter(':not(.only-monday-processed)')
              .addClass('only-monday-processe')
              .datepicker('option', settings.myModule[selector]);
          }
        }
      }
    }
    // ...
  };
}(jQuery));
share|improve this answer
I cannot get this to work. I added the drupal_add_js() code to my module. I replaced my JS code with your JS code. In both cases I replaced 'myModule' with the name of my module. Like I said, I still do not see the numbers of the week, especially this line (and your remarks about it) do not have any meaning to me: $('.selector:not(.only-monday-processed)').addClass('only-monday-processe').doS‌​omethingwithTheElements();. I have not enough knowledge of JS to understand this. Is it just an explanation? It is also in your JS code which I added. Do I have to modify it? – Gijs Aug 5 '11 at 23:32
There's a small bug $('.selector:not(.only-monday-processed)').addClass('only-monday-processed').do‌​SomethingwithTheElements();, a d was missing in the addClass part ;) – tostinni Aug 6 '11 at 0:06
I added the d, but that did not make it work. – Gijs Aug 6 '11 at 11:57
Yep, this is part of the explanation and not part of the solution. The larger JavaScript code at the end of my answer contains similar behavior. Also, you shouldn't use an answer to reply to another answer. drupal.stackexchnage.com is not a forum. – Pierre Buyle Aug 6 '11 at 13:32
1  
I could not reply, only answer. Strange and confusing. So I had no choice. Maybe because my question was migrated while I still was not registered on Stackexchange. – Gijs Aug 6 '11 at 17:33
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.