HI WELCOME TO KANSIRIS

How to Build an Ionic Calendar App

Leave a Comment
Out of the box there is no Ionic Calendar component to display a classic calendar inside your app, but what if you want to give people the chance to add events and display everything inside a good-looking view?
Like always we can rely on a helpful third-party package called Ionic calendar. In this tutorial we will build our own Ionic calendar app to add events into an almost Apple like looking calendar view! Of course we could also access the device calendar to add events, but that’s not helping us regarding showing some sort of table to the user.
Once done our app will look like in the image below, where we can insert new events into our own custom calendar view.
ionic-date-app

Get the Ionic Calendar Template

Enter your Email below to receive the needed files directly to your inbox!
Powered by ConvertKit

Setting up the Ionic Calendar App

As always we start with a blank new Ionic app. We create another page where we will add details for a new event, and we install the ionic-calendar package plus 2 more packages. The first is needed as a dependency of the calendar, the second is Moment.js which helps us to transform some of our dates more easily than we could do with pure Javascript.
Go ahead now and start your app like this:
To make use of the calendar we also need to import it, so open your src/app/app.module.ts and insert:

Building the Ionic Calendar View

The view of our main calendar is actually super easily, because all the heavy work is done by our package. Inside the nav bar we add a button to call our addEvent() function, and inside the content we only add the calendar.
We take a look at all the options in a second, for now simply add this to your src/pages/home/home.html:
We haven’t actually used everything of that plugin, but what we used so far is:
  • eventSource: The data we feed to the calendar view
  • currentDate: Needed to mark “today”
  • onEventSelected: Handle a tap on a single event
  • onTitleChanged: Happens when we swipe to a new month
  • onTimeSelected: Happens when we select a day or time
  • step: Used for accuracy of a day or week view
You can check out all options here again.
Now that we got the view let’s connect it with our controller. As we see we need to create some functions, and most of them are pretty basic.
When we want to add an event, we will show a new page to set up the information. Once the modal view is dismissed we create the event and push it to our eventSource. To update the information inside the view we also need to use setTimeout so the data will be reloaded inside the view.
If we click on a single event we will simply alert out some information. This is a point where you could also drill deeper into a day view or show more information for a single event.
Make sure your controller contains all the needed information and functions for the calendar, so go ahead and change your src/pages/home/home.ts to:
We are now ready to run our Ionic Calendar app, but we can’t add any events so let’s wrap this up with the last missing piece.

Adding User Events

We already implemented the function to display our second view, so this view will allow the user to insert some details for a new event. Of course we could also store some own information here and improve the date display, but for now this is enough.
Open your src/pages/event-modal/event-modal.html and change it to:
We just add a bunch of standard Ionic inputs and controls to build out the event object of that class.
Speaking of the class, here we only have a few interesting points. For one, we pass in the currently selected date from our calendar view. By doing this we can automatically set the start date to that previously selected date like any good calendar!
When we want to save or add the event, we will simply dismiss the page and pass the current event as an object. We already added the logic to catch this data and create a new event from it, so now we only have to add this to our src/pages/event-modal/event-modal.ts:
We are done and you should be able to see your calendar and add your own events now!

Conclusion

With the help of this plugin it’s actually not really hard to implement a calendar with Ionic. You can of course change the CSS so it fit’s your app, and you could add more logic to store and retrieve events from your own backend.
This tutorial should give you a good starting point if you want to implement your own calendar!

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.