jQuery transform Rails date datetime +
Si estan usando el datepicker de jquery-ui se puede hacer que los campos date_select o datetime_select de rails puedan ser reemplazados con el siguiente javascript que hice. En caso de que esten usando formtastic pueden usar este snipet http://snipt.net/boriscy/formtastic-datetime/,
- lo guardan como lib/custom_form_builder.rb
- En el config/environment.rb al final escriben include ‘custom_form_builder’
- Aumenten al final del archivo config/initializers/formtastic.rb Formtastic::SemanticFormHelper.builder = MyCustomFormBuilder
$(document).ready(function() {
// Define the dateFormat for the datepicker
$.datepicker._defaults.dateFormat = 'dd M yy';
/**
* Sets the date for each select with the date selected with datepicker
*/
$('input.ui-date-text').live("change", function() {
var sels = $(this).siblings("select:lt(3)");
var d = $.datepicker.parseDate($.datepicker._defaults.dateFormat, $(this).val() );
$(sels[0]).val(d.getFullYear());
$(sels[1]).val(d.getMonth() + 1);
$(sels[2]).val(d.getDate());
});
/**
* Replaces the date or datetime field with jquey-ui datepicker
*/
$(’.date, .datetime’).each(function(i, el) {
var input = document.createElement(’input’);
$(input).attr({’type’: ‘text’, ‘class’: ‘ui-date-text’});
// Insert the input:text before the first select
$(el).find(”select:first”).before(input);
$(el).find(”select:lt(3)”).hide();
// Set the input with the value of the selects
var values = [];
$(input).siblings(”select:lt(3)”).each(function(i, el) {
var val = $(el).val();
if(val != ”)
values.push(val);
});
if( values.length > 1) {
d = new Date(values[0], parseInt(values[1]) - 1, values[2]);
$(input).val( $.datepicker.formatDate($.datepicker._defaults.dateFormat, d) );
}
$(input).datepicker();
});
});
Aunque hay un problema con el Internet Explorer (solo probe con IE7), cuando uno cambia la fecha sin usar el datepicker sino escribiendola esta no es guardada correctamente, haber si lo resulven y lo testean con varios browsers.
Una recomendación que no la probe, es muy probable que el IE se solucione si el evento que se le asigne sea “blur”
Gracias Boris por el tip, pronto realizare uno sobre capistrano o vlad.
Saludos.