arrange data based on table heading using vue.js

I've recently upgraded to Vue 2.x and found out that the orderby directive is no longer supported as per this documentation.

I'm trying to avoid adding another JavaScript library, but if using lodash is the only option, I'm willing to give it a shot. I've attempted other methods of JS sorting from various sources (link, link), with no success.

The goal is to enable sorting by clicking on the table header value.

The rowData array is fetched from the database and has this structure:

rowData:Array[79]
 0:Object
  ActiveDate:"06/25/2018"
  CountryId:10
  ExportedDate:""
  ExportedStatus:"0"
  Id:1751
  ItemType:"Condition Types"
  LogId:72843
  ModifiedDate:"06/25/2018"
  Name:"GenericNameGoesHere"
  ParentTable:"tableNameGoesHere"
  Region:"Home Office"
  Type:"CI"
  TypeId:123
  selectedExport:false

Initially, I want to sort the array based on the ItemType property which has various values like Condition Types, Medical Reference, Synonyms, References, etc.

The table header setup includes:

v-on:click="sortBy('ItemType')"

Current implementation of the sorting method (sortKey is hardcoded for ItemType):

sortBy: function (sortKey) {
    var vm = this;
    _.orderBy(vm.rowData, ['ItemType'], ['asc']);
},

Unfortunately, the sorting is not working at the moment. I'm unsure if it's an issue with my Vue code or the lodash code.

If you require more information or code snippets, please let me know. I'm eager to implement sorting for all table headers (currently 7 others).

Edit:

I also attempted the following (without any changes to the sorting behavior):

return _.orderBy(vm.rowData, ['ItemType']);

Answer №1

When using _.orderBy, it creates a new array instead of mutating the existing one. As a result, vm.rowData remains unsorted and Vue does not detect any changes. Even if _.orderBy did mutate the array, Vue would still not track the changes due to its inability to monitor array access (a limitation mentioned in Vue's documentation: https://v2.vuejs.org/v2/guide/list.html#Caveats). To address this issue, you can:

sortBy: function (sortKey) {
    let vm = this;
    vm.$set(vm, 'rowData', _.orderBy(vm.rowData, ['ItemType'], ['asc']));
},

Implementing this should solve the problem.

Answer №2

Is this the correct method?

@click="sortData('Category')"

sortData (value) {
  this.tableInfo = _.orderBy(this.tableInfo, value, 'desc')
},

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

Using React.render to render an empty subcomponent within a jsdom environment

Experimenting with React in node using jsdom has been an interesting challenge for me. I encountered an issue when attempting to render a component that includes another component with content, only to have the subcomponent's content fail to render. F ...

The method expression does not match the Function type specified for the mongoose Model

Encountered a perplexing error today that has eluded my attempts at finding a solution. const mongoose = require('mongoose'); const userSchema = mongoose.Schema({ name: {type:String, required:false}, birthday: {type:String, required:f ...

Show a brief glimpse of the JS/jQuery object on the screen

Exploring JavaScript objects today and encountered an unusual glitch. <form method="get" action="" name="start" > <fieldset> <label for="date">Date</label> <input type="date" name="date" id="date" /> <div>&nbsp; ...

Navigating the world of updating problems with Express.js

Currently, I am working on a MEAN stack project and facing challenges with the routing for updating. Saving operations are functioning correctly. In my Angular controller, I have defined the following scope method in which staff represents the object subm ...

Issue with JQuery event handlers becoming unresponsive upon resizing the window

JSBin link: http://jsbin.com/4QLDC/6 Here is the JS code snippet that I have written: var toggleContent = function (event) { $(event.target).siblings().toggle(); }; var showOrHidePanels = function () { var windowHeight = $(window).height(); v ...

What is the best way to accurately determine an element's location after scrolling to it?

I'm currently working on pinpointing the location of a specific element on a webpage once it has been scrolled to using JavaScript. Queries: How can I precisely determine the position of an element post-scroll? What are some common errors or extra st ...

Tips for designing a personalized payment page in PayPal for flexible one-time and subscription-based payments

How can I display a PayPal checkout page with custom fields such as tax and total amount when a user makes a payment for a custom amount? Can multiple fields like sales tax and total amount be added? In addition to that, our web application already has Pa ...

Tips for creating a sticky bootstrap column that remains in place as you scroll

I am looking to design a FAQ page similar to Twitter's FAQ. The goal is to have the left column remain fixed in its position while allowing users to scroll through the content. Here is what I have attempted so far, but it is not functioning as expect ...

Tips for checking the type radio button input with Angular.js

I want to implement validation for a radio button field using Angular.js. Below is the code snippet I am working with: <form name="myForm" enctype="multipart/form-data" novalidate> <div> <input type="radio" ng-model="new" value="true" ng- ...

Is JSON.stringify failing to function correctly in Mozilla Firefox?

Currently, I am attempting to convert an object into a string in javascript. After stringifying the object, I have noticed some discrepancies between different browsers. {"jobTypeArray":"[CONTRACT -W2]"} In Firefox and Chrome, the values appear as follow ...

Craft a circular design with an Arc and a Pie using the DIV DOM element

After creating an ellipse using the div DOM element, here's how I did it: var body = document.querySelector('body'); var div = document.createElement('div'); div.style.borderRadius = '50%'; div.style.border = '1px s ...

What is the method for retrieving an element by its index in a GLib GArray?

I've been working on an application that relies on GLib and its dynamic arrays. After checking out GLib.Array documentation, it seemed like I should be able to access array data using the data property within the structure. However, trying to access t ...

Unable to receive acknowledgment from child component within Redux

I am trying to retrieve props from Redux within a child component, but for some reason, {this.props} is showing up as an empty object. While I am successfully using react-redux connect to access the props in the parent component and pass them down to the ...

How do I declare the links in my navigation drawer?

I have successfully implemented a navigation drawer with dropdown menus. However, I'm struggling to figure out how to declare the routes so that clicking on the navigation names redirects me to the desired route. Can you guide me on how to properly de ...

Trying to toggle between two Angular components within the app component using a pair of buttons

Currently, I am developing an application that requires two buttons to display different nested apps. Unfortunately, I am unable to use angular routing for this particular design. These two buttons will be placed within the app.component. When Button A i ...

The <el-dropdown> function is currently facing a problem and is unavailable for use

While using Element-Plus, I've encountered an issue with the el-dropdown component. It doesn't display as a functional dropdown menu when imported into my project. Below is the code snippet I have used: <template> <div> <el- ...

Guard your state with Vuex-persistedstate from the prying eyes of sessionStorage

As I develop an interactive quiz web application using Vue js and .NET Web API, I've integrated JSON Web Token authentication to store tokens in local storage. Additionally, I utilize vuex-persistedstate for maintaining state across routes. Storing da ...

Issue encountered: Object is not functioning properly with Node.js AuthenticationExplanation: A TypeError occurred

As a newcomer to Stack Overflow, I am doing my best to ask this question clearly. I am currently following a tutorial step by step ( http://scotch.io/tutorials/javascript/easy-node-authentication-setup-and-local ) but I encountered an issue after the thir ...

Send a query to Express as a parameter

Currently, I have implemented an ejs file that contains a table element with clickable rows defined as follows: <tr onclick="myFunction(this)"> To pass a query parameter in an http request using javascript, I have the following code snippe ...

Is there a way to retrieve the password of the currently logged in user using Keycloak in JavaScript?

Does anyone know how to access the password of a logged-in user in a React application using Keycloak? I have successfully retrieved most of the necessary information from the idToken, like this: keycloak.init({ onLoad: 'login-required'}).then(fu ...