导轨AJAX - 让一个下拉选择填充表导轨、AJAX

2023-09-10 13:55:48 作者:咬着棒棒糖甩天下

我试图让选择从下拉列表填充表在同一个页面,使用AJAX,这样的变化似乎不需要刷新页面。我得尽可能制定出了code远程调用,并添加自定义操作的控制器,但我不知道如何去得到返回的信息转换成我已经得到了表。目前,该表是刚刚经历的所有项目循环,但我希望它只是显示所选的一个项目的信息。

我会怎么需要在JS写(RJS?)文件得到它处理返回的信息,而我需要什么样的变化,使现有的表,以确保它显示了正确的信息?

谢谢!

下拉(与AJAX调用):

 <%= collection_select:身份证,Project.all,:ID,:姓名,:的onchange => remote_function(:URL => {:动作=>populate_projects'})%>
 

控制器操作:

 高清populate_projects
    @project = Project.find(PARAMS [:ID])
 结束
 

和现有的表:

 <表>
  &其中; TR>
    百分位>名称< /第i个
    百分位>分类和LT; /第i个
    百分位>预算与LT; /第i个
    百分位>截止日期:LT; /第i个
    百分位>公司ID< /第i个
    百分位>< /第i个
    百分位>< /第i个
    百分位>< /第i个
  < / TR>

<%@ projects.each办|专题| %>
  &其中; TR>
    &其中; TD>&其中;%= project.name%GT;&所述; / TD>
    &其中; TD>&其中;%= project.category%GT;&所述; / TD>
    < TD><%= number_to_currency(project.budget,:单位=>中和放大器;英镑;)%>< / TD>
    < TD><%= project.deadline.strftime(%A,%D%A%Y)%>< / TD>
    &其中; TD>&其中;%= project.company_id%GT;&所述; / TD>
    < TD><%=的link_to更多,项目%GT;< / TD>
    < TD><%=的link_to'编辑',edit_project_path(项目)%GT;< / TD>
    < TD><%=的link_to删除,项目,确认:你确定吗?,方法:删除%>< / TD>
  < / TR>
<%结束%GT;
< /表>
 
Excel 怎么让数据自动填充,不用下拉

解决方案

假设你正在使用jQuery

 <%= collection_select:项目:身份证,Project.all,:ID,:姓名,{},{:的onchange => '$获得(/ populate_projects)'}%GT;
 

然后在你的控制器

 高清populate_projects
  @project = Project.find(PARAMS [:ID])
  respond_to代码做|格式|
    ...
    format.js {渲染populate_projects',:格式=> [:JS]}
    ...
  结束
结束
 

最后,创建 populate_projects.js.erb ,写的任何变化表的内容脚本。 @project 是该脚本可用。

I'm trying to get a selection from a dropdown to populate a table in the same page, using AJAX so that changes appear without a page refresh. I've got as far as working out the code for the remote call, and adding the custom action to the controller, but I don't know how to get the returned information into the table I've got. At the moment, the table is just looping through all the projects, but I want it to only display the info for the one project that is selected.

What would I need to write in a JS (rjs?) file to get it to process the returned information, and what changes would I need to make to the existing table to make sure that it is displaying the correct info?

Thanks!

Dropdown (with AJAX call):

<%= collection_select :id, Project.all, :id, :name, :onchange => remote_function(:url=>{:action => 'populate_projects'}) %>

Controller action:

 def populate_projects
    @project = Project.find(params[:id])
 end

And the existing table:

<table>
  <tr>
    <th>Name</th>
    <th>Category</th>
    <th>Budget</th>
    <th>Deadline</th>
    <th>Company ID</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @projects.each do |project| %>
  <tr>
    <td><%= project.name %></td>
    <td><%= project.category %></td>
    <td><%= number_to_currency(project.budget, :unit => "&pound;") %></td>
    <td><%= project.deadline.strftime("%A, %d %B %Y") %></td>
    <td><%= project.company_id %></td>
    <td><%= link_to 'More', project %></td>
    <td><%= link_to 'Edit', edit_project_path(project) %></td>
    <td><%= link_to 'Delete', project, confirm: 'Are you sure?', method: :delete %></td>
  </tr>
<% end %>
</table>

解决方案

Assume you're using jquery

<%= collection_select :project, :id, Project.all, :id, :name, {}, {:onchange => '$.get("/populate_projects")'} %>

then in your controller

def populate_projects
  @project = Project.find(params[:id])
  respond_to do |format|
    ...
    format.js { render 'populate_projects', :formats => [:js] }
    ...
  end
end

finally, create populate_projects.js.erb and write any change table content script. @project is available in that script.