vue.jsvuejs2rangeslider

Apply style to an HTML element added dynamically by v-for using Vuejs


I'm adding elements to a list dynamically using v-for.

<ol>
    <li v-for="light in lights">
        <input type="range" min="0" max="255" v-model="light.currentBrightness" v-on:change="setBrightness(light)" />
    </li>
</ol>

I want to decorate the slider using rangeslider.

Problem is, when a new element is added after the DOM is initialized, it's not taking the style specified in rangeslider.js. Way to fix this is to call the reinitialize method in rangeslider.js which will redecorate all the slider elements.

I'm not sure how to call the javascript method when the element is added dynamically during the runtime. Does anyone how to do it? To me, it seems like a very common problem but I could not find a solution by Googling.

My issue is same as discussed in github.


Solution

  • If you're new to JavaScript and Vue, you're diving in pretty close to the deep end. The rangeslider isn't just styling (like CSS), it's a widget that replaces the built-in range input.

    One basic idea behind Vue is that it controls the DOM and you only modify your model, but there are some carefully controlled exceptions. Components have lifecycle hooks where you are allowed to insert and modify DOM elements owned by the component.

    Some instructions for v-model support:

    So for a component to work with v-model, it should (these can be configured in 2.2.0+):

    • accept a value prop
    • emit an input event with the new value

    So we make a component whose template is a range input element. We give it a value prop. In the mounted hook, we initialize the rangeslider on the input element (made available as el), then set it up to emit input events on change.

    new Vue({
      el: '#app',
      data: {
        lights: [{
            currentBrightness: 10
          },
          {
            currentBrightness: 30
          }
        ]
      },
      methods: {
        addRange: function() {
          this.lights.push({
            currentBrightness: 50
          });
        }
      },
      components: {
        rangeSlider: {
          props: ['value', 'min', 'max'],
          template: '<input min="{{min}}" max="{{max}}" type=range />',
          mounted: function() {
            var vm = this
            $(this.$el)
              .val(this.value)
              // init rangeslider
              .rangeslider({
                polyfill: false
              })
              // emit event on change.
              .on('change', function() {
                vm.$emit('input', this.value)
              })
          }
        }
      }
    });
    <link href="//cdnjs.cloudflare.com/ajax/libs/rangeslider.js/2.3.0/rangeslider.css" rel="stylesheet" />
    <script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.2.2/vue.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/rangeslider.js/2.3.0/rangeslider.min.js"></script>
    <div id="app">
      <ol>
        <li v-for="light in lights">
          <range-slider v-model="light.currentBrightness" min="0" max="255"></range-slider>
          <div>{{light.currentBrightness}}</div>
        </li>
      </ol>
      <button @click="addRange">Add Range</button>
    </div>