【问题标题】:jquery dataTable reorder plugin drag is working but drop is not workingjquery dataTable 重新排序插件拖动工作但 drop 不起作用
【发布时间】:2016-03-24 06:34:11
【问题描述】:

我正在使用 jquery.dataTables.rowReordering.js 插件对我的数据进行重新排序 Drag 正在工作,但 drop 不起作用,它会在鼠标离开时移动到相同的位置。下面是代码。

   <table class="table-bordered table dataTable" id="hotbuystable">
            <thead>
            <tr>
                <th>Image</th>
                <th>Brand Name</th>
                <th>Product Name</th>
                <th>Priority</th>
                <th>Remove</th>
            </tr>
            </thead>
            <tbody>

            <?php $count=0; foreach($getHotBuys as $keys => $item) {
                $count=$count+1;
                $cid = $item['phone_id'];
                echo "<tr data-position='$count' id='$cid'>";
                echo "<td><img src='" . $item['image'] . "' width='50px' height='50px'></td>";
                echo "<td>" . $item['brand_name'] . "</td>";
                echo "<td>" . $item['product_name'] . "</td>";
                echo "<td>" . $item['score'] . "</td>";
                echo "<td id='$cid'><a href='#' class='rowtag' id='$cid'><i class=\"fa fa-times fa-1x\"></i></a></td>";
                echo "</tr>";
            }
            ?>
            </tbody>
        </table>
    </div>
</div>
</div>
</body>
<script type="text/javascript" src="assets/js/plugins/jquery.min.new.js"> </script>
<script type="text/javascript" src="assets/js/lib/jquery-ui.js"></script>
<script type="text/javascript" src="assets/js/plugins/jquery-select2/select2.min.js"></script>
<script type="text/javascript" src="assets/js/plugins/jquery-datatables/jquery.dataTables.js"></script>
<script src="assets/js/lib/jquery.dataTables.rowReordering.js"></script>

jQuery(function ($) {

    'use strict';

     $('#hotbuystable').dataTable({
         "bPaginate": false,
         "bFilter": false,
         "bInfo": false,
         "bSort": false
     }).rowReordering();
});

请您注意是什么错误。

【问题讨论】:

  • 您能否提供一个带有最终 HTML 渲染(而不是 php 代码)的 JSFiddle 演示?

标签: php jquery datatables


【解决方案1】:

rowReorder 要求您有一个索引列并为所有&lt;tr&gt; 提供一个唯一的id。你已经在行上有id,所以:

<tr>
  <th>#</th>
  <th>Image</th>
  <th>Brand Name</th>
  <th>Product Name</th>
  <th>Priority</th>
  <th>Remove</th>
</tr>

...

<?php $count=0; foreach($getHotBuys as $keys => $item) {
  $count=$count+1;
  $cid = $item['phone_id'];
  echo "<tr data-position='$count' id='$cid'>";
  echo "<td>" . $count . "</td>"; 
  echo "<td><img src='" . $item['image'] . "' width='50px' height='50px'></td>";
  echo "<td>" . $item['brand_name'] . "</td>";
  echo "<td>" . $item['product_name'] . "</td>";
  echo "<td>" . $item['score'] . "</td>";
  echo "<td id='$cid'><a href='#' class='rowtag' id='$cid'><i class=\"fa fa-times fa-1x\"></i></a></td>";
  echo "</tr>";
}
?>

索引列不必是可见的,你可以像这样隐藏它:

var table = $('#example').dataTable({
  rowReorder: true,
  columnDefs : [ { targets : [0], visible: false } ]
});

如果您有一个基于 HTML 标记的表(不是数据源),那么您可以在初始化 dataTable 之前以编程方式创建索引列:

$('<th>').text('#').prependTo($('#example thead tr'))
$("#example tbody tr").each(function(i, tr) {
  $(tr).attr('id', 'id'+i)
  $('<td>').text(i).prependTo(tr)
})

上面使用# 索引列填充表,并为每个&lt;tr&gt; 分配一个唯一的id。

【讨论】:

    猜你喜欢
    • 2017-11-06
    • 2012-01-02
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 2017-07-13
    相关资源
    最近更新 更多