【问题标题】:Return multiple rows from table to Rails controller action将表中的多行返回到 Rails 控制器操作
【发布时间】:2014-12-24 16:14:32
【问题描述】:

Rails 4.0.1、Ruby 2.0.0、数据表 1.10、jquery-datatables-rails 3.1.1

我有一个表,它通过在所需行上指定一个 .active 类来选择多行。我想将活动行(实际上是活动行中的单个字段)返回给我的操作。这是基本问题。如何将类选择的行中的字段数组返回到 Rails 操作?我很茫然,希望能得到帮助。

明确地说,我可以在没有帮助的情况下编写路由和动作。我需要了解在视图中必须使用什么类型的按钮或表单来发送行。谢谢。

表格视图是:

表格是:

<table width="80%" cellspacing="0" class="display dt-responsive no-wrap table-striped dataTable no-footer dtr-inline" id="datatable" role="grid" aria-describedby="datatable_info">
    <thead>
    <tr role="row"><th class="all ui-state-default sorting_asc" tabindex="0" aria-controls="datatable" rowspan="1" colspan="1" aria-sort="ascending" aria-label="Associate: activate to sort column descending">Associate</th><th class="all ui-state-default sorting" tabindex="0" aria-controls="datatable" rowspan="1" colspan="1" aria-label="Logon: activate to sort column ascending">Logon</th><th class="min-tablet ui-state-default sorting" tabindex="0" aria-controls="datatable" rowspan="1" colspan="1" aria-label="Role: activate to sort column ascending">Role</th></tr>
    </thead>
    <tbody>
    <tr role="row" class="odd active">
          <td class="sorting_1"><a href="/admin/associates/25/edit">Aaron James</a></td>
          <td>D110</td>
          <td>Associate</td>
        </tr><tr role="row" class="even">
          <td class="sorting_1"><a href="/admin/associates/26/edit">Amy Clark</a></td>
          <td>D111</td>
          <td>Associate</td>
        </tr><tr role="row" class="odd active">
          <td class="sorting_1"><a href="/admin/associates/23/edit">Angela Jenkins</a></td>
          <td>D108</td>
          <td>Associate</td>
        </tr><tr role="row" class="even">
          <td class="sorting_1"><a href="/admin/associates/16/edit">Bonnie Carpenter</a></td>
          <td>D101</td>
          <td>Admin</td>
        </tr><tr role="row" class="odd active">
          <td class="sorting_1"><a href="/admin/associates/24/edit">Catherine Reid</a></td>
          <td>D109</td>
          <td>Associate</td>
        </tr><tr role="row" class="even active">
          <td class="sorting_1"><a href="/admin/associates/18/edit">Donald King</a></td>
          <td>D103</td>
          <td>Associate</td>
        </tr><tr role="row" class="odd">
          <td class="sorting_1"><a href="/admin/associates/27/edit">Evelyn Foster</a></td>
          <td>D112</td>
          <td>Associate</td>
        </tr><tr role="row" class="even">
          <td class="sorting_1"><a href="/admin/associates/22/edit">Gregory Torres</a></td>
          <td>D107</td>
          <td>Associate</td>
        </tr><tr role="row" class="odd">
          <td class="sorting_1"><a href="/admin/associates/29/edit">Heather Hall</a></td>
          <td>D114</td>
          <td>Associate</td>
        </tr><tr role="row" class="even active">
          <td class="sorting_1"><a href="/admin/associates/20/edit">Lillian Myers</a></td>
          <td>D105</td>
          <td>Associate</td>
        </tr></tbody>
  </table>

HTML.ERB 是:

<div class="span8">
  <%= link_to 'New Associate', new_admin_associate_path, class: 'btn btn-primary' %>
  <p>
  <table id="datatable" class="display dt-responsive no-wrap table-striped" cellspacing="0" width="80%">
    <thead>
    <tr>
      <th class="all">Associate</th>
      <th class="all">Logon</th>
      <th class="min-tablet">Role</th>
    </tr>
    </thead>
    <tbody>
    <% @associates.each do |associate| %>
        <tr>
          <td><%= link_to associate.name, edit_admin_associate_path(associate.id) %></td>
          <td><%= associate.logon %></td>
          <td><%= associate.roles.first.name.titleize unless associate.roles.first.nil? %></td>
        </tr>
    <% end %>
    </tbody>
  </table>
</div>

JavaScript 是:

$(document).ready(function () {
    var datatable = $('#datatable').DataTable({
        responsive: true,
        autoWidth: false,
        pagingType: 'full',
        jQueryUI: true
    });
    $('#datatable tbody').on( 'click', 'tr', function () {
        $(this).toggleClass('active');
    } );

【问题讨论】:

    标签: jquery html ruby-on-rails datatables jquery-datatables-rails


    【解决方案1】:

    不出所料,答案似乎在 jQuery 中。我还没有最终代码,但这是来自 DataTables.Net:

    fnGetSelectedData

    fnGetSelectedData 
    Get the data source objects/arrays from DataTables for the selected rows (same as fnGetSelected followed by fnGetData on each row from the table). 
    Default:  
    Input parameters: void 
    Return parameter: Array - Data from the TR nodes which are currently selected 
    Code example: 
    $(document).ready(function() {
        $('#example').DataTable( {
            dom: 'T<"clear">lfrtip',
            tableTools: {
                "sRowSelect": "multi",
                "aButtons": [ "select_all", "select_none" ]
            }
        } );
    } );
    

    【讨论】:

    • 这是一个可能的答案。我还没有完成代码,并且不会有几天。可能会有更好的答案。你有吗?
    猜你喜欢
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多