Active Admin date filter date format customisation

老子叫甜甜 提交于 2020-01-23 00:08:06

问题


Is there simple way to change the ActiveAdmin date filter display format from the default ISO format (yyyy-mm-dd)?


回答1:


Instead of overwriting the js you can provide extra options to the datepicker like this:

 = f.input :my_date, as: :datepicker, datepicker_options: { dateFormat: "mm/dd/yy" }



回答2:


The way i fixed is is like this:

$ ->
  # reset format
  $('.datepicker:not(.hasDatepicker)').each ->
    if $(@).val().length > 0
      p = $(@).val().split('-')
      $(@).val("#{p[2]}-#{p[1]}-#{p[0]}")

  # change format
  $(document).on 'focus', '.datepicker:not(.hasDatepicker)', ->
    $(@).datepicker dateFormat: 'dd-mm-yy'

So first reset the value from yyyy-mm-dd to yyyy-mm-dd, than set the correct format on the picker.

Hope this helps someone.

This works in ActiveAdmin pre 1 with the just_datetime_picker gem




回答3:


Yes, there is. I did it with setting options for datetime picker object with js

$(document).ready(function() {
  $(".datepicker").datepicker( "option", "dateFormat", 'D, d M yy' );

});

if you do this, input values will look like

Wed, 28 Nov 2012

http://docs.jquery.com/UI/Datepicker/formatDate here is manual about supported formats




回答4:


For Active Admin filters..

In the Active Admin gem, file: app/assets/javascripts/active_admin/pages/application.js.coffee, you'll see:

$ ->
  $(document).on 'focus', '.datepicker:not(.hasDatepicker)', ->
    $(@).datepicker dateFormat: 'yy-mm-dd'

You might have to revise the on focus event, for example (file: app/assets/javascripts/active_admin.js).

$(document).ready(function(){
  $(document).on('focus', '.datepicker.hasDatepicker', function() {
    $(this).datepicker( "option", "dateFormat", "mm-dd-yy");
  });
});

Or perhaps remove the events and re-initialize datepickers, for example:

$(document).ready(function(){

  // remove existing on focus events
  $._data( $(document)[0], 'events').focusin = null;

  // add new datepicker with format
  $('.datepicker').datepicker({dateFormat: 'mm-dd-yy'});

});


来源:https://stackoverflow.com/questions/12405230/active-admin-date-filter-date-format-customisation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!