diff --git a/web_tree_dynamic_colored_field/readme/CONTRIBUTORS.rst b/web_tree_dynamic_colored_field/readme/CONTRIBUTORS.rst index 69d39b5c2..12bffa38d 100644 --- a/web_tree_dynamic_colored_field/readme/CONTRIBUTORS.rst +++ b/web_tree_dynamic_colored_field/readme/CONTRIBUTORS.rst @@ -3,3 +3,4 @@ * Artem Kostyuk * Guewen Baconnier * Phuc Tran Thanh +* Sylvain LE GAL diff --git a/web_tree_dynamic_colored_field/readme/USAGE.rst b/web_tree_dynamic_colored_field/readme/USAGE.rst index 2be4517c5..251009474 100644 --- a/web_tree_dynamic_colored_field/readme/USAGE.rst +++ b/web_tree_dynamic_colored_field/readme/USAGE.rst @@ -26,6 +26,21 @@ With this example, column which renders 'name' field will have its text colored in white on a customer records. +* In the tree view declaration, use ``options='"color_field": "my_color"'`` attribute in the ``tree`` tag:: + + ... + + + ... + + ... + + + ... + +* You can also use ``colors="bg_color_field: my_color"`` to defined the field name that will be used + for the background color of the line. + * If you want to use more than one color, you can split the attributes using ';': .. code:: diff --git a/web_tree_dynamic_colored_field/static/src/js/web_tree_dynamic_colored_field.js b/web_tree_dynamic_colored_field/static/src/js/web_tree_dynamic_colored_field.js index ae7e98273..26182edf2 100644 --- a/web_tree_dynamic_colored_field/static/src/js/web_tree_dynamic_colored_field.js +++ b/web_tree_dynamic_colored_field/static/src/js/web_tree_dynamic_colored_field.js @@ -6,6 +6,33 @@ odoo.define("web_tree_dynamic_colored_field", function (require) { var py = window.py; ListRenderer.include({ + /** + * Look up for a `color_field` or ``bg_color_field`` parameter in tree `colors` attribute + * + * @override + */ + _renderBody: function () { + if (this.arch.attrs.colors) { + var colorAttr = this.arch.attrs.colors.split(';'); + if (colorAttr.length > 0) { + var colorType = colorAttr[0].split(':')[0].trim() + var colorField = colorAttr[0].split(':')[1].trim(); + // validate the presence of that field in tree view + if (this.state.data.length && colorField in this.state.data[0].data) { + if (colorType === "color_field") { + this.colorField = colorField; + } else if (colorType === "bg_color_field") { + this.bgColorField = colorField; + } + } else { + console.warn( + "No field named '" + colorField + "' present in view." + ); + } + } + } + return this._super(); + }, /** * Colorize a cell during it's render * @@ -33,6 +60,13 @@ odoo.define("web_tree_dynamic_colored_field", function (require) { if (node.tag !== "field") { return; } + var treeBgColor = record.data[this.bgColorField]; + if (treeBgColor) { + $td.css('background-color', treeBgColor); + } + // apply 's own `options` + if (!node.attrs.options) { return; } + if (node.tag !== 'field') { return; } var nodeOptions = node.attrs.options; if (!_.isObject(nodeOptions)) { nodeOptions = pyUtils.py_eval(nodeOptions);