bootstrap datapicker component using simple form - ruby-on-rails

I am struggling to add a f.input simple form line of code to add a bootstrap datepicker with component.
Following the datepicker examples I can add the bootstrap datepicker with component using the following code:
<div class="input-append date" id="datepicker" data-date-format="dd-mm-yyyy">
<input class="span2" size="16" type="text" value="12-02-2013" readonly>
<span class="add-on"><i class="icon-th"></i></span>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('[data-behaviour~=datepicker]').datepicker();
})
</script
how can I rewrite the above div as a f.input simple_form line of code

application.js or any .js file
$(function() {
$(".datepicker" ).datepicker({dateFormat: "dd/mm/yyyy"});
});
view file
<div class="input-append date">
<%=f.text_field :field_name, :class=>'datepicker'%>
<span class="add-on"><i class="icon-th"></i></span>
</div>

For my project, i used this : https://github.com/Nerian/bootstrap-datepicker-rails.
It's a good gem and easy to use
In my project, i create a date_time_input.rb file in app/inputs/date_time.input.rb for override simple form date time input.
class DateTimeInput < SimpleForm::Inputs::DateTimeInput
def input
#template.content_tag(:div, super)
"#{#builder.text_field(attribute_name, input_html_options)}".html_safe
end
end
Into the view :
= f.input :date, :required => true, :input_html => { “data-behaviour” => “datepicker” }
And JS :
$(document).ready(function(){
$(document).on(“focus”, “[data-behaviour~=’datepicker’]”, function(e){
$(this).datepicker({“format”: “dd/mm/yyyy”, “weekStart”: 1, “autoclose”: true});
});
});
This is my article for do that in french :-D
http://blog.jumboweb.fr/post/42841191962/datepicker-bootstrap-simple-form-avec-rails

Related

Tempusdominus datetimepicker and ASP.NET MVC

I try to use https://tempusdominus.github.io/bootstrap-4/ in ASP.NET MVC project.
HTML
<div class="input-group date" data-target-input="nearest">
#Html.TextBoxFor(model => model.TimeEnd,
new { #class = "form-control form-control-sm datetimepicker-input",
#data_toggle = "datetimepicker",
#data_target = "#TimeEnd",
type="text"
})
<div class="input-group-append" data-target="#TimeEnd" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
Where model.TimeEnd is string in the format dd.MM.yyyy HH:mm
JS
$(document).ready(function () {
$('#TimeEnd').datetimepicker({
locale: 'es',
});
});
When I open page the input has correct value but it is not visible in the datetimepicker.
UPDATE #1
If I set defaultDate via the same model like defaultDate: '#Model.TimeEnd' (where it is string in the format dd.MM.yyyy HH:mm) then the Spanish localization is gone and I see English calendar! Wow.
And the following error appears
moment-with-locales.min.js:1 Deprecation warning: value provided is
not in a recognized RFC2822 or ISO format. moment construction falls
back to js Date(), which is not reliable across all browsers and
versions. Non RFC2822/ISO date formats are discouraged and will be
removed in an upcoming major release. Please refer to
http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments: [0] _isAMomentObject: true, _isUTC: false, _useUTC: false,
_l: undefined, _i: 14.08.2018 00:00, _f: null, _strict: false, _locale: [object Object] Error
at Function.createFromInputFallback (http://localhost:62959/Scripts/moment-with-locales.min.js:1:3368)
at wa (http://localhost:62959/Scripts/moment-with-locales.min.js:1:21353)
at va (http://localhost:62959/Scripts/moment-with-locales.min.js:1:22064)
at Sa (http://localhost:62959/Scripts/moment-with-locales.min.js:1:22146)
at l (http://localhost:62959/Scripts/moment-with-locales.min.js:1:209)
at k.i.getMoment (http://localhost:62959/Scripts/tempusdominus-bootstrap-4.min.js:6:14261)
at k.i.defaultDate (http://localhost:62959/Scripts/tempusdominus-bootstrap-4.min.js:6:19874)
at String. (http://localhost:62959/Scripts/tempusdominus-bootstrap-4.min.js:6:14947)
at Function.each (http://localhost:62959/Scripts/jquery-3.3.1.js:360:19)
at k.i.options (http://localhost:62959/Scripts/tempusdominus-bootstrap-4.min.js:6:14895)
Any clue how to fix this issue?
It seems the probelm was around messing up the input and wraping div ids.
HTML
<div class="form-group row">
#Html.Label("Finish", new { #class = "col-sm-2 col-form-label", #for = "TimeStart2" })
<div class="col-sm-10">
<div class="form-group">
<div class="input-group date" id="datetimepicker1" data-target-input="nearest">
<input id="TimeEnd" name="TimeEnd" value="#Model.TimeEnd" type="text" class="form-control form-control-sm datetimepicker-input" data-target="#datetimepicker1" />
<div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
</div>
</div>
</div>
JS
$(document).ready(function () {
$('#datetimepicker1').datetimepicker({
locale: 'es'
});
});

Open a text box when other is selected in dropdown list in rails

I have a table "fundings" in which there is a field "status", for which i have a select field in the form. The options for this select field are ["approved", "declined", "pending"]. What i want is when "declined" is selected, a further text box shows to explain the reason for decline. Please help how can this be done.
<%= form_for([#parent, #child, #funding], :html => {class: "form-horizontal",role: "form"}) do |form| %>
<div class = "form-group">
<div class="control-label col-sm-2">
<%= form.label :status %>
</div>
<% if current_user.admin? %>
<div class="col-sm-8">
<%= form.select :status,['Pending', 'Approved', 'Declined'], class: "form-control" %>
</div>
<% else %>
<!-- Disabled for non-admin users -->
<% end %>
</div>
<!-- Submit button here -->
<% end %>
Update
<div class="form-group">
<%= "Status" %>
<%= form.select :status, ['Pending', 'Approved', 'Declined'], {}, id: "sample-status-select", class: "form-control" %>
</div>
<div class="form-group">
<%= "Decline Reason" %>
<%= form.text_area :decline_reason, class: "form-control hidden", id: "decline-reason-textarea" %>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<%= form.submit "Apply", class: 'btn btn-primary btn-lg' %>
</div>
</div>
</div>
</div>
<% end %>
<script type="text/javascript">
<plain>
$(function() {
$("#sample-status-select").on("change", function() {
var select_val = $(this).val();
console.log(select_val);
if (select_val === 'Declined') {
$("#decline-reason-textarea").removeClass("hidden");
} else {
$("#decline-reason-textarea").addClass("hidden");
$("#decline-reason-textarea").val("");
}
});
});
</plain>
</script>
$(function() {
$("#sample-status-select").on("change", function() {
var select_val = $(this).val(); // this gets the value of the dropdown menu
console.log(select_val); // this just displays the selected value in the browser console (if you have the browser console open)
if (select_val === 'Declined') {
// if the 'Declined' option is chosen
// we remove the 'hidden' class from the textarea
$("#decline-reason-textarea").removeClass("hidden");
} else {
// if any other option is chosen
// we put back the 'hidden' class to the textarea
// also, we update the textarea value to BLANK (this part is optional, it depends if you want to keep the value of the textarea)
$("#decline-reason-textarea").addClass("hidden");
$("#decline-reason-textarea").val("");
}
});
});
.hidden {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="https://httpbin.org/post" method="post">
Status
<select id="sample-status-select">
<option value="Pending">Pending</option>
<option value="Approved">Approved</option>
<option value="Declined">Declined</option>
</select>
<br>
<br> Decline Reason
<textarea id="decline-reason-textarea" class="hidden">
</textarea>
</form>
Check this snippet I made. It should work for you as well.
This is a basic html form so this works even without ruby on rails.
After you get the gist of this, you should be able to port for it to work with your rails app.
<script type="text/javascript">
$(function() {
$("#sample-status-select").on("change", function() {
var select_val = $(this).val();
console.log(select_val);
if (select_val === "Declined") {
$("#decline-reason-textarea").removeClass("hidden");
} else {
$("#decline-reason-textarea").addClass("hidden");
$("#decline-reason-textarea").val("");
}
});
});
</script>

Error using bootstrap datetimepicker

I'm trying to use bootstrap-datetimepicker from datetimepicker, but when the date picker is displayed, this covers the input.
The code that I use is something like this:
<div class="col-md-6" >
#Html.LabelFor(model => model.BeginDate)
<div class="form-group" id="">
#Html.TextBoxFor(model => model.BeginDate, new { #class = "form-control" , #id="datetimepicker"} )
</div>
#Html.ValidationMessageFor(model => model.BeginDate)
</div>
I initialize the Datetimepicker like this:
$(document).ready(function ()
{
$("#datetimepicker").datetimepicker({
defaultDate: '',
showTodayButton: true,
format: 'dd-mm-yyyy',
showClose: true,
showClear: true,
toolbarPlacement: 'top',
stepping: 15,
autoclose: true,
startView:3,
minView: 2,
language: 'es'
});
});
Bootstrap v3.0.2
ASP MVC 5
I believe your issue is because you are missing a div wrapping your control the class “input-group”. This gives it a relative position which is required as the picker is positioned absolute.
The docs have the following example
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar</span>
</span>
</div>
</div>
https://eonasdan.github.io/bootstrap-datetimepicker/

How to use radio button to not send data to the server?

I am able with the radio button and a little javascript to toggle between the two categories, but even if one category is hidden it's data still sends.
challenge.rb
class Challenge < ActiveRecord::Base
scope :oneshot, -> { where(categories: 'One-Shot') }
scope :ongoing, -> { where(categories: 'Ongoing') }
_form
<%= form_for(#challenge) do |f| %>
<%= f.text_field :action %>
<% Challenge::CATEGORY.each do |c| %>
<%= label(c, c) %>:
<%= f.radio_button(:category, c, :class => "date-format-switcher", checked: (c=='One-Shot')) %>
<% end %>
<div id='id_of_first_div'>
<%= f.date_select :deadline %>
</div>
<div id='id_of_second_div'>
<%= f.date_select :date_started %>
</div>
<% end %>
<script>
$(function(){
$('#id_of_second_div').hide();
$('#challenge_category_one-shot').click(function(){
$('#id_of_first_div').show().attr('disabled', false);
$('#id_of_second_div').hide().attr('disabled', true);
});
$('#challenge_category_ongoing').click(function(){
$('#id_of_first_div').hide().attr('disabled', true);
$('#id_of_second_div').show().attr('disabled', false);
});
});
</script>
Radio Button HTML
<input class="date-format-switcher" type="radio" value="One-Shot" checked="checked" name="challenge[category]" id="challenge_category_one-shot" />
<input class="date-format-switcher" type="radio" value="Ongoing" name="challenge[category]" id="challenge_category_ongoing" />
Adapting Fiddle
<input id='a-selector' type='radio' name='selector' value='A' checked/> A
<input id='b-selector' type='radio' name='selector' value='B'/> B
<div id='details'>
<div id='for-a'>
<%= f.date_select :deadline, include_blank: true, selected: Date.current %>
</div>
<div id='for-b'>
<%= f.date_select :date_started, include_blank: true, selected: Date.current %>
</div>
</div>
<script>
var removed = $('#for-b').detach();
$('#a-selector').click(function() {
$('#details').append(removed);
removed = $('#for-b').detach();
})
$('#b-selector').click(function() {
$('#details').append(removed);
removed = $('#for-a').detach();
})
</script>
Setting a form element as disabled only disables user interaction with it; it doesn't remove it from the form's data (although your jQuery code is actually setting the div to disabled, which does nothing, but fixing that wouldn't help at all). What you want to do instead is clear the value of the date elements, e.g.,
<script>
$(function(){
$('#id_of_second_div').hide();
$('#challenge_category_one-shot').click(function(){
$('#id_of_first_div').show();
$('#id_of_second_div').hide();
$('#id_of_second_div select').value('')
});
$('#challenge_category_ongoing').click(function(){
$('#id_of_second_div').show();
$('#id_of_first_div').hide();
$('#id_of_first_div select').value('')
});
});
</script>
Though for that to work, you will need to add include_blank: true as an option to your date_select tags:
<%= f.date_select :deadline, include_blank: true %>
This will add the empty value as an option to your date selects, so that the above code can reset them to that empty value.
I'd also like to note -- all of this is just front-end logic, which any malevolent (or curious) user can circumvent. Your backend code (i.e., your controller) should handle discarding the fields you don't need based on your business logic, too.

unable to trigger select event on jquery autocomplete

i have a search form whith jquery autocomplete on some fields.
<form id="prjs_form" class="prjcts" action="<wp:action path="/ExtStr2/do/enpim/projects/list.action" />" method="post">
<div class="row bord">
<div class="wid_ut">
<span class="label"><wp:i18n key="ACRONYM" /></span>
<span class="field"><input id="autAcronym" type="text" name="bean.acronym" value="${bean.acronym}" class="lrg autocomplete" /></span>
</div>
......................
<div class="row">
<div class="act_btns">
<input type="submit" name="search" value="<wp:i18n key='SEARCH'/>">
</div>
</div>
</form>
My need is to submit the form when I select one of the values ​​suggested by autocomplete.
this is my jquery
jQuery('#autAcronym').autocomplete({
params: { type: 'project_acronym' },
paramName: 'text',
serviceUrl: '<wp:info key="systemParam" paramName="applicationBaseURL" />json/hall.ShowParameters',
minChars: 2,
select: function(event, ui) {
if(ui.item){
$(event.target).val(ui.item.value);
}
$('#prjs_form').submit();
return false;
}
});
when i start typing on field the autocomplete work fine, but when i select one of the suggested value the select event doesn't trigger....
what's wrong?
the other similar post on stackoverflow doesn't solved my problem
thanks in advance

Resources