[英]Closest div as container for bootstrap datepicker in rails and coffee script and simple_form
I am trying to set up bootstrap datepicker in my rails application. 我试图在我的Rails应用程序中设置bootstrap datepicker。
It is working fine but the css is all messed up unless i assign a container div that can hold the pop up. 它工作正常,但是除非我分配可以容纳弹出窗口的div容器,否则CSS都一团糟。
I would like to make a generic method that checks the closest div for the popup instead of appending it to the body. 我想制作一个通用方法,该方法检查弹出窗口的最接近的div而不是将其附加到正文中。
Here is my code 这是我的代码
Coffee 咖啡
jQuery ->
jQuery('input.date_picker').datepicker
dateFormat: 'mm/dd/yyyy'
todayHighlight: true
container: jQuery(this).closest('div').attr(class)
autoclose: true
todayBtn: true
Erb b
<%= simple_form_for @qc_assay_parameter, :url => qc_assay_parameter_path(@qc_assay_parameter) do |f| %>
<%= f.error_messages %>
<%= f.input :reference_number, label: 'Reference #', error: 'Reference Number is mandatory' %>
<%= f.input :qc_assay_product_name_with_revision, label: 'Analyte , Rev#', url: '/qc_revisions', as: :autocomplete, placeholder: 'Type in Analyte Name', :input_html => {:class =>"span2", :rows => 6, :type => :text}%>
<%= f.input :assay_date, as: :date_picker, placeholder: 'mm/dd/yyyy' %>
<%= f.button :submit %>
<%end%>
Plain Html 纯HTML
<form id="edit_qc_assay_parameter_176094" class="simple_form edit_qc_assay_parameter" novalidate="novalidate" method="post" action="/qc_assay_parameter.176094" accept-charset="UTF-8">
<div style="margin:0;padding:0;display:inline">
<div class="input string required qc_assay_parameter_reference_number">
<div class="input autocomplete optional qc_assay_parameter_qc_assay_product_name_with_revision">
<label class="autocomplete optional control-label" for="qc_assay_parameter_qc_assay_product_name_with_revision">Analyte , Rev#</label>
<span class="ui-helper-hidden-accessible" role="status" aria-live="polite"></span>
<input id="qc_assay_parameter_qc_assay_product_name_with_revision" class="autocomplete optional span2 ui-autocomplete-input" type="text" value="LAP revision 0" size="30" rows="6" placeholder="Type in Analyte Name" name="qc_assay_parameter[qc_assay_product_name_with_revision]" data-autocomplete="/qc_revisions" autocomplete="off">
</div>
<div class="input date_picker optional qc_assay_parameter_assay_date">
<label class="date_picker optional control-label" for="qc_assay_parameter_assay_date">Assay date</label>
<input id="qc_assay_parameter_assay_date" class="string date_picker optional datepicker" type="date_picker" value="Sun, 03 Apr 2016 00:00:00 -0500" size="30" placeholder="mm/dd/yyyy" name="qc_assay_parameter[assay_date]">
</div>
<input class="btn" type="submit" value="Update Qc assay parameter" name="commit">
</form>
I would like to do something like 我想做类似的事情
jQuery ->
jQuery('input.date_picker').datepicker
dateFormat: 'mm/dd/yyyy'
todayHighlight: true
container: jQuery(this).closest('div').attr(class)
autoclose: true
todayBtn: true
container: jQuery(this).closest('div')
If the date_picker
class is always set on the input
then you can do this: 如果始终在
input
设置date_picker
类,则可以执行以下操作:
var nearestDivClassName = '.' + $('.date_picker').closest('div').attr('class')
jQuery('input.date_picker').datepicker
dateFormat: 'mm/dd/yyyy'
todayHighlight: true
container: nearestDivClassName
autoclose: true
todayBtn: true
Note that the container
option needs to be an element selector so we prepend a .
请注意,
container
选项必须是元素选择器,因此我们在前面加上.
to the string returned by .attr('class')
到
.attr('class')
返回的字符串
Update 更新资料
As you need it to be generic enough to account for more than one .date_picker
, you can use jQuery's .each()
method to iterate through them and then you can use this
as you expect/need: 当您需要足够通用的名称来说明一个以上的
.date_picker
,可以使用jQuery的.each()
方法对其进行迭代,然后可以根据需要使用this
:
$('input.date_picker').each(function(){
$(this).datepicker({
dateFormat: 'mm/dd/yyyy',
todayHighlight: true,
autoclose: true,
todayBtn: true,
container: closestDivClassName($(this))
});
});
function closestDivClassName($el){
return '.' + $el.closest('div').attr('class');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.