4

This is with Odoo 10 and the default bootstrap-datetimepicker.

I have a field in my view that has a "Start of event" datetime. I'd like the date picker that shows up to work in 5 minute intervals (minuteStepping: 5) and to show the time picker along with the date picker (sideBySide: true).

I've confirmed that this works as I want it to by editing addons/web/static/src/js/widgets/date_picker.js manually.

However, I'd prefer to just give the two options I want to change as a parameter to my <field ..> definition under my <form> tag in the view XML. The source Widget accepts an options parameter in its init method that it extends to end up with the final options object, but I've been unable to insert my configuration options into this object.

I tried giving it as <field ... options="{...}" and as .. t-field-options='..', but I'm guessing the latter won't work since I'm not in a qweb context in my view, and the first one isn't read by widgets.

Is there any way I can do this without creating a new widget? (and hopefully without subclassing or extending the existing widget, but keep it as a pure view configuration option instead)?

MatsLindh
  • 49,529
  • 4
  • 53
  • 84

1 Answers1

2

You can see a great example in this module. https://github.com/OCA/web/tree/10.0/web_m2x_options

In the file. static/src/js/form.js.

The module has overridden the fields Many2one to add different options can be set in the XML declaration of field.

Example : <field name="partner_id" options="{'search_more':true}" />

In this example. The search more button is visible in all cases.

You can use the logic of this module as a base of your widget extension.

Installation:

In a First time, you must download the Github repository.

https://github.com/jo541/web

Select the branch 10.0. In the repository, there are a named module "web_widget_datepicker_options". This module gives you the possibility to specify any options as you want for a specific field.

After download and install the module on Odoo. You needing to reload the cache of your browser to be sure.

Modification:

Now you can modify your form view. For an example, I will use the view form sale.order.

In the form view sale.order, you have a field "date_order". If you would like to have the time step 5 by 5.

<field name="date_order" options="{'datepicker':{'minuteStepping': 5}}" attrs="{'invisible': [('state', 'in', ['sale', 'done', 'cancel'])]}"/>

All the options in the dict of the key datepicker will be adding to the option of the bootstrap datepicker.

jo541
  • 1,155
  • 6
  • 10
  • Thank you for the answer! But the question is how/if I can pass custom settings to the datepicker, not how I can give options to my own, custom module - which is the thing I want to avoid. Writing my own date picker widget (even just to sub class the existing one) to change just its configuration is what I don't want to do. – MatsLindh Oct 12 '17 at 10:17
  • I have found an OCA module to add all possible option in datepicker. But one problem is, the module doesn't upgrade for version 10. I work on it and you can found it on my github. https://github.com/jo541/web/tree/10.0/web_widget_datepicker_options The original is https://github.com/OCA/web/tree/10.0/web_widget_datepicker_options PS: Select the branch 10. – jo541 Oct 12 '17 at 13:49
  • Thanks! But that's using the old datepicker and not the new, bootstrap based one (I also found that module earlier). If someone has already written a widget for using the new datepicker with configurable options (and as I posted in the question - the JS file _does_ extend a configuration array already, but I'm not sure how - if possible - to get any values into it when it's created), I'd be interested in seeing that. The main goal is to do with what's already in Odoo so we don't have to keep upgrading modules for new versions (.. like in that case for jQuery-datepicker for 9) – MatsLindh Oct 12 '17 at 13:53
  • 2
    My version of the module working with datepicker bootstrap, not jquery datepicker. I completely rewrite the code. You can easier add the possibility to use the minuteStepping by 5. You only need to install my version of the module and add the option on the field like this (options="{'datepicker':{'minuteStepping': 5}}" ) – jo541 Oct 12 '17 at 14:10
  • Cool! Nice work. I'll try it out when I'm at the office tomorrow! Do you want to update your answer with instructions and links to the module as well, so I can accept it as the canonical answer to the question? – MatsLindh Oct 12 '17 at 16:43
  • 1
    Yes I'll update my answer tomorrow. With all information to install the module and add option on the field. – jo541 Oct 12 '17 at 20:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156627/discussion-between-jo541-and-matslindh). – jo541 Oct 13 '17 at 07:37