开发者

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:

I implemented the great jgrid by 2dconcepts (http://www.2dconcept.com/jquery-grid-rails-plugin). 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). 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:

<%= button_to_function('EDIT CHECKED', 'handleSelection(id)', {
:id => "products_select_button"}) %>
开发者_StackOverflow中文版

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...

Your help is greatly appreciated!

Val

"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

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

That piece of code sends the id and name parameter to your products controller with a POST. Depending on what your controller looks like different actions will recieve the POST. You can always do

rake -T

in you rails app in order to see which action that handles the POST. I don't really know what the final grid looks like but this piece of jQuery code might get you started?

$(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
  });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜