Refreshing Data on Vuetify Range Slider

My goal is to update the value as the slider position changes.

[codepen]https://codepen.io/JakeHenshall/pen/WLezNg

<div id="app">
   <v-app id="inspire">
<v-card flat color="transparent">

  <v-subheader>Tick labels</v-subheader>

  <div v-if="value == 0">
     {{ ticksLabels[0] }}
  </div>
  <div v-else-if="value === 1">
    {{ ticksLabels[1] }}
  </div>
  <div v-else-if="value === 2">
    {{ ticksLabels[2] }}
  </div>
        <div v-else-if="value === 3">
    {{ ticksLabels[3] }}
  </div>
  <div v-else>
    {{ ticksLabels[4] }}
  </div>

  <v-card-text>
    <v-slider
      v-model="fruits"
      :tick-labels="ticksLabels"
      :max="4"
      step="1"
      ticks="always"
      tick-size="2"
    ></v-slider>
  </v-card-text>
</v-card>

Javascript:

new Vue({
    el: '#app',
    data () {
      return {
        value: 0,
        ticksLabels: [
          '0 - £5k',
          '£5k - £10k',
          '£10k - £25k',
          '£25k - £50k',
          '£50k+'
        ]
      }
    }
  })

If anyone has tips on how to tidy up the if statement, I would greatly appreciate it.

Many thanks.

Answer №1

Make sure to follow these steps:

  1. The fruits being used as the v-model on your v-slider must be properly declared. If value is declared but not used, consider using it instead as your model.
  2. Ensure that the model is applied to both the slider and the text value representing its position to avoid the need for conditional statements.

For example:

<v-text-field v-model="ticksLabels[value]"
              class="mt-0"
              type="text"></v-text-field>

<v-card-text>
  <v-slider v-model="value"
            :tick-labels="ticksLabels"
            :max="4"
            step="1"
            ticks="always"
            tick-size="2"></v-slider>

Check out the updated codepen here: https://codepen.io/jayas/pen/QzLrZd

Answer №2

If you eliminate the need for your if clause altogether, you can obtain the index of your selected item by using v-model="value" (as you are already doing, but 'fruits' may be incorrect) and substituting your if clause with this small snippet of code:

<div>
  {{ticksLabels[value]}}
</div>

I have made updates to your Fiddle here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a Rectangular Trapezoid Shape with CSS - Eliminating Unnecessary Spacing

I'm trying to create a trapezoid button using CSS. Here is the intended look: However, my current implementation looks like this: The button appears fine but there seems to be some excess space below it. It's almost like an unwanted margin, ev ...

Troubleshooting `:=` Syntax Error in Vue.js: A Step-by-Step Guide

In my latest Vue.js project, I encountered an error with the following code snippet: <script setup> let imageInfo = { src: "https:/myImg.jpg", alt: "my img", width: 400 }; </script> <template> <img ...

Animating elements on a webpage can be achieved by using both

Is there a way to create an animation that moves objects based on button clicks? For example, when the "Monday" button is pressed, the object with the correct color will move down. If any other button like "Tuesday" is clicked, the previous object will mov ...

Acquire the value of ant-design Select dropdown upon hover

How can we detect the selected option in an ant-design dropdown when it is hovered? <Select defaultValue="lucy" style={{ width: 120 }} onChange={handleChange}> <Option value="jack">Jack</Option> <Option value=& ...

Angular $resource encounters a 400 Bad Request error when attempting a PUT request, triggering the $resolve and $promise

My service is structured as follows (with variables removed): angular .module('app') .factory('Employee', function($resource) { return $resource("https://api.mongolab.com/api/1/databases/:dbName/collections/:collectionN ...

Exploring the functionalities of class methods within an Angular export function

Is it possible to utilize a method from an exported function defined in a class file? export function MSALInstanceFactory(): IPublicClientApplication { return new PublicClientApplication({ auth: AzureService.getConfiguration(), <-------- Com ...

The angular application fails to load the page properly and keeps refreshing itself

I'm currently working on an Angular app that searches for a Github user based on their username and then displays the list of repositories. When a user clicks on a repo name, it should show the open issues and contributors associated with that reposit ...

Using setTime in JavaScript allows for customizing and adjusting the

I'm having trouble getting this code to display the time. I thought it would work, but it's not showing the time. Can someone please help me figure out what's going wrong? function startTime() { var currentTime = new Date(); ...

What is the reason behind "Script" being considered the offspring of "Body"?

Unfortunately, I struggle with HTML/CSS/Javascript and am just trying to get through my exams. I have the code snippet below: <script> window.onload=function() { var c = document.body.childNodes; var txt = ""; var i; for ...

Ways to trigger the keyup function on a textbox for each dynamically generated form in Angular8

When dynamically generating a form, I bind the calculateBCT function to a textbox like this: <input matInput type="text" (keyup)="calculateBCT($event)" formControlName="avgBCT">, and display the result in another textbox ...

What is the best way to arrange an array or display it accurately?

Guys, here's a challenge for you: extract the values from this JSON data: [[name, brand, oem, category], [name, brand, oem, category], [name, brand, oem, category], [name, brand, oem, category]] Check out my JavaScript code: $(function(){ $('i ...

Use Vue and axios to retrieve images from imgur

When attempting to retrieve all images from the Galleries template, I encounter the error/warning message saying: "Property "token" was accessed during render but is not defined on instance". Instead of displaying the images, I see: <img src="function ...

Instructions for selecting a checkbox using boolean values

According to the HTML specification, you should use the checked attribute in the <input type="checkbox"> tag to indicate that it is checked. However, I only have a boolean value of either true or false. Unfortunately, I am unable to manipulate the b ...

How to accentuate search results using Angular filters and conceal non-matching text?

Recently, I came across an interesting example of using Angular filter to highlight search results. It works well in highlighting the word 'suit', but I noticed that all non-matching text remains visible. If you'd like to see the example I ...

Crop and upload images asynchronously using Node.js

I need to resize an image into multiple sizes and then upload them to AWS S3. The specific resizing dimensions are stored in an array. To accomplish this, I am utilizing the async waterfall method along with the series method. async.each(crop_sizes, func ...

What is the reason behind the failure of next/script with Google reCAPTCHA?

Currently, I am in the process of upgrading from next js version 8 to version 11. I wanted to take advantage of the amazing next js feature for "next/script". However, when I tried to implement it for Google reCAPTCHA using "react-recaptcha": "^2.3.10", th ...

The tooltip chart is not displaying all of its data

I create a dynamic tooltip with a custom chart inside of it. tooltip: { borderRadius: 15, borderWidth: 0, shadow: false, enabled: true, backgroundColor: 'none', useHTML: true, shared: true, formatter: function() { ...

The issue with the $(window).width() property not functioning correctly in Internet Explorer

Currently, I have a Div element with absolute positioning: <div id="target" style="height: 300px; position: absolute; top: 275px;"></div> My goal is to calculate the horizontal resolution of the screen using JavaScript. With this width, I the ...

What are the steps to activating components in vue.js?

Why do I always see the UsersList.vue component when opening any URL? Is it because I include it in the App.vue? If I change it to <router-view/>, I only see an empty page. How can I resolve this issue? Also, how can I navigate from ListView to Detai ...

Making a REST API call with an ID parameter using Angular

I am currently working on an Angular application that interacts with a REST API. The data fetched from the API is determined based on the customer ID, for example: api/incident?customer_id=7. I am unsure of how to incorporate this into the API URL and serv ...