[英]Creating a filter in javascript to filter results through url
I feel like I am really close to figuring this out. 我觉得我真的很想弄清楚这一点。 Iv tried so many different ways.
iv尝试了许多不同的方式。
I want to create two selects
one for writing_type
and the other for sort_by
in the agreement
model, to filter the results. 我想在
agreement
模型中创建两个selects
一个用于writing_type
,另一个用于sort_by
,以过滤结果。 I think I need a different code in JS file for index.html
and possibly edit in my controller. 我想我需要在JS文件中为
index.html
使用不同的代码,并可能在控制器中进行编辑。
I found a piece of a solution to my problem, but i couldnt figure out the rest of what i need to make this work. 我找到了解决问题的方法,但是我无法弄清完成这项工作所需要的其余内容。
What i think i need to do: change the model variable to accept params[:writing_type]
and :sort_by
? 我认为我需要做的是:更改模型变量以接受
params[:writing_type]
和:sort_by
吗?
Index.html Index.html
<form accept-charset="UTF-8" action="/scribe_requests" method="get"> <input type='submit' value="go"> <select id="querySelct" name="writing_type"> <option value=":all" label="Writing Type">Writing Type</option> <option value="College Applications" label="College Applications">College Applications</option> <option value="College Essays" label="College Essays">College Essays</option> <option value="Business Papers" label="Business Papers">Business Papers</option> <option value="Resumes" label="Resumes">Resumes</option> <option value="High-School Essays" label="High-School Essays">High-School Essays</option> <option value="Scholarship Essays" label="Scholarship Essays">Scholarship Essays</option> <option value="Language Translation" label="Language Translation">Language Translation</option> </select> <select id="landingSelect" name= "sort_by"> <option value="created_at desc" label="Sort By">Sort By</option> <option value="created_at desc" label="Due Date(closer)">Due Date(closer)</option> <option value="created_at asc" label="Due Date(further)">Due Date(further)</option> </select> </form>
controller: 控制器:
def index
@agreements = Agreement.where("accepted = ?", false).where("due_date >= ?", DateTime.now).where("writing_type = ?", params[:writing_type]).order(params[:sort_by]).paginate(:page => params[:page], :per_page => 20)
end
Here is partial being called in index.html.erb 这是在index.html.erb中被部分调用的
</div class="agreements-list"> <%= render 'agreements/lists', agreements: @agreements %> </div>
The model agreements has columns that i want to sort it by and writing_types
that i want to filter with. 模型协议具有要
writing_types
排序的列和要过滤的writing_types
。
So the plan is to use the url and based off the url the list will change it uses &writing_type=1
for example to include at the end of the URL. 因此,计划是使用url,然后根据URL更改列表,并使用
&writing_type=1
进行更改,例如将其包括在URL的末尾。
How can i add the script URL to the main URL at the top and have it refresh on its own? 如何将脚本URL添加到顶部的主URL中并自行刷新? i can add ajax later!!
我可以稍后再添加ajax! Unless you have the ajax answer with it?
除非您有Ajax答案? thank you so much this is very important to me!
非常感谢,这对我非常重要! thank you!
谢谢!
I feels like you are doing something straightforward in quite a hard way. 我觉得您正在以一种非常困难的方式做一些简单的事情。 You should be able to achive what you want without all the JS by doing it in a form as follows:
通过使用以下形式的代码,您应该能够在没有所有JS的情况下实现所需的功能:
<form accept-charset="UTF-8" action="/agreements" method="get">
<input type='submit' value="go">
<select id="querySelct" name="writing_type">
<option value="0" label="Writing Type">Writing Type</option>
<option value="1" label="College Applications">College Applications</option>
<option value="2" label="College Essays">College Essays</option>
<option value="3" label="Business Papers">Business Papers</option>
<option value="4" label="Resumes">Resumes</option>
<option value="5" label="High-School Essays">High-School Essays</option>
<option value="6" label="Scholarship Essays">Scholarship Essays</option>
<option value="6" label="Language Translation">Language Translation</option>
</select>
<select id="landingSelect" name= "sort_by">
<option value="0" label="Sort By">Sort By</option>
<option value="1" label="Due Date(closer)">Due Date(closer)</option>
<option value="2" label="Due Date(further)">Due Date(further)</option>
</select>
</form>
By putting it in a form, with names against the selects, it should submit to the form path (I have guessed it is a get request to /agreements
which will route to your index action in the controller), with the parameters populated based on what option is selected 通过将其放入具有针对选择名称的表单中,它应提交到表单路径(我猜想这是对
/agreements
的get请求,该请求将路由到控制器中的index动作),并根据选择了什么选项
In your index action you should then be able to get the parameters by params[:writing_type]
and params[:sort_by]
然后,在索引操作中,您应该能够通过
params[:writing_type]
和params[:sort_by]
获取参数
Update: 更新:
To handle all writing types, do it in your index controller differently eg 要处理所有写入类型,请在索引控制器中进行不同的处理,例如
if params[:writing_type] == "all" #or whatever option represents all
@agreements = Agreement.all
else
@agreements = Agreement.where(writing_type: params[:writing_type])
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.