我是 AJAX 和 jQuery 的新手,所以如果这是基本的,请原谅我:
我通过 2dconcepts (http://www.2dconcept.com/jquery-grid-rails-plugin) 实现了很棒的 jgrid。现在我想使用复选框选择表中的数据, - 获取产品 ID(效果很好,我看到来自“handleSelection”的警报)并将其传递给 Rails Controller 中的自定义操作以编辑指定的记录(与 Ryan B 的railscast #165 非常相似)。我根本不知道该怎么做。
<script type="text/javascript">
function handleSelection(id) {
alert('Open those up?:'+id);
}
</script>
<% title "JGRID Table" %>
<%= jqgrid("Products", "products", "/products",
[
{ :field => "id", :label => "ID", :width => 40, :resizable => false },
{ :field => "vendorname", :width => 200, :label => "vendorname", :editable => true },
{ :field => "productname", :width => 230, :label => "productname", :editable => true },
{ :field => "metakeyword", :width => 250, :label => "metakeyword", :editable => true },
{ :field => "status", :width => 100, :label => "status", :editable => true, :edittype => "select",
:editoptions => { :value => [["inbox","inbox"], ["todo", "todo"], ["final","final"]] } },
{ :field => "category_id", :label => "category_id", :width => 100, :resizable => false, :editable => true }
],
{ :add => true,
:edit => true,
:inline_edit => true,
:delete => true,
:edit_url => "/products/post_data",
:rows_per_page => 30,
:height => 270,
:selection_handler => "handleSelection",
:multi_selection => true }
)%>
我想我需要将后请求放入函数中并使用如下方式调用它:
<%= button_to_function('EDIT CHECKED', 'handleSelection(id)', {
:id => "products_select_button"}) %>
但说实话,即使该按钮也不起作用,因为它将字符串“products_select_button”传递给函数,而不是收集的值......
非常感谢您的帮助!
瓦尔
“产品选择按钮”}) %>最佳答案
要将参数发送到 Controller ,您始终可以使用 jQuery 的 post 函数,它在任何设置中都很容易实现
$.post("products/", { id: 1, name: "John" } );
该段代码通过 POST 将 id 和 name 参数发送到您的产品 Controller 。根据您的 Controller 的外观,不同的操作将收到 POST。你总是可以做
rake -T
在 Rails 应用程序中查看哪个操作处理 POST。 我真的不知道最终的网格是什么样子,但是这段 jQuery 代码可能会让您开始?
$(document).ready(function() {
$("#button").click(function() { // Submit button is pressed
var grid_id = $("#grid").val(); // Grab value of the grid
$.post("products/", { id : grid_id }); // Post the grid_id to your controller
});
});
关于jquery - Rails/jquery 将 jQuery 中收集的变量(ID)传递给 Rails Controller /操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2231131/