繁体   English   中英

Rails / jquery将在jQuery中收集的变量(ID)传递给rails控制器/动作

[英]Rails/jquery passing a variable(IDs) collected in jQuery to a rails controller/action

我是AJAX和jQuery的新手,所以请原谅我,如果这是基本的:

我用2dconcepts( http://www.2dconcept.com/jquery-grid-rails-plugin )实现了很棒的jgrid。 现在我想用复选框选择表格中的数据, - 获取产品ID(工作正常我看到来自'handleSelection'的警报)并将其传递给我在rails控制器中的自定义操作以编辑指定的记录(非常类似于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 }
)%>

我想我需要将post-request放在函数中并用这样的方式调用它:

<%= button_to_function('EDIT CHECKED', 'handleSelection(id)', {
:id => "products_select_button"}) %>

但说实话,即使该按钮也不起作用,因为它将字符串“products_select_button”传递给函数,而不是收集的值...

非常感谢您的帮助!

瓦尔

“products_select_button”})%>

要将参数发送到控制器,您始终可以使用jQuery的post函数,在任何设置中都可以轻松实现

$.post("products/", { id: 1, name: "John" } );

这段代码使用POST将id和name参数发送到您的产品控制器。 根据控制器的外观,不同的操作将接收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
  });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM