Search code examples
vue.jsvuejs2vue-componentrangeslider

How to update value after change range slider VueJS


I want to update value after dragging the Slider.

This is my code

<div id="app">
    <vue-range-slider v-model="rangeSlider" v-bind="options"></vue-range-slider>
    <p class="mt-50">Value: {{ value }}</p>
</div>

let app = new Vue({
    el: '#app',
    data() {
        return {
            value: 0,
            rangeSlider: 0,
            options: {
                eventType: 'auto',
                width: 'auto',
                height: 5,
                dotSize: 16,
                min: 0,
                max: 50,
                interval: 1,
                show: true,
                speed: 1,
                tooltipDir: 'bottom',
            }
        }
    },
    computed: {
        rangeChange () {
            let range = this.rangeSlider;

            if (range > 0 && range <= 20) {
                return this.value = 4;
            }
            if (range > 20 && range <= 30) {
                return this.value = 5;
            }
            if (range > 30 && range <= 40) {
                return this.value = 6;
            }
            if (range > 40 && range <= 50) {
                return this.value = 7;
            }
        }
    }
})

I've created rangeChange() in computed to change the value, but after I dragging the Slider, the value is not update. You can see more here: https://codepen.io/LinhNT/pen/KKpNeva

I use this range slider vue: https://github.com/xwpongithub/vue-range-slider

How can I fix that?


Solution

  • First rangeChange is not used anywhere so the value is never computed.

    Second You shouldn't modify state in a computed value you can do that with a watcher.

    Updated code :

    Vue.config.devtools = false;
    Vue.config.productionTip = false;
    
    let app = new Vue({
      el: "#app",
      data() {
        return {
          value: 0,
          rangeSlider: 0,
          options: {
            eventType: "auto",
            width: "auto",
            height: 5,
            dotSize: 16,
            min: 0,
            max: 50,
            interval: 1,
            show: true,
            speed: 1,
            tooltipDir: "bottom",
            tooltipStyle: {
              backgroundColor: "#2AB6CB",
              borderColor: "#2AB6CB"
            },
            processStyle: {
              backgroundColor: "#2AB6CB"
            }
          }
        };
      },
      watch: {
        rangeSlider(range) {
          if (range > 0 && range <= 20) {
            this.value = 4;
          }
          if (range > 20 && range <= 30) {
            this.value = 5;
          }
          if (range > 30 && range <= 40) {
            this.value = 6;
          }
          if (range > 40 && range <= 50) {
            this.value = 7;
          }
        }
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    <link href="https://unpkg.com/[email protected]/dist/vue-range-slider.min.css" rel="stylesheet"/>
    <script src="https://unpkg.com/[email protected]/dist/vue-range-slider.min.js"></script>
    <div id="app">
      <vue-range-slider v-model="rangeSlider" v-bind="options"></vue-range-slider>
      <p class="mt-50">Value: {{ value }}</p>
    </div>