Is there a way to transform integers into their matching strings within Vue markup?

I have a database table where I retrieve data. The "status" field is used to manage the information in this table.

If the status is equal to 1, it indicates "Active." If the status is equal to 2, it signifies "Completed." If the status is equal to 0, it represents "Deleted."

Now, I want to display the corresponding status word in the table on the webpage. Below is how I am currently showing the status number:

<tr v-for="(depAcc, index) in depAccs" :key="index">
  <td>{{ index + 1 }}</td>
  <td>{{ depAcc.created_at }}</td>
  <td>{{ depAcc.name }}</td>
  <td>{{ depAcc.status }}</td>

Can you provide guidance on how to display the status word in the table instead?

Answer №1

To effectively manage the status of your data, I suggest creating a data property to store a status map. Here's an example:

data() {
  return {
    dataStatus: {
      0: 'Deleted',
      1: 'Active',
      2: 'Completed'
    }
  }
}

You can then easily reference this map in your markup:

<td>{{ dataStatus[depAcc.status] }}</td>

Answer №2

Within Vue, content enclosed in double curly braces {{ //this }} gets interpreted as JavaScript. This allows you to create a method that generates strings based on a particular status, and then call that method within the double curly braces.

<tr v-for="(depAcc, index) in depAccs" :key="index">
    <td>{{ index + 1 }}</td>
    <td>{{ depAcc.created_at }}</td>
    <td>{{ depAcc.name }}</td>
    <td>{{ statusText(depAcc.status) }}</td>
</tr>

To implement this functionality, include the following method in your page script:

var app = new Vue({
    el: '#app',
    methods:{
        statusText(status){
            let message = "Unknown Status"
            if (status == 0) message = "Deleted"
            if (status == 1) message = "Active"
            if (status == 2) message = "Completed"
            return message
        }
    }
});

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

Fluid sifting and interactive webpage

I'm almost done with my website, but I'm facing a problem. There are 3 blocks of text on my page - 2 static and 1 dynamic. When I click a button, the page should change, which is working fine. However, when I added smooth scroll to the website, ...

Keep the link underlined until a different button is pressed

I need help with a webpage that displays a list of partners organized by categories. When clicking on sidebar categories, the link remains underlined. However, if you click anywhere else on the page, the link becomes inactive. CSS: button:hover { t ...

Form an array using the values that are returned

As I iterate through an object and extract elements, my console.log displays: ["item 1"] ["item 2"] ["item 3"] and so on. All I want is to create a new array formatted like this: ["item 1","item 2","item 3"]; ...

Utilizing reactjs (MERN stack) to dynamically update content on a single page based on both URL parameters and database queries

Hello everyone, please excuse my English Imagine I have page1 with content in a database, and page2 with different content in another database. Both page1 and page2 share the same template, but I want to dynamically change the content based on the URL or ...

Troubleshooting jsPDF problem with multi-page content in React and HTML: Converting HTML to PDF

I need to convert HTML content in my React application into a PDF file. My current approach involves taking an HTML container and executing the following code snippet: await htmlToImage .toPng(node) .then((dataUrl) => { ...

Three.js - Optimizing and Handling Images - Best Practices

My current project has a unique concept: https://i.sstatic.net/Jd3Ot.jpg The main idea revolves around a virtual room that dynamically adjusts its height based on the number of images loaded by the user. While it functions smoothly with fewer pictures, ...

What is the best way to save an image in Node.js?

While I am extracting data from a website, I encountered the need to solve a captcha in order to access further data. I thought of presenting the captcha to the user, but the site is built using PHP and PHP-GD, complicating the process. The URL provided in ...

What could be the reason for my inability to retrieve req.user.username with passport.js?

I recently started using passport.js for authentication and I'm encountering an issue. When I log in via Google, the only information available to me through req.user is the user id. I have provided my passport setup code, along with the routes, hopin ...

Here is the process to make a GET request to an API with input parameters using JQuery and JavaScript in an Asp.net view when a particular tag

Seeking assistance in displaying the count of orders on my view using a special tag. I have written a stored procedure in SQL that returns the order count based on the input type. To showcase all types of orders, I have designed multiple boxes on my form. ...

Particular JSON formatting for a PHP array

My current code provides a specific output, but I am looking to format it in a JSON structure as shown in the expected output below. Can someone assist me in achieving this? Or point out any errors in my current approach? <?php $text = "A very nice ún ...

What is the best way to transform Json into XML using Scala?

I am attempting to transform a JSON value: [ { "msg": "Hiiiiii", "name": "Ruchirrrr" }, { "msg": "Holaaa Amigo", "name": "Pablo" }, { ...

What is the purpose of parsing my array if they appear identical in the console?

While working on a D3 component, I came across an interesting question. Why do different arrays require different data treatment even if they all contain the same information? In the first case, I have a 'hardcoded' array that looks like this: ...

I am receiving HTML code instead of JSON data when making a NodeJS request to get data

I am attempting to retrieve a JSON object from a GET request. It works fine in Python, but in NodeJs it displays the HTML source code of the page. Below is my NodeJs code: app.get("/well", function(request, response) { const req = require(&ap ...

Alter the class following modifications to the list

https://i.stack.imgur.com/QZob0.pngIn my dual list, the data is displayed in a ul li format fetched from a JSON file. Users can move items between the two lists. However, I am facing an issue where I want to apply a property that only displays content with ...

Is it acceptable to designate the db instance as a global variable so that it is always accessible without requiring the "require" statement in Node.js?

I am just starting my journey with "node js" and I am currently working on a program that requires a database model similar to "wpdb" in WordPress. Should I create it as a global variable or use the "require" statement only when needed? Your help in provi ...

Updating image URL for grouped objects with Fabric JS

Check out this link :http://jsfiddle.net/mishragaurav31/ang58fcL/#base I created two groups of objects; one consisting of a circle and text, and the other with two images. When I try to change attributes for group 1, it works fine. However, when attempt ...

A tool developed in Javascript that allows for the conversion of .ini files to .json files directly on the client

Does anyone know of a JavaScript library that can convert .ini files to .json files on the client-side? I checked out this library, but it doesn't meet my requirements. Here is an example of an .ini file: [Master_Settings:1] Model Name=RC-74DL IP Ad ...

Guide to Inputting Numbers in a Form Field using a Pop-up Keypad (with Javascript/AJAX)

I am working on a small project that involves creating a keypad with buttons for all the digits, backspace, and decimal. When these buttons are clicked, they should populate a form field like a text box. The keypad will be located next to the form field as ...

Modifying the background hue of the element-ui tooltip

I am currently working with vue.js version 2.6.10 and element-ui version 2.15.1. The element-ui component I am using is as follows: <el-tooltip content="Top center" placement="top"> <span>Dark</span> ...

Combining data from two CSV files to create a JSON file

My goal is to merge two CSV files and convert the data into JSON format. The challenge lies in the fact that the same column values in both files may not correspond uniquely across rows, as demonstrated here: gpo_full.csv: Date hearing_sub_type ...