Rails数据表选择过滤器

时间:2016-06-10 00:56:18

标签: javascript jquery ruby-on-rails datatable coffeescript

我的SKU有一个ajax数据表。为此,我使用ajax-datatables-rails gem。搜索和排序工作完美,但现在我尝试在我的表格中添加过滤功能,它似乎没有做任何事情。我将此示例用于过滤函数:https://datatables.net/examples/api/multi_filter_select.html。 在示例中,选择框是在页脚中绘制的,但对我来说页脚是空的。就像代码根本没有运行一样。我也没有收到任何错误。

我在我的coffeescrip文件(assets / javascripts / vendor_skus.js.coffee)中初始化我的数据表,所以我不得不把它翻译成coffeescript。我没有经历过coffeescript或者使用带有导轨的ajax,所以我很遗憾出了什么问题。

我如何解决我的问题:

标准选择框对我的情况有问题,因为我在我的表中使用AJAX,而选择框似乎只能在客户端表上正常工作。我决定制作自己的自定义过滤器,而不是使用标准选择框。这些是常规选择框,如下所示:

<%= select_tag "store-id", options_from_collection_for_select(@stores, "id", "name"), include_blank: true, class:"store-id form-control" %>  
<%= select_tag "status", options_for_select([ "Open", "On Hold", "Cancelled", "Closed", "Error" ]), include_blank: true, class:"form-control", multiple:true %>

这是我的coffeescript,让jQuery将参数提交给服务器并重新加载表onchange:

$ ->
  $('#orders-table').DataTable
    processing: true
    serverSide: true
    retrieve: true
    pageLength: 50
    title: 'orders'
    lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]]
    ajax: data: (d) ->
      d.store_id = $('#store-id').val();
      d.status = $('#status').val();
      return

$ ->
  $('#store-id').on 'change', ->
    $('#orders-table').DataTable().ajax.reload()
    return

$ ->
  $('#status').on 'change', ->
    $('#orders-table').DataTable().ajax.reload()
    return   

在控制器中,确保将参数传递给Datatables,如下所示:

respond_to do |format|
  format.html
  format.json { render json: OrderDatatable.new(view_context, { store_id: params[:store_id], status: params[:status] }) }
end

然后在您的Datatable文件中,使用参数来过滤结果。在这种情况下,我使用多选状态,所以当选择空白值时,参数[:status] .present?结果是真的。这就是我添加支票以查看第一项是否为空字符串的原因。

  def get_raw_records
    # insert query here
    query = Order.all
    query = query.status(params[:status]) if params[:status].present? && (params[:status].count == 1 && params[:status][0] == "") == false 
    query = query.store(params[:store_id]) if params[:store_id].present?
    query.joins(:store)
  end

2 个答案:

答案 0 :(得分:1)

我在实现这个时遇到了同样的问题。我发现问题出在这一行:

column.search((if val then '^' + val + '$' else ''), true, false).draw()

咖啡脚本不喜欢以下内容:

  

,true,false

删除后如下:

column.search(if val then '^' + val + '$' else '').draw()
一切顺利。需要注意的是,我不是一个javascript / coffeescript人,所以对结果造成的负面影响超出了我的范围。但是和我一样,我正在努力让所有结果出现在可选择的下拉过滤器中。它只显示当前数据页面中的任何唯一值 - 这没有帮助。

仅供参考,要获得分页功能,请转到您的datatable.rb文件,并将正确的行取消注释到引用您正在使用的分页的顶部。我正在使用&#34; will_paginate&#34;对于bootstrap,所以我看起来像这样:

  include AjaxDatatablesRails::Extensions::WillPaginate

希望有所帮助。您是否有机会在选择过滤器中找到显示所有结果的方法?

答案 1 :(得分:0)

我使用 ajax-datatables-rails gem 的 ajax 数据表过滤器的工作代码。

在数据表视图中,我在数据表上方创建了一个表来输入范围变量,然后添加一些 javascript 以在更改时重新加载数据表:

<table>
        <tbody><tr>
            <td>Minimum CWD:</td>
            <td><input type="text" id="minCWD" name="minCWD"></td>
        </tr>
        <tr>
            <td>Maximum CWD:</td>
            <td><input type="text" id="maxCWD" name="maxCWD"></td>
        </tr>
    </tbody></table>

<script>
$(document).ready(function () {             
            // other options
            var table = $('#example').DataTable()
                    
            $("#minCWD").change(function () {
              table.ajax.reload();
            });
            $("#maxCWD").change(function() {
              table.ajax.reload();
            });
        });
</script>

然后将过滤器变量添加到ajax调用中(在coffee文件中):

ajax: {
      url: $('#example').data('source'),
      beforeSend: (xhr) => xhr.setRequestHeader('Content-Type', 'application/json'),
      data: (d) ->
        $.extend {}, d, 'minCWD': $('#minCWD').val(),
        $.extend {}, d, 'maxCWD': $('#maxCWD').val()
    }
// note: the beforeSend may not be required

然后在model_datatable.rb中添加一个过滤器:

def get_raw_records
    #YOUR TYPICAL SELECTION...note: I'm using AREL and joining schools with pstats
    #now filter by your max min variables
    if params['minCWD'].present?
      schools = schools.where(pstats[:cwd_percent].gteq(params['minCWD']))
    end
    if params['maxCWD'].present?
      schools = schools.where(pstats[:cwd_percent].lteq(params['maxCWD']))
    end
    return schools
  end

我的控制器如下所示:

respond_to do |format|
      format.html
      format.json { render json: ExampleDatatable.new(params, view_context: view_context) }
end

这里的工作示例:https://schoolsparrow.com/arizona/schools

相关问题