ruby 具有选择过滤器等的Ajax DataTable Rails(Client DashBoard示例)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 具有选择过滤器等的Ajax DataTable Rails(Client DashBoard示例)相关的知识,希望对你有一定的参考价值。

## app/datatables

class ClientDashboardDatatable < AjaxDatatablesRails::Base
  include AjaxDatatablesRails::Extensions::Kaminari
  def_delegators :@view, :params, :link_to, :image_tag, :select_tag, :options_from_collection_for_select, :edit_sound_path, :sound_path

  def sortable_columns
    @sortable_columns ||= ['Sound.created_at']
  end

  def searchable_columns
    @searchable_columns ||= []
  end

  private

  def data
    records.map do |sound|
      [
        sound.created_at.strftime("%d/%m/%y %H:%M"),
        sound.title,
        [image_tag("/assets/flags/#{sound.language.id}.png", class: 'label-flag')],
        sound.status,
        #CORRECTORS VALIDATORS COORDINATORS
        [
          if sound.user.client_correctors.count > 0
            if sound.correction
              sound.correction.corrector ? sound.correction.corrector.email : "None"
            else
              select_tag "Corrector", options_from_collection_for_select(sound.user.client_correctors, "id", "email"), prompt: "Choose a Corrector..", class: 'corrector-autocomplete'
            end
          end
        ],
        [
          if sound.user.client_validators.count > 0
            if sound.correction
              sound.correction.validator ? sound.correction.validator.email : 'None'
            else
              select_tag "Validator", options_from_collection_for_select(sound.user.client_validators, "id", "email"), prompt: "Choose a Validator..", class: 'validator-autocomplete'
            end
          end
        ],
        [
          if sound.user.client_coordinators.count > 0
            if sound.correction
              sound.correction.coordinator ? sound.correction.coordinator.email : 'None'
            else
              select_tag "Coordinator", options_from_collection_for_select(sound.user.client_coordinators, "id", "email"), prompt: "Choose a Coordinator..", class: 'coordinator-autocomplete'
            end
          end
        ],
        [ if !sound.correction
            link_to(I18n.t('sound.create_correction'), '#', class: "btn btn-warning create-correction-btn", "data-id": sound.id)
          else
            "Correction created on "+sound.correction.created_at.strftime("%d/%m/%y at %H:%M")
          end
        ]
      ] - [[nil]]

    end
  end

  def get_raw_records
    result = Sound.where(user_id: options[:current_user].id, state: [*(-20..19), (100..199)])
    result = result.where('lower(title) like ?', "%#{params["search"]["value"].downcase}%") if params["search"]["value"]
    params["columns"].each do |column, option|
      next unless option["searchable"] == "true" && option["search"]["value"].present?
      result = result.where("#{option["name"]} = ?", "#{option["search"]["value"]}")
    end
    result
  end
end
table#client_dashboard_table data-source="/client/datatable"
  thead
    tr.filters
      th style="text-align: center" =t('sound.index.table.creation_date')
      th style="text-align: center" =t('sound.index.table.title')
      th style="text-align: center" =select(:sound , :language_id, options_from_collection_for_select(Language.all, "id", "name"),  prompt: "All Langs", include_blank: false)
      th style="text-align: center" =t('sound.index.table.status')
      -if @client_correctors.count > 0
        th style="text-align: center" Correctors
      -if @client_validators.count > 0
        th style="text-align: center" Validators
      -if @client_coordinators.count > 0
        th style="text-align: center" Coordinators
      th style="text-align: center" =t('sound.index.table.action')
  tbody
dashboard = $('#client_dashboard_table').DataTable
      processing: true
      serverSide: true
      ajax: $('#client_dashboard_table').data('source')
        # url: '/sounds/datatable'
      pagingType: 'full_numbers'
      columns: defColumns()
      "aaSorting": []
      initComplete: ->
        $('.corrector-autocomplete').select2
          placeholder: 'Choose a Corrector..'
          allowClear: true

        $('.validator-autocomplete').select2
          placeholder: 'Choose a Validator..'
          allowClear: true
        $('.coordinator-autocomplete').select2
          placeholder: 'Choose a Coordinator..'
          allowClear: true

        $('.filters select').on 'change', (e) =>
          th = $(e.target).closest("th")
          @api().column(th.index()).search($(e.target).val()).draw()
          
          # Re-Render the Select2 Fields after th Filter Draw
          $('.corrector-autocomplete').select2
          placeholder: 'Choose a Corrector..'
          allowClear: true

          $('.validator-autocomplete').select2
            placeholder: 'Choose a Validator..'
            allowClear: true
          $('.coordinator-autocomplete').select2
            placeholder: 'Choose a Coordinator..'
            allowClear: true
gem 'jquery-datatables-rails'
gem 'ajax-datatables-rails', github: 'ajahongir/ajax-datatables-rails'

以上是关于ruby 具有选择过滤器等的Ajax DataTable Rails(Client DashBoard示例)的主要内容,如果未能解决你的问题,请参考以下文章

Logstash:使用 Ruby 过滤器

jquery ajax 历史和书签插件

使用 Ruby 的“准备好了吗?”带有gets、puts等的IO方法

ruby 我似乎总是需要这种方法来获取文件等的适当时间戳。

请求Ajax成功后刷新表

使用 jQuery 的具有复杂功能的弹出窗口