[英]Rails/jquery passing a variable(IDs) collected in jQuery to a rails controller/action
I am new to AJAX and jQuery so please excuse me if this is kind of basic: 我是AJAX和jQuery的新手,所以请原谅我,如果这是基本的:
I implemented the great jgrid by 2dconcepts ( http://www.2dconcept.com/jquery-grid-rails-plugin ). 我用2dconcepts( http://www.2dconcept.com/jquery-grid-rails-plugin )实现了很棒的jgrid。 Now I would like to select the data in my table with the checkbox, - get the product ids (that works fine I see the alert from the 'handleSelection') and pass it over to my custom action in the rails controller to edit the specified records (very similar to Ryan B's railscast #165). 现在我想用复选框选择表格中的数据, - 获取产品ID(工作正常我看到来自'handleSelection'的警报)并将其传递给我在rails控制器中的自定义操作以编辑指定的记录(非常类似于Ryan B的railscast#165)。 I simply have no idea how I would do that. 我根本不知道我会怎么做。
<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 }
)%>
I think I need to put the post-request in the function and call it with something like this: 我想我需要将post-request放在函数中并用这样的方式调用它:
<%= button_to_function('EDIT CHECKED', 'handleSelection(id)', {
:id => "products_select_button"}) %>
But to be honest, not even that button would work, as it passes the string "products_select_button" to the function, and not the collected value... 但说实话,即使该按钮也不起作用,因为它将字符串“products_select_button”传递给函数,而不是收集的值...
Your help is greatly appreciated! 非常感谢您的帮助!
Val 瓦尔
"products_select_button"}) %> “products_select_button”})%>To send parameters to your controller you can always use jQuery's post function, it's very easy to implement in any setup 要将参数发送到控制器,您始终可以使用jQuery的post函数,在任何设置中都可以轻松实现
$.post("products/", { id: 1, name: "John" } );
That piece of code sends the id and name parameter to your products controller with a POST. 这段代码使用POST将id和name参数发送到您的产品控制器。 Depending on what your controller looks like different actions will recieve the POST. 根据控制器的外观,不同的操作将接收POST。 You can always do 你总能这样做
rake -T
in you rails app in order to see which action that handles the POST. 在你的rails应用程序中,以查看处理POST的操作。 I don't really know what the final grid looks like but this piece of jQuery code might get you started? 我真的不知道最终的网格是什么样的,但这段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
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.