npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sirmekus/vuelar

v1.0.1

Published

A Laravel pagination implementation for Vuejs

Downloads

2

Readme

vuelar

vuelar (Vue-Laravel) is a vue package that takes care of pagination for a Laravel-powered backend on a Single-Page Application. This package assumes familiarity with Laravel and requires vue-router for navigation. This package takes care of extra query parameters that may be appended to a URL as well and not just the 'page=*' query. This way, if your query in the backend is based on some logic or detail, it will still be preserved.

Installation

$ npm install @sirmekus/vuelar

For starters, this package takes care of the navigation URL and updates the browser so that you can watch the route parameter change and perform any desired action.

Usage

After Installation, in your app.js file, all you have to do is import the package like so:

import { createApp } from 'vue';
import  vuelarPlugin  from "@sirmekus/vuelar"

let app = createApp({});
app.use(vuelarPlugin)

//...continue doing other operations like component registration, etc. After which you can then mount your component(s)

Now the package will be available globally and you can reference or use it wherever you want pagination like so (please note that styling is done using Bootstrap 5 so you may want to check it out):

<template>

//template content goes in here. For instance, displaying all users of the platform

<vuelar :links="state.result"  />

</template

It is good practise to include the pagination component only when it's needed or necessary so the above code can be restructured to:

<template>

//template content goes in here. For instance, displaying all users of the platform

<vuelar v-if="state.result.last_page > 1" :links="state.result"  />

</template

Overview

A typical laravel pagination-generated response looks (and should) like this. This is the condition necessary for this package to work.

current_page: 1
data: []
first_page_url: "https://example.com/api/users?page=1"
from: 1
last_page: 2
last_page_url: "https://example.com/api/users?page=2"
links: [{url: null, label: "&laquo; Previous", active: false},…]
next_page_url: "https://example.com/api/users?page=2"
path: "https://example.com/api/users"
per_page: 2
prev_page_url: null
to: 2
total: 3

In laravel, displaying this pagination has already been taken care of in blade when you do {{ $result->links() }} (asssumption: after feeding our view data via a variable called "$result"). On the front-end, however, we don't have any sure-fire, any-size-fits-all solution for this. This is where VueLar comes to the rescue.

It is important that the variable being passed to the :links variable expected by the vuelar component be reactive. This is because this variable will typically contain data that will change as the pagination changes, thus needs to be 'refreshed' or 'recalculated' as well. To accomplish this you will typically have a function that calls an api after/during/before your component mounts (to avoid repeating yourself). After your component must have been mounted all you have to do is watch the URL bar for changes and then call this function. This package appends a query to the end of your browser url (whatever the URL may be) when a pagination link is clicked which you should then fetch and append as query to your API url. Let's take an example (we'll use the Composition API as it's encouraged but this this package is not limited to using only the Composition API):

<template>

//template content goes in here. For instance, displaying all users of the platform

<vuelar v-if="state.result.last_page > 1" :links="state.result"  />

</template>

<script type="text/javascript" >
import { reactive, watch } from 'vue'
import { useRoute } from "vue-router"

export default {
    setup() {
        const route = useRoute()
        const endpoint = apiEndpoint //The api endpoint to your Laravel-powered backend where you fetch your data
        const state = reactive({
            result: null, //this is where we'll typical 'save' the result to be used in (y)our template
        })

    //Watch when the URL on the browser tab changes, fetch any available query appended there and then append it to your endpoint URL.
    watch(() => route.params, (toParams, previousParams) => {
            getResult(endpoint, route.query)
        })
        
    async function getResult(endpoint, data=null){
        try {
            const response = await axios.get(endpoint, {
                params: data
            })
            state.result = response.data
        } catch (error) {
           //do whatever your like here
        }
    }

    getResult(endpoint, route.query)

    return {
            state
        }
    }
}
</script>

From the above, whenever the URL bar changes the api endpoint will be called with the appended paginated query and the state will be 'refreshed'. The actual URL in the browser won't be changed (since in a typical SPA this URL will likely be different from the API endpoint(s)) and any exiting query won't be removed or replaced, only the api-generated pagination query will be appended to it so that you can watch it and do whatever you like- typically to call the endpoint again.

I hope you enjoy the package. Greater days ahead