Agenda
The blog will teach us how to work with events and communicate with different components using events.
We also implement Pagination and see how can we generate a Global Pagination Class.
Blog Details
This blog gives us a brief about how can we communicate between different Lightning Web Components and use custom events and play with them.
And we also implement pagination using events in this blog and made it generic.
What we are going to do
In this blog, We create custom events and use that event to handle pagination dynamically.
Requirements:
You need to have access to your org.
Should have installed the VS Code Installed in your system.
You should have some basic knowledge of Lightning Web Component.
You must have some proper records in your org to show data on the table.
Events in Lightning Web Component
Lightning Web Component dispatch standard DOM events. Components can also create and dispatch custom events.
The DOM events system is a programming design pattern :
An event name, Called type.
A configuration to initialize the event.
A JavaScript object that emits the event
To create events, should use a CustomEvent interface. CustomEvent provides a more consistent experience using across browsers, including Internet Explorer.
Create and Dispatch Event:
To create an event, Use CreateEvent(), and to dispatch event, call the EventTarget.dispatchEvent() method.
Example:
this.dispatchEvent(new CustomEvent('EventName'));
‘this’ keyword is used because a field is shadowed by a method or constructor parameter.
‘dispatchEvent’ is used to Dispatch the event and ‘CustomEvent’ is used to create a new Custom event and this method contains a string that works as the name of the event.
Pagination Using Lightning Web Component:
HTML SYNTAX:
When we click buttons, previousHandler or nextHandler function executes. These functions create and dispatch the previous and next event.
JavaScript Syntax:
These events don’t pass any data Payload into the DOM tree. Pagination is a child component that can drop anywhere we required it.
Parent Component to use pagination:
HTML Syntax:
To listen to events, use an HTML attribute with the syntax oneventtype.
JavaScript Syntax:
When the Parent component receives the previous or next events, previousHandler or nextHandler increases or decreases the page number.
Blog’s Conclusion:
So, this blog gives us a brief about how we can communicate between different Lightning Web Component using events and how can we use events as the follow-up commands.
We also created pagination (not fully but you can put your logic accordingly) and see how can we use a component inside a component and understand the concept of the parent-child relationship between components.

Informative !!
ReplyDelete