0

I have a table with two-button Inch and CM, and I want to change the numbers from Inch to CM on click, Please can someone teach me how to do it 🙏🙏🙏

enter image description here

 data() {
        return {

          guild_size_inch: [
            {
              size: "S",
              chest: "92",
              length: "71"
            },
            {
              size: "M",
              chest: "40",
              length: "29"
            },
            {
              size: "L",
              chest: "44",
              length: "30"
            }
          ],
        }
      },
  
      methods: {
        changeToCM: function() {
          this.guild_size_inch * 2.54;
          alert(this.guild_size_inch)
        }
      }
1
  • I added an answer, Hope that will work as per your expectation. Commented Mar 21, 2022 at 14:18

3 Answers 3

1

I created a below code snippet as per your requirement. Please find the explanation of the logic inline in the snippet.

const app = new Vue({
  el: '#app',
  data() {
    return {
      guild_size_inch: [
        {
          size: "S",
          chest: "92",
          length: "71"
        },
        {
          size: "M",
          chest: "40",
          length: "29"
        },
        {
          size: "L",
          chest: "44",
          length: "30"
        }
      ],
      isInch: true, // property to enable/disable the 'Inch' button.
      isCM: false // property to enable/disable the 'CM' button.
    }
  },
  methods: {
    // Method used to convert the Inch values into CM.
    changeToCM() {
      this.isCM = true;
      this.isInch = false;
      this.guild_size_inch = this.guild_size_inch.map((obj) => {
        obj.chest = parseInt(obj.chest) * 2.54;
        obj.length = parseInt(obj.length) * 2.54;
        return obj;
      });
    },
    // Method used to convert the CM values into Inch.
    changeToInch() {
        this.isCM = false;
      this.isInch = true;
      this.guild_size_inch = this.guild_size_inch.map((obj) => {
        obj.chest = Math.ceil(parseInt(obj.chest) / 2.54);
        obj.length = Math.ceil(parseInt(obj.length) / 2.54);
        return obj;
      });
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <button @click="changeToInch" :disabled="isInch">Inch (Default)</button>
  <button @click="changeToCM" :disabled="isCM">CM</button>
  <table>
    <th>Size</th>
    <th>Chest</th>
    <th>Length</th>
    <tr v-for="(item, index) in guild_size_inch" :key="index">
      <td>{{ item.size }}</td>
      <td>{{ item.chest }}</td>
      <td>{{ item.length }}</td>
    </tr>
  </table>
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

You are trying to multiply the object instead of the numbers contains in your object.

What you should try is loop through your array and multiply both chest and length.

You can perform this with a forEach loop : see documentation here

changeToCM: function() {
   this.guild_size_inch.forEach(item => {
      item.chest = parseInt(item.chest) * 2.54
      item.length = parseInt(item.length) * 2.54
   });
   alert(this.guild_size_inch)
}

2 Comments

I see, I really appreciate it, that's teaching me a lot, I have another question, how to get back to Inch again? or I should make same function and change the formula?
I think there are 3 ways of doing this : Either you can make another method changeToInch, pass a parameter to the existing function or set a global parameter like @Boussadjra Brahim suggests and check it in the existing function
1

Define a property called currentUnit initially set to inch which will be changes by the click event, then use a computed property that returns the sizes based on the current unit which you selected :

 data() {
   return {
      guild_size_inch:[....],
      currentUnit:'cm'

   }
 },
computed:{
   sizes(){
      return this.guild_size_inch.map(size=>{
          size.chest=this.currentUnit==='cm'?size.chest*2.54:size.chest*2.54
          size.length=this.currentUnit==='cm'?size.length/2.54:size.length/2.54
         return size;
    })
  }
},
methods:{
  changeUnit() {
     this.currentUnit==='cm'?this.currentUnit='inch':this.currentUnit='cm'
 }
}

then render the sizes in the table.

2 Comments

that's genius, thank you so much for this amazing code and explanation, I do have a few questions, to switch between Inch and Cm need to add @click="currentUnit = 'inch'"? the second question is when I added Math.ceil() the numbers increasing when I switch to inch to cm or cm to inch: size.chest=this.currentUnit==='cm'?Math.ceil(size.chest*2.54):Math.ceil(size.chest/2.54) size.length=this.currentUnit==='cm'?Math.ceil(size.length*2.54):Math.ceil(size.length/2.54)
you should add @click="changeUnit" please look at the edited answer I fixed some mistakes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.