Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Nivethan
Nivethan

Posted on • Originally published atnivethan.dev

A PetiteVue Tutorial - 05 Sorting

Now that we have searching and have rewired our project, it will be relatively straightforward to add sorting.

The first thing we will do is add 2 variables to our project. One to keep track of the column we are sorting on and another to keep track of if we are ascending or descending our sort.

createApp({searchString:"",search,filteredWorkers:[],sortColumn:"",order:"ASC",setSortColumn,getWorkers,workers:[],loaded:false,mounted,}).mount("#app");
Enter fullscreen modeExit fullscreen mode

We are going to set the order to ascending by default.

Now we can update the html, we will add some click events to the header.

<thead><th@click="setSortColumn('name')">Name</th><th@click="setSortColumn('position')">Position</th><th@click="setSortColumn('office')">Office</th><th@click="setSortColumn('age')">Age</th></thead>
Enter fullscreen modeExit fullscreen mode

We are going to call our setSortColumn function and this will set what column we will sort on and the direction. One thing to keep in mind is that we are specifying the key we are sorting on.

Now let's look at what our setSortColumn function will look like.

functionsetSortColumn(key){if(this.sortColumn===key){if(this.order==='ASC'){this.order='DESC';}else{this.order='ASC';}}else{this.order='ASC';}this.sortColumn=key;this.search();}
Enter fullscreen modeExit fullscreen mode

We first check if our column that we clicked is one that already have active. If this is the case, then we will need to flip the order based on what the current order is. If however we are clicking a new column to sort on, then we set the order to ASC by default.

We then set the sortColumn and called our search function. We will be updating our search function to sort the filteredWorkers list as that makes the most sense.

Our updated search function:

functionsearch(){this.filteredWorkers=this.searchString===""?this.workers:this.workers.filter(wo=>Object.values(wo).join("").indexOf(this.searchString)!==-1);constcolumn=this.sortColumnconstorder=this.order;this.filteredWorkers.sort(function(a,b){varnameA=a[column]+"".toUpperCase();varnameB=b[column]+"".toUpperCase();if(order==="DESC"&&nameA>nameB){return-1;}if(order==="DESC"&&nameA<nameB){return1;}if(nameA<nameB){return-1;}if(nameA>nameB){return1;}return0;});}
Enter fullscreen modeExit fullscreen mode

Once we search for our string, we can then sort our filteredWorkers by the column we have clicked on. We also need to sort by ascending or descending.

We should now be able to refresh our web page and be able to click on a heading to sort the table. We can also search for a string and then sort as well.

With that we are done! We have now used PetiteVue to do everything to set up a table that is searchable and sortable. Through that process we have hit the major elements of PetiteVue and we can now add interactivity and templating easily.

I really like petitevue for how simple it has kept everything and the best part is that most of this logic will work as is in Vue2 and Vue3. There are some slight changes that need to be made but for the most part the logic is all the same.

The Full Code

<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1"><title>PetiteVue Tutorial</title><linkrel="icon"href="data:;base64,="><style></style></head><body><divid="app"@vue:mounted="mounted"><divv-if="!loaded">Waiting for data...</div><divv-else><inputtype="text"v-model="searchString"v-on:keyup.enter="search"><button@click="search">Search</button><table><thead><th@click="setSortColumn('name')">Name</th><th@click="setSortColumn('position')">Position</th><th@click="setSortColumn('office')">Office</th><th@click="setSortColumn('age')">Age</th></thead><tbody><tbody><trv-for="worker in filteredWorkers"><td>{{worker.name}}</td><td>{{worker.position}}</td><td>{{worker.office}}</td><td>{{worker.age}}</td></tr></tbody></tbody></table></div></div><scripttype="module">import{createApp}from'/js/petite-vue.es.js'asyncfunctiongetWorkers(){constworkers=[{name:"Airi Satou",position:"Accountant",office:"Tokyo",age:33},{name:"Angelica Ramos",position:"Chief Executive Officer (CEO)",office:"London",age:47},{name:"Cedric Kelly",position:"Senior Javascript Developer",office:"Edinburgh",age:22},{name:"Jennifer Chang",position:"Regional Director",office:"Singapore",age:28},];this.loaded=true;returnworkers;}functionsearch(){this.filteredWorkers=this.searchString===""?this.workers:this.workers.filter(wo=>Object.values(wo).join("").indexOf(this.searchString)!==-1);constcolumn=this.sortColumnconstorder=this.order;this.filteredWorkers.sort(function(a,b){varnameA=a[column]+"".toUpperCase();varnameB=b[column]+"".toUpperCase();if(order==="DESC"&&nameA>nameB){return-1;}if(order==="DESC"&&nameA<nameB){return1;}if(nameA<nameB){return-1;}if(nameA>nameB){return1;}return0;});}functionsetSortColumn(key){if(this.sortColumn===key){if(this.order==='ASC'){this.order='DESC';}else{this.order='ASC';}}else{this.order='ASC';}this.sortColumn=key;this.search();}asyncfunctionmounted(){this.workers=awaitthis.getWorkers();this.filteredWorkers=this.workers;}createApp({searchString:"",search,filteredWorkers:[],sortColumn:"",order:"ASC",setSortColumn,getWorkers,workers:[],loaded:false,mounted,}).mount("#app");</script></body></html>
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromNivethan

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp