JQuery Datepicker in Bootstrap modal - jquery-ui

I'm working with Javascript only for a couple of months, and am stuck with this issue. I have a page with 2 modals, which contains a JQuery-UI-Datepicker. First modal creates an entry with start date, and the second modal allows making changes to this entry.
First modal works perfectly. When I try to change the date in the second modal, nothing happens. I can select a month, a year, but when I select a date, the calendar form disappears, and the value is not changed.
I assumed that the datepicker hide behind the modal as here,
but the solution didn't work out for me. The second assumption was like in this question but it wasn't helpful for me either.
Here's the code I work with (I didn't write it myself):
$(document).on("click",".datepicker",function() {
var first_datepicker = $(".datepicker:first");
if($(this) !== first_datepicker || $(this).datepicker('getDate') == null) {
$(this).datepicker('setDate',first_datepicker.datepicker('getDate'));
}
});
// First modal
.modal.fade{id: "create_ea_bahncard", role: "dialog", tabindex: "-1"}
.modal-dialog{role: "document"}
.modal-content
= semantic_form_for #ea_bahncard,url: finance_ea_bahncards_path(person_id: person),html_options: {method: :post} do |f|
= f.input :person_id, as: :hidden
.modal-header
.row
.col-md-10
%h2
Neue Bahncard
.col-md-2
%button.close{type: :button, data: {dismiss: 'modal'}, aria: {hidden: 'true'}}
%i.fa.fa-times-circle
.modal-body
.row
.col-md-5
= f.input :bahncard_type, as: 'string', label: "Bahncard-Typ", input_html: {class: "form-control"}
= f.input :valid_until, as: 'bs_jquerydate', label: "Gültig bis", input_html: {class: "form-control"}
.col-md-5
= f.input :total_amount, as: 'string', label: "Voller Betrag", input_html: {class: "form-control"}
.modal-footer
= submit_tag 'Speichern', class: 'btn btn-default btn-primary'
%button.btn{data: {dismiss: 'modal'}} Abbrechen
// Second modal
.modal.fade{id: "ea_bahncard_#{bc.id}"}
= semantic_form_for bc,url: finance_ea_bahncard_path(bc),html_options: {method: :put} do |f|
= f.input :person_id, as: :hidden
.modal-header
.row
.col-md-10
%h2
Bahncard bearbeiten
.col-md-2
%button.close{type: :button, data: {dismiss: 'modal'}, aria: {hidden: 'true'}}
%i.fa.fa-times-circle
.modal-body
.row
.col-md-5
= f.input :request_date, as: 'string', label: "Antragsdatum", input_html: {class: "form-control"}
= f.input :bahncard_type, as: 'string', label: "Bahncard-Typ", input_html: {class: "form-control"}
= f.input :valid_until, as: 'bs_jquerydate', label: "Gültig bis", input_html: {class: "form-control"}
.col-md-5
= f.input :proportional_amount, as: 'string', label: "Anteiliger Betrag", input_html: {class: "form-control"}
= f.input :total_amount, as: 'string', label: "Voller Betrag", input_html: {class: "form-control"}
.modal-footer
= submit_tag 'Speichern', class: 'btn btn-default btn-primary'
%button.btn{data: {dismiss: 'modal'}} Abbrechen
I have the idea of selecting only the second modal for the function, but didn't implement it yet.
Can you please share you ideas of what to do?

After a couple of days I found a solution.
First, it turned out that there were several input fields and the code was written the way it chose only the first input field. That is the reason other fields did not respond.
For solving that i added a selected class to the clicked element.
Second, the selected input field had the same id as the a class above it. This question was a great help. I changed the id and it worked.
The function now looks like this:
$(document).delegate(".datepicker", 'click', function() {
$('.datepicker').removeClass('selected');
$(this).addClass('selected');
$(this).datepicker({dateFormat: 'dd-mm-yy'});
$(this).datepicker('setDate', new Date($(this).val()));
});
.modal-body
.row
.col-md-3
= f.input :requested_at, as: 'bs_jquerydate', label: "Eingangsdatum", input_html: {class: "date", id: "input_edit_request_#{request.id}"}
.modal-body
.row
.col-md-5
= f.input :valid_until, as: 'bs_jquerydate', label: "Gültig bis", input_html: {class: "form-control", id: "input_ea_bahncard_#{bc.id}"}
Hope this helps others.

Related

Ruby on Rails - Can't access the updated model from Slim View

I have a view for editing my model. I am using Slim views.
My model_name is 'cupcake'
= simple_form_for [:admin, :order, record] do |f|
.row
.col-md-6
label for="_kind" Kind
= f.select :kind, [['Clipart', :clipart], ['Image', :image], ['Message', :message]], { selected: record[:kind] }, value: record[:kind], class: 'form-control', :input_html => { :name => "cupcake[:kind]" }
- if #record[:kind] == 'image'
.col-xs-12
= f.input :image, as: :shrine_file
- elsif #record[:kind] == 'clipart'
.col-md-6
label for="_clipart" Clipart
= f.select :clipart_id, options_for_select(#cliparts.map { |c| [c['name'], c['id'], {'value' => c['id']}] }, selected_key = selected_clipart(record).id), { selected: selected_clipart(record).name }, class: 'form-control', :input_html => { :name => "cupcake[:clipart_id]" }
- else
.col-md-6
= f.input :text_line_style, collection: ["Three Line Text","Single Line Text"], checked: record['first_line'] ? 'Three Line Text' : "Single Line Text", as: :radio_buttons, item_wrapper_class: "inline"
.row
.col-xs-12
hr
button.btn.btn-success.btn-lg
= fa_icon('check')
| Save
- redirect_location = [:admin, :cliparts]
= link_to redirect_location, class: 'btn btn-primary btn-lg'
= fa_icon('chevron-left')
| Back
.col-xs-12
- if f.object.created_at.present?
p.help-block Created at: #{f.object.created_at}
- if f.object.updated_at.present?
p.help-block Updated at: #{f.object.updated_at}
I want to update the view based on the kind selected. Currently, it is not updating the view if I select a different kind. When I try to access cupcake[:kind], it is showing undefined error. Any way to access the cupcake object? I looked for it in the f object. But, I couldn't find it.
Anyone, please help?

Simple_form with Bootstrap: create 2 inline fields in a horizontal form

I am trying to create 2 inline fields (minimum age and maximum age fields) in a generally horizontal form but nothing is working.
=simple_form_for([:admin, #group], :html => {:class => 'form-horizontal', :remote => true }, wrapper: :horizontal_form, wrapper_mappings: {check_boxes: :horizontal_radio_and_checkboxes, radio_buttons: :horizontal_radio_and_checkboxes, file: :horizontal_file_input, boolean: :horizontal_boolean}, ) do |f|
=f.input :title
=f.input :desc, :label => "Description"
=f.input :minimum_age,:wrapper_html => { :style => 'display: inline !important' }
=f.input :maximum_age, :wrapper_html => { :style => 'display: inline !important' }
=f.input :include_null_birthday, as: :radio_buttons, label_html: {data: {toggle: "tooltip", title: "Some comment.", placement: "right"}}, label: "Include people with no birthday set [?]"
Does anyone know how this can be done?
It's a bit late but as this one is easy :
<div class="button-inline">
<%= f.input :minimum_age,:wrapper_html %>
<%= f.input :maximum_age, :wrapper_html %>
</div>
In your css :
.button-inline {
display: flex;
justify-content: space-between;
}

text area in form - cursor won't start at the top

I'm working on a contact form and have two queries:
the text area displays correctly, but when the user starts typing in it, the cursor begins 2 lines down (and also the text runs over one line (i.e. keeps going in one long straight line) rather than staying within the box borders
I'd like to add inline styling for the label but I can't seem to get the styling code in the right place.
View code:
= simple_form_for #contact, :html => {:class => 'form-horizontal' } do |f|
.field
= f.input :name, input_html: { class: 'gtb_text_field' }
.field
= f.input :email, input_html: { class: 'gtb_text_field' }
-#.field
-#= f.input :type, as: :select, collection: WHY_CONTACT, label: 'Type of enquiry', allow_blank: false
.form_field_wrapper
= f.input :message, label: 'My message' , input_html: {class: 'gtb_text_area', style: 'margin-left: 50px; width: 310px;'}
.actions
= f.submit 'Send message', class: 'signup_button'
Can anyone help?

Rails - simple form

I use simple_form gem in my Rails app. When I use code like this
= f.input_field :name, label: false, placeholder: 'Name'
= f.input_field :price, label: false, placeholder: 'Price'
= f.input_field :description, label: false, placeholder: 'Description'
= f.input_field :image, label: false, placeholder: 'Image'
I get HTML for input:
<input class="string required textform" id="item_name" label="false" maxlength="255" name="item[name]" placeholder="Имя" size="255" type="text">
As you can see size of input is 255 now. Actually it's much more than enough. How can specify the size of inputs?
Following is from simple_form documentation here
It is also possible to pass any html attribute straight to the input,
by using the :input_html option, for instance:
<%= simple_form_for #user do |f| %>
<%= f.input :username, input_html: { class: 'special' } %>
<%= f.input :password, input_html: { maxlength: 20 } %>
<%= f.input :remember_me, input_html: { value: '1' } %>
<%= f.button :submit %>
<% end %>
To set size of 100 for name input:
= f.input :name, label: false, placeholder: 'Name', input_html: { size: 100 }

Strong_parameters in view with mixed models

I am using strong parameters and when I try to save the following form I get the following message.
undefined method `household_params' for # '<'VisitsController:0x007fa88deec428'>'
I am confused because I am using the visits controller and not the households controller - The visit is associated with a household as shown below and I call the view with the following code:
= link_to 'New Visit', {controller: 'visits', action: 'new',
household_id: household.id, method: 'post'}, class: 'btn btn-primary'
The Form is:
%h3 Household: #{household.name}
%h4 Household Members: #{household.neighbors.count}
%h4 Visits: #{household.visits.count}
= simple_form_for visit do |f|
= f.input :visited_on, :label => 'Visited On', as: :date_picker, input_html: { class: 'span2' }
= f.input :starch, :label => false, collection: ['Beans','Rice','Potatoes'],selected: 'Beans'
= f.input :cereal, :label => false, collection: ['Cereal','Grits','Oatmeal']
= f.input :option1, :label => false, collection: ['Peanut Butter Jelly', 'Deserts','Baby Fromula'], prompt: 'Options'
= f.input :items_received, :label => 'Special Needs',input_html: {rows: 4, class: 'span9' }
= f.input :notes, :label => 'Notes',input_html: {rows: 4, class: 'span9' }
= f.button :submit, :class => 'btn-primary', :label=> 'Save'
The form works fine without the three lines that display information about the household
I am thinking strong_parameters is getting confused

Resources