使用ajax更新行状态 [英] Update row status using ajax
问题描述
我目前正在尝试使用Rails对我的第一个ajax接口进行编程.
I am currently trying to program my first ajax interface using Rails.
应用程序当前显示一个填充有列表项的表.用户必须批准或拒绝每个列表项.目前,我在每行的末尾都有一个编辑链接,其中显示了一个我可以批准列表项目的表格.
The application currently shows a table populated with list items. The user has to approve or reject each of the list items. I currently have an edit link at the end of each row that shows a form in which I can approve the list item.
我正在考虑使用复选框而不是编辑链接.当用户单击复选框时,我要使用状态,用户名和日期/时间更新数据库,而无需离开此页面.
I am thinking on using a checkbox instead of the edit link. When the user clicks the checkbox I want to update the database with the status, user name and date/time without leaving this page.
- 我应该遵循哪些步骤?
- 我可以使用复选框还是我仅限按钮吗?
- 我应该使用什么xxx_remote助手?
- 如何使用ajax调用的结果更新复选框状态?
推荐答案
我认为复选框不是您要查找的正确控件.您说您希望用户能够批准或拒绝项目,这意味着您有3个状态:未处理,已批准和已拒绝.复选框仅支持2种状态:关闭和打开
I don't think that a checkbox is the correct control for what you're looking for. You said you want user's to be able to approve or reject items which means that you have 3 states: unhandled, approved and rejected. A checkbox only supports 2 states: off and on
我将使用两个链接接受和拒绝,然后执行以下操作.
I would use two links accept and reject and then do it as follows.
您认为:
...
<tr id="item1">
<td>Accept or Reject</td>
<td>
link_to_remote 'accept', :action => :accept, :id => 1, :method => :post
link_to_remote 'reject', :action => :reject, :id => 1, :method => :post
</td>
</tr>
...
在您的控制器中
def accept
item = Item.find(params[:id])
item.accept
respond_to do |want|
want.js {
render :update do |page|
page << "$('item_#{item.id}').cells[0].innerHTML = 'Accepted'"
...include other updates you need to make to your row...
page.visual_effect :highlight, "item_#{item.id}"
end
}
end
end
... similar for reject method ...
这篇关于使用ajax更新行状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!