[英]Ruby/Rails button and search filtering
I've added the Active and Archived buttons to this page for extra filtering.我在此页面中添加了 Active 和 Archived 按钮以进行额外过滤。
The existing search box functionality uses the following js.coffee which calls the controller index to pull the data from the db.现有的搜索框功能使用以下 js.coffee 调用控制器索引从数据库中提取数据。
triggerSearch: (e) ->
if searchCompanyTimer
window.clearTimeout searchCompanyTimer
searchCompanyTimer = window.setTimeout(( ->
searchCompanyTimer = null
query_text = $(e.currentTarget).val()
el_id = $(e.currentTarget)[0].id
$.get( "companies", "q[name_cont]": query_text )
), 500, e)
I have added 2 similar js.coffee methods which set an active flag to true or false depending on which button was pressed.我添加了 2 个类似的 js.coffee 方法,它们根据按下的按钮将活动标志设置为 true 或 false。 Here is one of those methods.
这是其中一种方法。
triggerShowActive: (e) ->
if searchCompanyTimer
window.clearTimeout searchCompanyTimer
searchCompanyTimer = window.setTimeout(( ->
searchCompanyTimer = null
$.get( '/companies', {active: true} )
), 500, e)
here is part of my controller.这是我的控制器的一部分。
class CompaniesController < ApplicationController
respond_to :js, :json, :html
$active_inactive_flag = true
def index
puts "params are: #{params}"
puts "active param is: #{params[:active]}"
puts "@active_inactive_flag pre any conditions is: #{$active_inactive_flag}"
$active_inactive_flag = params[:active] ? params[:active] : $active_inactive_flag
puts "@active_inactive_flag after check on params[:active] is: #{$active_inactive_flag}"
if $active_inactive_flag.try :nonzero?
$active_inactive_flag = true
end
puts "@active_inactive_flag after check if it has a value, else true - is: #{$active_inactive_flag}"
@companies =
Company.search(params[:q])
.result
.order('created_at DESC')
.page(params[:page])
.per(Settings.paginate_limit)
.where(is_active: $active_inactive_flag)
end
here is my index.html.erb file where the buttons and search code is.这是按钮和搜索代码所在的 index.html.erb 文件。
<div class="row">
<div class="col-md-3">
<label>Filter By:</label>
<button class="show_active btn btn-primary" style="border-radius:10px">Active</button>
<button class="show_inactive btn btn-primary" style="border-radius:10px">Archived </button>
</div>
<div class="col-md-9">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">Filter by name</span>
<input class="form-control" id="q_name_cont" name="q[name_cont]" type="text">
</div>
</div>
</div>
I am using a global variable (tried instance and class variables also) in the controller but it's not working as expected.我在控制器中使用了一个全局变量(也尝试过实例和类变量),但它没有按预期工作。 Sometimes the value in
$active_inactive_flag
switches from false
to true
or vice versa incorrectly.有时
$active_inactive_flag
中的值会错误地从false
切换为true
,反之亦然。 I don't want to use a global variable but I am at a loss as to how to combine both filter and search correctly.我不想使用全局变量,但我不知道如何正确组合过滤器和搜索。 When the page loads we want Active button to be on by default and return active companies.
当页面加载时,我们希望活动按钮默认打开并返回活动公司。 The problem I am having is knowing what button was pressed when the search box is used.
我遇到的问题是知道使用搜索框时按下了哪个按钮。
Edit: Here is an example of what is happening.编辑:这是正在发生的事情的一个例子。
Any direction would be grateful.
任何方向将不胜感激。
Thanks Dave,谢谢戴夫,
in js.coffee
added the following:在
js.coffee
中添加了以下内容:
returnActiveInctiveFlag = true
and then to the triggerSearch updated to:然后将 triggerSearch 更新为:
$.get( "companies", { "q[name_cont]": query_text, active: returnActiveInctiveFlag } )
In my new methods updated returnActiveInctiveFlag
accordingly.在我的新方法中相应地更新
returnActiveInctiveFlag
。
Was really overcomplicating it.真的太复杂了。 Thanks again for getting me thinking in the correct way.
再次感谢您让我以正确的方式思考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.