2017-06-30 21:20:27 +08:00
|
|
|
class CustomDatatable < AjaxDatatablesRails::Base
|
|
|
|
private
|
|
|
|
|
|
|
|
def dt_params
|
|
|
|
@dt_params ||=
|
|
|
|
params.permit(:assigned, :draw, :length, :start, search: %i(value regex))
|
|
|
|
.to_h
|
|
|
|
end
|
|
|
|
|
|
|
|
def columns_params
|
|
|
|
@columns_params ||= params.require(:columns).permit!.to_h
|
|
|
|
end
|
|
|
|
|
|
|
|
def order_params
|
|
|
|
@order_params ||=
|
|
|
|
params.require(:order).require('0').permit(:column, :dir).to_h
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_records
|
|
|
|
records = get_raw_records
|
2022-10-13 20:55:24 +08:00
|
|
|
records = filter_records(records) if dt_params.dig(:search, :value).present?
|
2021-07-16 16:49:41 +08:00
|
|
|
records = sort_records(records) if order_params.present?
|
2017-06-30 21:20:27 +08:00
|
|
|
records = paginate_records(records) unless dt_params[:length].present? &&
|
|
|
|
dt_params[:length] == '-1'
|
|
|
|
records
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort_records(records)
|
|
|
|
sort_by = "#{sort_column(order_params)} #{sort_direction(order_params)}"
|
|
|
|
records.order(sort_by)
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_sortable_displayed_columns
|
|
|
|
@sortable_displayed_columns = []
|
|
|
|
columns_params.each_value do |col|
|
|
|
|
@sortable_displayed_columns << col[:data] if col[:orderable] == 'true'
|
|
|
|
end
|
|
|
|
@sortable_displayed_columns
|
|
|
|
end
|
2018-11-09 22:58:08 +08:00
|
|
|
|
|
|
|
def formated_date
|
|
|
|
f_date = I18n.backend.date_format.dup
|
|
|
|
f_date.gsub!(/%-d/, 'FMDD')
|
|
|
|
f_date.gsub!(/%d/, 'DD')
|
|
|
|
f_date.gsub!(/%-m/, 'FMMM')
|
|
|
|
f_date.gsub!(/%m/, 'MM')
|
|
|
|
f_date.gsub!(/%b/, 'Mon')
|
|
|
|
f_date.gsub!(/%B/, 'Month')
|
|
|
|
f_date.gsub!('%Y', 'YYYY')
|
|
|
|
f_date += ' HH24:MI'
|
|
|
|
f_date
|
|
|
|
end
|
2017-06-30 21:20:27 +08:00
|
|
|
end
|