【问题标题】:jQuery sortable table not posting correctly on railsjQuery 可排序表未在 Rails 上正确发布
【发布时间】:2014-06-17 21:09:36
【问题描述】:

我使用 jQuery 制作了一个可排序的表格,请参阅:https://www.youtube.com/watch?v=bRbl2ey9gt4

我需要它以新顺序发布项目的位置。

目前其发布的是空值,见下文:

Started POST "/episodes/sort" for 127.0.0.1 at 2014-06-17 16:30:44 -0400
Processing by EpisodesController#sort as */*
  Parameters: {"episodes"=>["", "", "", "", "", "", "", ""]}

我知道我必须使用 content_tag,但我不理解它背后的逻辑,因为我使用的是 html 表。

这是我的看法:

管理剧集

<tbody id='eps'>
<tr>
  <th>Title</th>
  <th>#</th>
  <th>Season</th>
  <th>Download?</th>
  <th>Shared?</th>
  <th>Length</th>
  <th>Status</th>
        <th></th>
</tr>
<% for episode in @episodes %>
  <tr>
    <td width="20%"><%=h episode.title %></td>
    <td><%=h episode.number %></td>
    <td><%=h episode.season %></td>
    <td><%=h episode.is_downloadable %></td>
    <td><%=h episode.shared_with_dev %></td>
    <td><%=h episode.length %></td>
    <td>

排除不必要的东西。

这是我的 JS:

$(window).load(function() {
    $("#eps").sortable({

      // Maintains width when being sorted
      helper: function(e, tr){
        var $originals = tr.children();
        var $helper = tr.clone();
        $helper.children().each(function(index){
          $(this).width($originals.eq(index).width()) 
        });
        return $helper;
      },

      axis: 'y',
      placeholder: 'ui-state-highlight',
      cursor: 'move',
      dropOnempty: false,
      scroll: true,
      opacity: 0.4,
      tolerance: 'pointer',
      revert: 'true',

      update: function(event, ui){
        var itm_arr = $("#eps").sortable('toArray');
        var pobj = {episodes: itm_arr};
        $.post("/episodes/sort", pobj);
      }

    });
});

谢谢

【问题讨论】:

    标签: javascript jquery ruby-on-rails jquery-ui


    【解决方案1】:

    你需要trs 有一个属性供jQuery UI sortable 的toArray 获取。 The documentation indicates id is used by default.

    试试

    <tr id="<%= episode.id %>">
    

    【讨论】:

      猜你喜欢
      • 2018-01-26
      • 1970-01-01
      • 2012-04-30
      • 2013-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      相关资源
      最近更新 更多