Welcome!

Share and discuss the best content and new marketing ideas, build your professional profile and become a better marketer together.

Sign up

You need to be registered to interact with the community.
This question has been flagged
1 Reply
20 Views

Hey Cyllo peeps, anyone know how to create a custom range slider widget? I'm struggling to find a good solution. Any tips or pointers would be awesome!

Avatar
Discard

Here's how you can create a custom range slider widget for integer fields in Odoo using OWL, QWeb, and Python. This approach provides a user-friendly interface for adjusting integer values and efficiently handles updates to the backend. 

This part adds an integer 'quality_level' field to the 'product.product' model.

from odoo import fields, models

class ProductProduct(models.Model):
​_inherit = 'product.product'

​ quality_level = fields.Integer("Quality Level")

This XML snippet adds the 'quality_level' field to the product form view and applies the custom 'range_slider' widget.

This OWL component implements the range slider functionality, including debouncing for efficient backend updates.

 /** @odoo-module **/

import { Component } from "@odoo/owl";
import { useState } from "@odoo/owl";
import { registry } from "@web/core/registry";
import { debounce } from "@web/core/utils/timing";

export class RangeSliderField extends Component {
​static template = 'FieldRangeSlider';
​setup() {
  ​const { min, max, step } = this.__owl__.parent.props.fieldInfo.options || {};
  ​this.state = useState({
​value: this.props.record.data[this.props.name],
​  ​min: min ?? 0,
  ​max: max ?? 100,
  ​step: step ?? 1,

​});
  ​this._debouncedWrite = debounce(this._writeToServer.bind(this), 300);

​}
​getValue(e) {
​this.state.value = e.target.value;
  ​this._debouncedWrite();
​}
  ​_writeToServer() { 
​const config = this.env.model.config;
  ​this.env.model.orm.write(config.resModel, [config.resId], {
  ​[this.props.name]: this.state.value, });
  ​} 
​}
​export const rangeSliderField = {

​   component: RangeSliderField,

​   displayName: "RangeSliderField",

​   supportedTypes: ["int"],

​};

​registry.category("fields").add("range_slider", rangeSliderField);


Frontend: Qweb Template

   

     

              ​t-att-max="state.max" t-att-step="state.step"

​t-att-value="state.value" t-on-input="getValue"/>


            ​style="background-color: #e6dddd; min-width: 40px; text-align: center;">

        ​​

     

   

 

Avatar
Discard

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!