Try implementing the @click event with a <select> element to update the value of a computed property

I am currently working on a project where I need to input emotions along with their intensity. The process involves adding an emotion using a text box and rating its intensity using a select box by clicking the blue button.

  1. After adding the emotion and intensity, there is a rerate section where I can update the intensity of that emotion.

In the rerate section, I have a computed property called rerateEmotions, which contains an array of objects with emotion and intensity properties. My goal is to change the intensity of each emotion in this computed property using the select tag.

Edit

Upon further investigation, I realized that I need to change the @click event to @change for the rerate <select>

Now, I'm trying to implement a separate rating system for both "rate emotion" and "rerate emotion" sections.

const app = new Vue({
  el: "#app",
  
  // data setup
  
  methods: {
    // method definitions
  }
});
// Sample code snippets for the Vue.js components and logic

Answer №1

If you require two different intensity ratings, simply have two properties on each object - for example, intensity and newIntensity. Initially set both to the same value when an entry is created, but the second property can be left empty if desired.

The computed property rerateEmotions seems unnecessary, so I have eliminated it in favor of directly using emotionIntensity. If the intention was to create a copy, using a computed property will not achieve that effect; it will just be the same array.

For the re-rate dropdown, I have utilized v-model, although an event listener could also be used if preferred. The important aspect is that the newIntensity property is being used for the second value.

const app = new Vue({
  el: "#app",
  data: function() {
    return {
      emotion: "",
      intensity: null,
      newIntensity: null,
      emotionIntensity: []
    };
  },
  methods: {
    openEmotion(id) {
      this.emotionId = id;
    },
    addEmotionIntensity() {
      var emotionIntensity = {
        emotion: this.emotion,
        intensity: this.intensity,
        newIntensity: this.intensity
      };
      this.emotionIntensity.push(emotionIntensity);
      this.emotion = "";
      this.intensity = null;
    },
    removeEmotion(id) {
      if (id > -1) {
        this.emotionIntensity.splice(id, 1);
      }
    },
  }
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">

  <div class="row mb-3 pr-4">
    <div class="col-6">
      <h1>Rate</h1>
      <input v-model="emotion" type="text" placeholder="Enter emotion here." class="form-control" id="basic-url" aria-describedby="basic-addon3">
    </div>
    <div class="col-5 select-outline">
      <select class="mdb-select md-form md-outline colorful-select dropdown-primary" v-model="intensity">
        <option value="" disabled selected>Rate Emotion</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
      </select>
    </div>
    <button @click="addEmotionIntensity()" type="button" class="col-1 btn btn-primary">+</button>
  </div>

  <ul>
    <li v-for="(emotion, index) in emotionIntensity">
      <b>{{emotion.emotion}} : {{emotion.intensity}}</b>
      <button type="button" @click="removeEmotion(index)">
        X
      </button>
    </li>
  </ul>

  <hr>

  <div class="form-group">
    <label for="exampleFormControlTextarea1"><h5>Rerate</h5></label>

    <li v-for="(emotion, index) in emotionIntensity">
      <b>{{emotion.emotion}} : {{emotion.newIntensity}}</b>
      <select v-model="emotion.newIntensity" class="mdb-select md-form md-outline colorful-select dropdown-primary">
        <option value="" disabled selected>Rate Emotion</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
      </select>
    </li>
    <hr>
  </div>
</div>

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

Encountered an Angular 9 error at core.js:6228, where a TypeError occurs when trying to read the property 'name' of an undefined

I'm currently working on developing a decentralized application (Dapp) with Angular 9 for the frontend. However, I've encountered an error that says: ERROR TypeError: Cannot read property 'name' of undefined. Here's a snippet of ...

Expanding an array in JavaScript

I need assistance with... let a = ['a', 2, 3]; a += function(){return 'abc'}; console.log(a[3]); Therefore, I am looking for a shorthand method to push() in array with the above content. Does anyone know of an operator that can help ...

The destroySlider() function of BxSlider fails to work due to either an undefined slider or a function that is not

I'm facing an issue with my carousel setup using bxslider. Here is the code snippet responsible for initializing the carousel: jQuery(document).ready(function() { var carouselWidth = 640; var carousel; var carousel_Config = { minSlides: 1, ...

What is the most efficient method for sorting a complex JSON object?

I have a JSON object with multiple nested levels: { "myJson": { "firstGroup": { "0": [ { "month": 1.0, "amount": 1.7791170955479318, ...

I encountered an issue while trying to build a website using React-static: the module 'perf_hooks' could not be found

Encountered an issue while trying to create a site with the react-static create command: Error: Cannot find module 'perf_hooks' at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25) at Mod ...

Identify all untagged comments using regex

I am looking to extract all nodes from an HTML or XML file that are not commented out. Below is the regular expression I am currently using. My Regular Expression /<span.*?>([\s\S]*?)<\/span>/gi Here is an example of XML < ...

Discover the method for showcasing a random database row by simply clicking a button!

I am new to coding and trying to figure out how to randomly show a row from a database when the BoostrapButton is clicked. Take a look at my code below: <h3 id="myHeader">Click the button below to display a name</h3> <script> ...

Reference now inactive in an array object no longer exhibiting reactivity

After implementing the following code successfully, we noticed that changing the language updates the text correctly thanks to the ref: const mainNavigationLinks = computed(() => [ { label: context.root.$t('navigationMenu.home') }, { labe ...

Is there a method in JavaScript or jQuery that can be used to detect changes in the width of a div element while debugging HTML

Is there a method to detect changes in the width of a div element when debugging HTML using JavaScript or jQuery? I am experiencing a situation where a div's size is being altered by some CSS rules, but I am unable to identify the source even when ana ...

Save JSON data in an HTML document or vice versa

Hey there, the title of this post might seem a bit unclear but I couldn't think of another way to phrase it. So here's the dilemma: I have some data that is JSON encoded and stored in a .json file: {"foo":"bar", "bar":"foo", "far":"boo"} Additi ...

a:hover CSS style altering the behavior of buttons with images in a web browser

Earlier I attempted to ask this question from my phone, but it ended up being overly convoluted and confusing. Therefore, I have decided to start fresh after locating a working PC. Unfortunately, due to the sensitive nature of the project, I am unable to p ...

Accessing AngularJS variable scope outside of a function

Utilizing a socket, I am fetching data into an angularJS controller. $rootScope.list1= ''; socket.emit('ticker', symbol); socket.on('quote', function(data) { $rootScope.list1 = angular.fromJson(data.substring(3)); //I can ...

Tips on dividing the time of an upload progress bar evenly using VueJS

When working with js and JavaScript, I am attempting to utilize the Axios onUploadProgress function in a way that it gradually progresses from 1% to 100%. Below is the code snippet: onUploadProgress: function(progressEvent) { let percentage = (progressE ...

Is there a way to directly access the React component that was clicked?

I'm looking to dynamically change the class of a component when clicked. By using state to create a new component with properties such as name and done (initiated as false), which are then added to the todos array, I want to find out how to identify t ...

I seem to be facing some issues while trying to create an avatar bot on discord.js

I'm trying to create a command for my bot that shows the user's avatar, but I keep running into an issue: DiscordAPIError: Cannot send an empty message at RequestHandler.execute (C:\Users\Pooyan\Desktop\PDM Bot Main\n ...

Mastering the art of passing props in VueJS

My setup in the App is quite straightforward, with two main components: The HelloWorld component and a dialog component. The dialog component receives props from the HelloWorld component. The data in the HelloWorld component is rendered by looping over an ...

Position-fixed Vuetify notification component

Struggling to create a VueJS/nuxtjs alert component that behaves like a snackbar with a fixed-bottom position, but can't find much relevant documentation. Despite exploring the vuetify alert component API and comparing it to the snackbar component, I ...

Checkbox click triggers a delayed update to the array

In my attempt to develop an array of user permissions that can be managed through a series of checkboxes, I encountered an issue. When I select a checkbox and use console.log() to display the array, it shows all the permissions I have checked. However, upo ...

An occasion to monitor alterations in attributes

Is there a way to attach an event to an HTML element to listen for attribute changes? My goal is to trigger an action when the visibility of a div changes. I can't alter the JavaScript code that controls the visibility, so I need to find a way to dete ...

Setting up Angular 6 on Azure Platform

I am currently facing challenges while trying to deploy a sample application on the azure portal. To start off, I decided to run some tests by creating an Angular app using 'ng new poc-pwa-angular-v2', and then pushed it to a Bitbucket repositor ...