Update the value shown in a v-slider and v-text-field depending on the selection made in a v-select dropdown

I am facing a rather intriguing problem that I can't quite figure out.

Within a form, I have multiple <v-slider>s and <v-text-field>s. Each attribute of an object has a slider and text field using it as their v-models in the following way:

<v-slider v-model='object1.value'></v-slider>
<v-text-field v-model='object1.value'></v-text-field>

The issue is that these fields retrieve values from the Database in seconds, which can be cumbersome for users when entering values that could be more easily represented as hours or minutes. I have a <v-select> dropdown that allows the user to choose between hours or minutes, but I'm struggling to change the value displayed on the sliders and text fields accordingly.

For example:

  1. The slider value is 3600 (in seconds).
  2. User selects hours from the dropdown.
  3. 3600 should then change to 1.

I've attempted setting a watcher on the dropdown change, but I keep getting NaN when trying to manipulate the values provided.

Does anyone have any ideas on how to solve this?

EDIT: CodePen

EDIT 2: It appears that a computed property might be what I need, but I'm struggling to make it work. I might just move these values bound to my inputs to other variables and reassign them upon form submission.

Answer №1

If you want to adjust the original value based on the selected time unit, one way to do it is by using a computed property. To implement this in your code, make the following changes:

Inside the select element, include the number of seconds for each time unit as the value - this will act as your conversion factor

<v-select 
  :items=[
    {text: 'Seconds', value: 1},
    {text: 'Minutes', value: 60},
    {text: 'Hours', value: 3600}
  ]" 
  v-model="conversionFactor"
/>

The computed property should divide by the conversion factor when getting the value and multiply by the factor when setting the value:

computed: {
  convertedValue: {
    get: function() {
      return this.exObj.value / this.conversionFactor
    },
    set: function(newValue) {
      this.exObj.value = newValue * this.conversionFactor
    }
  }
}

Additionally, you can adjust the maximum value of the slider based on the selected conversion:

<v-slider 
  v-model="convertedValue" thumb-label
  :max='9999 / conversionFactor'
  :min='0' 
/>

Check out this working example on CodePen.

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

Allowing ng-click to launch a webpage in a new browser tab

I am struggling to open a valid URL generated by ng-click in a browser window, as it triggers my 3rd party app. I have tried various options and found some additional button solutions (2-6) on Stack Overflow and Google, but I need help with making it open ...

Encountering an issue when trying to submit a post request: "Received an API resolution without a response for /api/comments, which could lead to delayed requests."

Hey, I recently started diving into Next.js and I'm facing an issue with making a POST request to the API. In my project structure, I have a comments folder nested inside the api folder. Within the comments folder, I've written the following cod ...

What is the best way to ensure that circles only touch each other by their edges?

Trying to align three circles touching each other has been a challenge for me. Although I have successfully managed to make two touch, the third one remains elusive. How can I ensure that all three circles are in contact with each other, especially when th ...

Incorporating unique numbers in the map reduce process

I have a CSV file containing information on which numbers called each other and the details of the calls like duration, time, etc. My goal is to compile all the numbers that a specific number has called into an array. Each element in this array should be ...

Just encountered an issue stating "PrismaClient cannot run in the browser" while working with [Next.js]

My initial plan was to log all the IDs of news in my database using console. However, when I executed the code, an error occurred as shown in this image. What is the best way to address and resolve this issue? https://i.stack.imgur.com/ci8G1.png ...

Firefoxe's slideToggle function adds an additional space

Having trouble with the drop-down menu in Firefox. There seems to be a mysterious gap of about 200 px below the list created by slideToggle. When inspected, this area appears completely empty with nothing in it. Chrome displays everything correctly. You c ...

Interacting with Django and Python using AJAX POST method

I’ve been working on developing a web app using django that allows users to record audio, convert it into text, and then send it to the server using AJAX. During my research, I came across this helpful article: https://medium.com/@manask322/implementing- ...

Is there a way to dynamically update a game's highscore from a database without having to refresh the page?

I developed a JS snake game using HTML5 canvas. In the event that the user loses, the score is transmitted to the database through an AJAX call in my PHP script. The PHP code then compares this score to the current highscore and updates it if needed. Howev ...

Which function is triggered first - onclick or ng-click?

Currently, I have a button with a validation process occurring on click. The validation process triggers a web service call and other processes if successful. However, I'm uncertain if the validation is actually taking place. This is my page setup: ...

Using document.write, adding elements to an Array with Array.push, and concatenating

I need my code to be able to update the current HTML page visible to the user using document.write(). Although it successfully updates the HTML page, it doesn't display the desired text. When I utilize my code to insert an element using the addElement ...

Utilizing For Loop to Integrate TextGeometry in THREE.js

Attempting to construct numbers along a 3D grid using THREE.TextGeometry() However, only one number is being generated. How can I fix this issue? The "current" variable ranges from -10 to 10, excluding 0. My IDE (PyCharm) is indicating that the "Mutable ...

Executing a SQL query with multiple WHERE clauses by assigning values in one command

Here is an example of a values array: const params = [ ['2022-12-10', 'aaaaa', '2022-12-01', 'xhxha', '2022-12-10'], ['2022-12-11', 'ababa', '2022-12-01', 'xhxha', &a ...

Guide on presenting an image retrieved from a database with ajax

I am currently developing a straightforward mobile application that utilizes a database to store various types of content, including images. I have implemented ajax requests to retrieve this information and display it within the app. However, I am encounte ...

Only implement $(document).on(...) for every JQuery request

As someone new to Jquery, I'm facing challenges in grasping its functionality. Specifically, my struggle lies in setting up an event listener on an i element with the class of .dataPickerIcon, which should respond to a click by inserting more HTML usi ...

Guide on accessing and using the value of a specific key in an Object within an if statement using JavaScript

As a JavaScript beginner and enthusiast, I'm in need of help. I am encountering an issue with the code snippet provided below: if (newTask.done === false) { newTask = doneArray.push({ id: doneArray.length, description: ...

JavaScript: Adjust DIV Width Based on Window Height

Is there a way to make the width of a div equal to the height of the window in jquery? I attempted this method: <script type="text/javascript"> $(".rt-container").style.width = $(window).height(); </script> Unfortunately, it did not work. I ...

Angular service to display an array of data

I have been experimenting with Angular, working on small exercises to improve my skills. I have managed to display objects in the controller but am struggling to use service and factory to display an array called "names" on a table. Can someone please revi ...

Ways to resolve incorrect URL that interfere with date search results

I'm currently developing a webpage that displays user data in a table using bootstrap and PHP Laravel. It features search functions by date and name, a form control for selecting the number of entries per page, pagination buttons, and other miscellane ...

Difficulty in Verifying ECDSA Signatures Across React and Python Environments

Currently, I am tackling a project that requires me to create an ECDSA signature in a React application and then validate it in a Python backend. Although the signature generation and validation processes work perfectly within their respective environments ...

Best practice for detecting external modifications to an ngModel within a directive

I've been working on creating a directive that can take input from two sources and merge them into one. To achieve this, I'm monitoring changes in both inputs and updating the combined value in the ngModel of my directive. However, there's ...