0

I would like to create a module which controls edit button on certain conditions. I tried the following code in js but I got no effect. So I would like to know how to extend a function in js.

formView.include({
    init:function(){

        var edits = new Model('sale.order');
        edits.query(['validity_date']);
        console.log(validity_date)
        },
    on_button_edit: function(){
        this._super();

1 Answer 1

1

You can write something like this in the js file. I wrote some examples to help you.

openerp.custom_edit_button = function (instance) {
    var _t = instance.web._t;   

    instance.web.FormView.include({
        init: function() {
            console.log('JS loaded')  
            this._super.apply(this, arguments);   
        },  

        to_edit_mode: function(){
            // examples of useful methods
            var field_values = this.get_fields_values();
            var ids = this.get_selected_ids();
            var id = field_values['id'];
            var date = field_values['date'];
            var model = this.model;

            console.log(field_values)
            console.log(ids)
            console.log(id)
            console.log(model)
            console.log(date)
            console.log(Date.today())

            date_to_compare = new Date(date);
            console.log(date_to_compare)

            if(date_to_compare < Date.today()){
                error = this.error;
                var QWeb = instance.web.qweb;
                var dialog = new instance.web.Dialog(this, { 
                    title: _t("Set new expiry date"), 
                    width: '30%', 
                    size: 'medium',
                    /*dialogClass: 'oe_act_window',*/
                    buttons: [ 
                          { text: _t("OK"), click: function() { self.set_new_expiry_date(); }}, 
                          { text: _t("Close"), click: function() { dialog.close(); return; }
                          },                          
                      ],
                }, QWeb.render('custom_edit_button.expiry_date_form', {error: error})).open();

            }

            this._super();
        }
    });
}

So, if the expiry date is in the past, the form is going to appear to change it. You must define the method set_new_expiry_date as well. And on the other hand you must add this template, or something similar to show the form. Add the file in the qweb section of your __openerp__.py

<templates xml:space="preserve">
    <div t-name="custom_edit_button.expiry_date_form" >
        <div class="form-group">
            <label for="date" class="control-label">New expiry date:</label>
            <input name="date" class="form-control"/>
        </div>   
    </div>
</templates>

Notice that the name of my module is custom_edit_button in the example

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for replying what i am trying is Based on existing expiry date(validity_date) field on sale.order, enable edit mode only if the expiry date exceeds current date .
Mmm I think you can't do that because you don't have the id of the record when the edit button appears. Another option is to use the attrs attribute in each field or group or fields.
the problem here is i can't update date.My idea is,if current date exceeds, then open a wizard to enter a new expiry date. The edit button is js (addons/web/static/src/js/form_view.js)
Are you sure that you can't change the date? If you use the attrs attribute you can update the date only if the condition is satisfied
formView.include({ on_button_edit: function() { return this.to_edit_mode(); }, to_edit_mode: function() { this._super(); } });
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.