Lily

Lily

Is there any way to include a calendar of events in my Phoenix Project?

Is there any way to include a calendar of events in my Phoenix Project?

I would like to show these fields (with dates) in a calendar:

I would like to make new fields through a calendar as well. Is it possible? Please help me :cry:

Most Liked

carlosarli

carlosarli

+1 for full calendar.io here :slight_smile: I have used it for my master thesis project and it worked pretty well for me. I have a demo here if you want to have a look and see if that suits your needs: https://youtu.be/pH58U4tmZoU.
Let me know in case you need some help, although I should say I am a noob with Phoenix and Elixir so don’t rely too much on the way I implemented the solution it might not follow best practices :wink:

dokuzbir

dokuzbir

you can achieve that via javascript, ajax and fullcalendar library.

or you can create simple weekly view like that and you can generate hardcoded fields dynamically

  dates= ["2018-06-01", "2018-06-02", "2018-06-03", "2018-06-04", "2018-06-05", "2018-06-06"],
  fake_database_appointmens = [
    %{date: "2018-06-01"},
    %{date: "2018-06-01"},
    %{date: "2018-06-02"},
    %{date: "2018-06-03"}
  ]
render(conn, "whatever.hmtl", dates: dates, fake_database_appointmens: fake_database_appointmens)

and in whatever.html.eex

<div class="container">
<div class="row">

<%= for date <- @dates do %>

<div class="col"><h6><%= date %></h6> 

<%= for appointment <- Enum.filter(@fake_database_appointmens, fn x -> x.date == date end ) do %>
<div class="btn btn-primary"><%= appointment.date %></div>
<% end %>

</div>
<% end %>

</div>
</div>
joaoevangelista

joaoevangelista

As the guys pointed out, fullcalendar is the best to deal with that kind of requirement, here are some steps to help you figure it all out.

  • You need to add fullcalendar on your project by either using a script tag or using brunch and npm
  • On your current template used to render the table of appointments change it to include a simple div <div id="calendar" style="width:600px"></div>.
  • On a javascript file include the following snippet to render the calendar on the div you placed: (Events Docs)
// get the calendar element with jquery
 const calendarDiv = $('#calendar');
// check if the div is on the page
 if (calendarDiv) {
   // load the fullcalendar on div
   calendarDiv.fullCalendar({
     // events is a url to your controller that respond with json
     events: "/appointments/calendar"
   });
 }
  • Now you need to handle the events path you just added on js side. On the router.ex, add a route next to the already present resources("/appointments", AppointmentController): get("/appointments/calendar", AppointmentController, :calendar), which will add the /appointment/calendar.
  • Go to your AppointmentController. Now you need to create an action named calendar, there you will handle the data fetching for the current view of calendar such as the date range, you will get a url /appointiment/calendar?start=2013-12-01&end=2014-01-12&_=1386054751381, then you can parse the parameters, convert to a date time format and pass it to the Ecto query. Here is an example:

defmodule MyApp.AppointmentsController do
  # ... other actions
  
  def calendar(conn, %{"start" => start_date, "end" => end_date} = _params) do
    data = 
      AppointmentsContext.fetch_appointments(start_date, end_date)
      |> AppointmentsConverter.convert()
    # instead of render and a view, use json to a quick json serializing and return
    json(conn, data)
    
  end
end

Here some notes on the implementation on the example:

  • The functoin fetch_appointments needs to convert the start_date and end_date into a date time format to be usable on ecto such as Ecto.DateTime. Next we pipe the list resulting of the query to a function that convert the structs of Appointments to an map or new struct that conforms with fullcalendar’s Event Object that will return as json.

Then its done.
Hope this helps you

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

We're in Beta

About us Mission Statement