Display addition of day to a localdate - thymeleaf

I use thymeleaf 3.
I display a localdate with
<div th:text="${sampling.buildDate}" th:remove="tag"></div>
is it possible to add a number of day to this date?

You'll want to manipulate the date on the server and display it on the front-end. This allows you to unit test and re-use the code, as well as keep the front-end logic easier for a UI designer to understand.
In your #GetMapping method:
model.addAttribute("modifiedBuildDate", modifiedBuildDate);
in the HTML:
<div th:text=""${#temporals.format(modifiedBuildDate, 'dd-MM-yyyy HH:mm')}"" th:remove="tag">Some modified date</div>
If you must manipulate it on the front-end (and I wouldn't recommend it),
you can try the utility ${#temporals.create(year,month,day)} and modify the date as needed.

Related

datePicker : get the TD element object of a DAY

I want to be able to find the parent "td" of a day on a datepicker calendar, but can't find a way to do this.
Example of the outpout of a datepicker :
<td class=" " data-year="2013" data-month="6" data-event="click" data-handler="selectDay">
<a class="ui-state-default" href="#">22</a>
</td>
There is a "data-year", "data-month" but no "data-day".. why ? I don't want to have to fetch each element to find the good one for every day object i want to get.. i should be able to access a day element directly...
The purpose is to add a class to the days i want (like an highlight class for example). I can't use "beforeShowDay"...everyone is talking about this, but it don't work for me since i need to add the class after the page load (example: booking a specific date by ajax and updating the calendar by highlighting the selected days without reloading the whole calendar or page)
I don't want to use "onselect" too, i need to be able to highlight my dates (by adding a class) without this AND be able to do it after the calendar is loaded.
To be more simple, i would like to access a day element like i access a div with
$('#divname").css(...)
for example. Or a similar way to change directly a day element.
Thanks for the help

Translating HTML5 Form Helpers in rails

I am using the date_field_tag.
<%= date_field_tag :date_from, value = nil, options = {} %>
The date picker is displayed in English, with the machine locale set to is.I am wondering if it is possible to translate the date picker text using I18n.t? Or is this not possible since the date picker is provided by the browser?
The Rails helper date_field_tag generates an input field with a type of "date", but it's not a widely supported field type. You can either let the browser render the field and hope for the best, or use a polyfill to provide support for users who don't have a browser with the functionality. You could also use something like the jQuery UI datepicker for everybody, regardless of whether the browser supports date fields or not.
Your options for translating the control depend on which approach you take. If the browser is drawing the calendar widget, you can't control the language it uses. If you're providing a calendar widget via JavaScript, then you can - it's just HTML, after all. jQuery's datepicker provides a lot of localisation options.
You can try out how your browser displays native date-related fields in this JSFiddle.

What are the benefits, if i use rails view form template?

Example, i made a form like this
<form name="register" method="post" enctype="multipart/form-data">
<p><h3>User check</h3></p>
<p>admin ID: <input type="text" name="userid"></p>
<p>admin Pass: <input type="password" name="password"></p>
<input type="submit" name="apply" value="Submit"></p>
<p> </p>
</form>
and my manager wants to change this form to rails form template like this,
<%= form_for(:model) do |form| %>
<p>
<%=form.label :input%>
<%=form.text_field :input, :placeholder => 'Enter text here...'%>
</p>
<%end%>
My question is, it works fine with html based front code. Why do i have to change this to rails code? I just want to keep my front-end code...I don't know why i have to change this :(. Also, I'm new on Ruby on Rails. That is the main reason. I dont' want to change the existing code, if it is working.
I really hate this job. I have to translate all the attributes to the rails code and that makes me really exhausted :(
Form builders are here to help
Form helpers are supposed to make your life simpler. They are quicker to write than their pure html alternative, provided you don't write pure html first.
They also provide a lot of easy implementations for difficult integration pieces, like :
displaying a date selection select group
mirroring the fact that a check box has been unchecked in POST params
automatically adding multipart param on form if you add a file input (not actually difficult to achieve, but easy to forget)
... and many more
Further development
All of this is about comfort, and you may think you could avoid it if you already have a perfectly working pure html implementation.
But what happen if someone later has to add a date select input in your form ? She will have to use the rails helper, since it saves a lot of time in controller part to set date in database. But she won't be able to leverage form builder, since you haven't used it.
Now, she has to choose between using a non builderdate_select tag mixed in pure html or ... to rewrite your form completely. As you may realize, mixing different styles is generally considered ugly, and we don't like ugly in ruby.
Security
Form tag helpers also provide an important security measure : CSRF protection. Every time you use a rails helper to create a <form> tag, it automatically adds an hidden input containing a secret key. That key has to be posted with form data to prove request originated from current website.
If you use plain html forms, you won't have this security. You could of course add the token manually using the correct method, but this would again be more time wasting than simply using form helpers.
Bottom line
The main problem is that you write pure html before using rails helpers - that is what is wasting time.
Some benefits of using Rails form helpers are:
Consistent naming of input elements names and ids
i18n support for labels
generated URL with form_for is less error prone
Better handling of checkboxes
Nice helpers like options_for_select
Less typing
That last ones might be my favourite: less typing.

Can we change the event calendar size dynamically on Rails?

I'm using event calendar plug-in on rails 2.3.8 and I need to know whether I can change the calendar size dynamically. For example in one page I need to show calendar in full page in another page I need to put that same calendar in small div. Is it possible with the event calendar plug-in ?
Without the real example, I can only give a sketch how it could possibly work:
Include the view in something like:
<div id="smallcal">
<%= event_calendar %>
</div>
Add then a stylesheet that copies the rules of event_calendar.css, but modifies them like in the following example:
#smallcal > .ec-calendar { ... modified rules here ... }
So your modified rules will match better only for your variant where you want to make the calendar smaller. Perhaps it is possible to ensure that the calendar is smaller by including:
div#smallcal {
width: 200px;
...
}

Is there a benefit to using the HtmlHelper in MVC?

Is there a specific reason why I should be using the Html.CheckBox, Html.TextBox, etc methods instead of just manually writing the HTML?
<%= Html.TextBox("uri") %>
renders the following HTML
<input type="text" value="" name="uri" id="uri"/>
It guess it saves you a few key strokes but other than that. Is there a specific reason why I should go out of my way to use the HtmlHelpers whenever possible or is it just a preference thing?
Another benefit is that if your ViewData contains a value matching the name of the field it will be populated.
e.g.
ViewData["FirstName"] = "Joe Bloggs";
<%=Html.TextBox("FirstName") %>
will render
<input type="text" value="Joe Bloggs" id="FirstName" />
There are huge benefits:
It has overloaded methods to pre-populate the values (formatted, and safe for HTML) just like the ViewState.
It allows built in support for the Validation features of MVC.
It allows you to override the rendering by providing your own DLL for changing the rendering (a sort of "Controller Adapter" type methodology).
It leads to the idea of building your own "controls" : http://www.singingeels.com/Articles/Building_Custom_ASPNET_MVC_Controls.aspx
One thing is for consistency...I for one always forget the name attribute. Plus, you can extend the functions for your own projects. They're not called helpers for nothing!
The upside to using an abstraction layer is future proofing your code in a pluggable way. Maybe today, you create HTML 4 pages but tomorrow you want to create XHTML pages or XAML or XUL. That's a lot of changes if you just hard code the tags everywhere, especially if you've got hundreds of pages. If everything is calling this library, then all you've got to do is rewrite the library. The downside is that it is usually considered to be slightly less readable by humans. So, it most probably increases the cognitive demand on your maintenance programmers. These advantages and disadvantages really have nothing to do with MVC.
It actually auto populates your textbox based upon first your ViewData.Model.uri and second by ViewData["uri"]. Doing it manually you'd need to do <input value="<%Html.Encode(ViewData.Model.Uri"%>" />
I haven't been doing MVC too long, but I've already written some extension methods to generate menu tabs based on Html.ActionLink. It allows me to be consistent with my usage and, if I decide to change how my CSS menus work, only modify a single method to output the new tab format.
The other use that I have made of them is in conditional output using ViewData to supply values to the controls.

Resources