There’s a lot of information online about how a modern grid UI component should look like and what features it should provide. One common requirement is for the grid component to be easy to visualize and act on the information.
That’s easy to accomplish using Vue.js and in this article we’ll build a grid component that comprises the following user experience:
I’ll assume you have Vue.js installed.
Note that the code below is not production-ready and is presented here for educational purposes.
The code below is a modified example taken from the official Vue.js documentation.
Create a new single-file component, Grid.vue, with the following code:
Now import this component to your App.vue
file and add some data:
Use the component by passing the corresponding data:
Next let’s extend this component with the additional functionality we talked about in the introduction.
I’m using a technique from the article The Holy Grail: Pure CSS Scrolling Tables with Fixed Headers and the markup is already setup according to this article. Define a height for the table and make table body scrollable:
First let’s define the number of rows per page, and the start position. For this will add two new properties:
Let’s add the markup for the navigation that shows the current page number:
To change between pages add click events handler for our buttons. @click=movePages(-1)
for the Back button and @click=movePages(1)
for the Next button.
This method will count the number of rows that is first on the page:
Now add a computed property that will filter and return the rows for the current page:
We now have to display updated data, since we’re able to move between pages. Change .table-body
from filteredData to dataPerPage:
To allow to set the number of rows add a simple select with v-model equal to the rowsPerPage property.
For options we will set the pageSizeMenu property to an array with number values:
And this is all you need to have the ability to change the number of rows per page. Vue will do the rest of the work, thanks to two-way data binding.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Nice!! its vue pure!.. thanks for code