NinjaBat
NinjaBat

Reputation: 390

Odoo - Disable selection option in tree view for some rows based on condition

How to disable selection option in tree view for some rows based on condition ?

enter image description here

Upvotes: 1

Views: 1759

Answers (2)

Kenly
Kenly

Reputation: 26748

According to the documentation, there is no multi editing feature.

The checkbox selector is added in _renderSelector function of the ListRenderer widget, so you need to override it to disable the checkbox based on condition.

In the following example we disable the selection checkbox when the state (hard coded) field value is equal to draft.

Example:

odoo.define('web.CustomListRenderer', function (require) {
"use strict";

    var ListRenderer = require('web.ListRenderer');

    ListRenderer.include({
        _renderRow: function (record) {
            var self = this;
            var tr = this._super(record);
            tr.find("input[type='checkbox']").prop('disabled', record.data.state == 'draft');
            return tr;
        },
    });
});

You can pass an evaluation domain in the action context, then evaluate the domain in the _renderSelector function using the current record value.

We initialize the domain in init function and use the compute function to get the evaluation result:

odoo.define('web.CustomListRenderer', function (require) {
"use strict";
    var ListRenderer = require('web.ListRenderer');
    var Domain = require('web.Domain');

    ListRenderer.include({
        init: function (parent, state, params) {
            this._super.apply(this, arguments);
            if (state.context.selection_domain) {
                this.domain = new Domain(state.context.selection_domain, state.context);
            }
        },

        _renderRow: function (record) {
            var self = this;
            var tr = this._super(record);
            if (record.evalContext.selection_domain) {
                tr.find("input[type='checkbox']").prop('disabled', this.domain.compute(record.data));
            }
            return tr;
        },
    });
});

You need to add the selection_domain to the action context:

 {..., 'selection_domain': [('state', '=', 'draft')]}

Check the Assets Management documentation to see how to add the above JavaScript code to the web assets, you can also see an example in Adding files in an asset bundle section.

Upvotes: 0

WebMob Technologies
WebMob Technologies

Reputation: 39

Provide one unique id to each and every checkbox.

Use Jquery or javascript onLoad or document.ready method to disable the checkbox. create for loop and write simple disable code for your matching value.

document.getElementById("myCheck").disabled = true;

I m not sure about odoo. but I have a little bit of knowledge about odoo. you have to set a data field for this if dynamic data come from Database.

Upvotes: 0

Related Questions