gpt4 book ai didi

javascript - Filterrific gem 没有按预期刷新或过滤我的表

转载 作者:太空宇宙 更新时间:2023-11-04 16:14:16 25 4
gpt4 key购买 nike

我是一个相对新手,我正在尝试使用 filterrific gem 进行一些过滤。基本上,我有一个显示 vendor 列表的索引页面,我希望能够使用过滤来主动过滤/排序记录。我能够完成教程并解决所有错误,但由于某种原因,当您将数据输入表单时,没有任何反应。它根本没有刷新 View 。对我做错了什么有什么想法吗?

vendor 型号:

    filterrific(
default_filter_params: { sorted_by: 'created_at_desc' },
available_filters: [
:sorted_by,
:search_query,
:with_vendor_type_id,
:with_created_at_gte
]
)

scope :search_query, lambda { |query|

return nil if query.blank?
terms = query.downcase.split(/\s+/)

terms = terms.map { |e|
(e.gsub('*', '%') + '%').gsub(/%+/, '%')
}

num_or_conds = 2
where(
terms.map { |term|
"(LOWER(vendors.name) LIKE ? OR LOWER(vendors.bio) LIKE ?)"
}.join(' AND '),
*terms.map { |e| [e] * num_or_conds }.flatten
)
}

scope :sorted_by, lambda { |sort_option|

direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
case sort_option.to_s
when /^created_at_/
order("vendors.created_at #{ direction }")
when /^name_/
order("LOWER(vendors.name) #{ direction }, LOWER(students.bio) #{ direction }")
when /^vendor_type_title_/
order("LOWER(vnedor_types.title) #{ direction }").includes(:vendor_type)
else
raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
end
}

scope :with_vendor_type_id, lambda { |vendor_type_ids|
where(with_vendor_type_id: [*vendor_type_ids])
}

scope :created_at_gte, lambda { |reference_time|
where('vendors.created_at >= ?', reference_time)
}


def self.options_for_sorted_by
[
['Name (a-z)', 'name_asc'],
['Registration date (newest first)', 'created_at_desc'],
['Registration date (oldest first)', 'created_at_asc'],
['Type(a-z)', 'vendor_type_id_asc']
]
end

我的 vendor Controller

 def index

@filterrific = initialize_filterrific(
Vendor,
params[:filterrific],
select_options: {
sorted_by: Vendor.options_for_sorted_by,
with_vendor_type_id: VendorType.options_for_select
},
persistence_id: 'shared_key',
default_filter_params: {},
available_filters: [],
) or return

@vendors = @filterrific.find.page(params[:page])

# Respond to html for initial page load and to js for AJAX filter updates.
respond_to do |format|
format.html
format.js
end

rescue ActiveRecord::RecordNotFound => e
puts "Had to reset filterrific params: #{ e.message }"
redirect_to(reset_filterrific_url(format: :html)) and return
end

我的索引 View

<h1>Vendors</h1>

<%= form_for_filterrific @filterrific do |f| %>
<div>
Search
<%= f.text_field(
:search_query,
class: 'filterrific-periodically-observed'
) %>
</div>
<div>
Vendor Type
<%= f.select(
:with_vendor_type_id,
@filterrific.select_options[:with_vendor_type_id],
{ include_blank: '- Any -' }
) %>
</div>
<div>
Registered after
<%= f.text_field(:with_created_at_gte, class: 'js-datepicker') %>
</div>
<div>
Sorted by
<%= f.select(:sorted_by, @filterrific.select_options[:sorted_by]) %>
</div>
<div>
<%= link_to(
'Reset filters',
reset_filterrific_url,
) %>
</div>
<% end %>

<%= render(
partial: 'vendors/list',
locals: { vendors: @vendors }
) %>

我的部分

<div id="filterrific_results">

<div>
<%= page_entries_info vendors %>
</div>

<table>
<tr>
<th>Name</th>
<th>User ID</th>
<th>Country</th>
<th>Registered at</th>
</tr>
<% vendors.each do |vendor| %>
<tr>
<td><%= link_to(vendor.name, vendor_path(vendor)) %></td>
<td><%= vendor.user_id %></td>
<td><%= vendor.vendor_type %></td>
<td><%= vendor.created_at %></td>
</tr>
<% end %>
</table>
</div>

<%= will_paginate vendors %>

JS文件

<%# app/views/vendor/index.js.erb %>
<% js = escape_javascript(
render(partial: 'vendors/list', locals: { vendors: @vendors })
) %>
$("#filterrific_results").html("<%= js %>");

我已经到了 from 渲染的地步,并克服了所有错误消息,但它实际上并没有进行过滤。

最佳答案

在“app/assets/javascripts/application.js”中包含 //= require filterrific/filterrific-jquery

关于javascript - Filterrific gem 没有按预期刷新或过滤我的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41196403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com