Tips on organizing columns in Vue when sorting is needed for computed values

I have come across various resources for sorting data that is contained within an array, but I am unable to locate any information on how to sort dynamically generated data.

<table>
    <thead>
      <tr>
        <th>Program</th>
        <th>Rewards</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="program in programs" :key="program.id">
        <td>{{ program.program_name }}</td>
        <td>{{ pointValue(program) | percent }}</td>
      </tr>
    </tbody>
  </table>

The pointValue() function calculates and returns a value displayed as a percentage in the Rewards column. I am interested in making the table sortable by Programs and by Rewards. (Program is simply a string).

Answer №1

Generate an array of computed values for programs by utilizing the map and sort methods, then iterate through it instead

computed: {
  computedPrograms() {
    return this.programs
      .map(program => {
        return {
           ...program,
           value: this.pointValue(program)
        }
      })
      .sort((a, b) => a.value - b.value)
  }
}
<tr v-for="program in computedPrograms" :key="program.id">
  <td>{{ program.program_name }}</td>
  <td>{{ program.value | percent }}</td>
</tr>

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 error while attempting to load resource: the server returned a 404 (Not Found) status code when trying to load an image in an

I am looking to dynamically load an image when it is selected from a file picker dialog. The code provided below attempts to achieve this, however, the image does not load into the img tag. <script src="https://cdnjs.cloudflare.com/ajax/libs/jq ...

What is the speed of retrieving new data once it has been inserted into a firebase real-time database?

In the midst of developing my personal project using next.js, I've encountered an issue with a component that includes a getstaticprops function. This function scrapes a website and then posts the extracted data to a firebase realtime database. Howeve ...

What is the best way to transform the request query id = ' [12eetftt76237,jhgasduyas7657] ' into an array of elements or strings like [12eetftt76237,jhgasduyas7657]?

Hey there, I am working on a project using hapijs and typescript. I have a requirement to send an array of IDs as parameters through the request URL. Here is an example of the URL: localhost:3444/?id='[askjajk78686,ajshd67868]' I attempted to u ...

The instance is referencing "underscore" during render, but it is not defined as a property or method

I have experience as a skilled react developer, but I've taken over a vue.js project from another developer and managed it for quite some time. Regrettably, I haven't put in the effort to learn vue properly. When using lodash, I encountered an u ...

Encountered difficulty in setting cookie with ngx-cookie-service

I'm currently using angular 9 and I have an issue with setting a cookie. I have integrated ngx-cookie-service 3.0.4 into my project and I'm attempting to set the cookie as shown below: this.cookieService.set("cookieName", user.tokenId, date, "/" ...

Caution when using a React form: Value of `true` has been detected for a non-boolean attribute `validate`

I am trying to address a warning message that I have received index.js:1 Warning: Received true for a non-boolean attribute validate. If you want to write it to the DOM, pass a string instead: validate="true" or validate={value.toString()}. I ...

The configuration error occurred for the `get` action due to an unexpected response. Instead of an object, an array was received

Despite numerous attempts, I am struggling to find a solution that works for me. In my Courses controller, I am using the Students service and Staff service to access my staff and student objects. My goal is to retrieve the staffs and students objects in o ...

Revise the reply within ExpressJS

I need help with editing the response to a request in Express. When the request is made via XHR, I want to encapsulate the body inside a JavaScript object. This way, different parts of the page can be accessed individually within the JavaScript object, suc ...

Retrieve all data from the 'belongsToMany' table where a particular record is included in the list

In my database, I have models for User, Message, and Group. Each Message belongs to a Group, and each Group can have many users as members. To query all messages where a specific user is a member of the group, I use an include statement in my Message mode ...

Update the class of the appropriate navigation tab when the corresponding div is scrolled into view

After reading similar questions and doing some research on scrollspy, I don't think it will provide the functionality I need. It seems to only support bootstrap style highlighting. If there is more to it that I'm not aware of, please inform me! ...

Is it possible to utilize router.push within Redux thunk? Is this considered a beneficial approach?

I have this anchor element: <a className="btn btn-sm btn-circle" href={`https://www.facebook.com/sharer/sharer.php?u=${ process.env.NEXT_PUBLIC_ENVIRONMENT == "prod" ? "https://tikex.com" : "https:/ ...

Obtaining a response in string format using the $.ajax function

var module = (function(){ return{ loadData: function(url, success, error){ $.when($.ajax({ type: 'GET', cache: false, url: url, contentType: 'application ...

Welcome to the latest update of React Router version 6.4.3 featuring the new

As a newcomer, I came across an issue with V6.4.3 where the update appeared to authenticate using the loader. I attempted to simplify the original method by using useLoaderData(), but it resulted in undefined. Index.js const router = createBrowserRouter( ...

The issue with Mongoose not properly dropping the database and closing the connection is causing unexpected behavior in Mocha tests

I encountered an issue with my tests - one passed and the other failed due to a problem with the Schema being compiled again. Error: Cannot overwrite CheckStaging model once compiled. The first test that passed: var mongoose = require('mongoose& ...

Creating custom fields with user input in WordPress widgets

I'm currently working on a theme and I need to create dynamic input fields for a set of predefined labels in a custom widget. My goal is to achieve the following layout: CourseName FieldONE FieldTWO ----------------------------- ...

What is the method for ending the mouseleave effect?

Clicking on the box will trigger a change in the text on mouseleave. Clicking on the button will also cause another change in the text. How can we revert the text back to its original position after removing the effects triggered by clicking the button and ...

Tips for updating the background color of a highlighted row within a Vuetify Datatable

I am looking to customize the background color of a clicked row in my Vuetify data table. I noticed in the documentation that there is an event called click:row which returns the row's data, but how can I identify the row itself and modify its CSS acc ...

Chrome not responding to ajax requests

Having an issue with a script that uses ajax to post text: <?php if (isset($_POST['q'])) { echo 'q is '.$_POST['q']; } else { ?> <!DOCTYPE HTML> <html> <head> <script ...

The measurement of a HTML window's entire content height (not just the visible viewport height)

Currently, I am attempting to determine the total height of a webpage's content, not just what is visible. In my efforts, I have managed to achieve some success in FireFox using: document.getElementsByTagName('html')[0].offsetHeight. Howeve ...

Why does the parent URL become the origin for an AJAX request coming from an iframe?

I am facing an issue with a website where I need to load an iframe from a different subdomain. The main website is hosted on portal.domain.com, and the iframe is on iframe.domain.com. To make requests to iframe.domain.com from portal.domain.com, I decided ...