Generic form parser and server fetch with optional pagination of results. Vanilla Javascript.
›_ demo
- Sets query parameters automatically from named form inputs
- Updates the browser's URL and history with changed form inputs
- Make automatic GET requests on each form
change
andinput
event - Included but optional pagination of results
Install with npm
npm i @dotburo/form-digger
import {FormDigger} from 'src/FormDigger.js';
import {Paginator} from 'src/Paginator.js';
const paginator = new Paginator({
elements: document.querySelectorAll('.pagination'),
});
const digger = new FormDigger({
baseUri: '/api/v1/items/search',
form: document.querySelector('form'),
results: document.querySelector('.results'),
renderItem: item => `<li>Item: ${item.name}</li>`,
paginator: paginator,
});
The URI to which the query parameters will be appended to make the GET request. Default: empty string
The form HTML element which should be interpreted. Default: null
The HTML element to display the results. Default: null
Function to return HTML on empty results. Default parameters => '<div>No items found for these criteria</div>'
Function to render the result list wrapper. Default: html => `<ul>${html}</ul>
Function to render each item. Default: item => `<li>${JSON.stringify(item)}</li>
Property name in the server response which contains the results. Works with the default Laravel data
property. Default: 'data'
Optional Paginator instance, see src/Paginator
. Default: null
Delay server fetching by the number of milliseconds.
Minimum number of characters from which server requests will be made.
Events are dispatched on the form element.
Custom event at the start of the query and after rendering the results.
Custom event triggered on each form change.
HTML elements in which to render the pagination menu. Default: []
Map of key names of the server response properties required for the pages calculation. Works with the default Laravel pagination properties. Default:
{
current: 'current_page',
total: 'total',
perPage: 'per_page',
}
GNU General Public License (GPL). Please see the license file for more information.