dataTables实现鼠标拖动编辑行上下移动
更新:HHH   时间:2023-1-7


实现方式:
在初始化dataTables时增加属性:
var param_index=0; //定义排序字段,在新增表的行时用到
$('#dt-table').DataTable({
.....
"rowReorder":{
dataSrc :"自己数据库中排序的索引" eq:param_index
},
...
})

参看官方例子:
https://datatables.net/extensions/rowreorder/examples/

    //增加行
    $('#addRow').on( 'click', function () {
         dataTable.row.add({"param_index":param_index,"input_type":"","param_name":"","field_id":"","param_type":"","param_desc":""}).draw();
    } );

            //自动编号来显示序号
             dataTable.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
                    param_index = i+1;
                    cell.innerHTML = i+1;
                } );
            //页面引入的js,官网可以下载到 (rowReorder.js)这是排序的主要js
                                    <script type="text/javascript" src="../../js/plugin/datatables/jquery.dataTables4.min.js"></script>

<script type="text/javascript" src="../../js/plugin/datatables/dataTables.bootstrap4.min.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/dataTables.rowReorder.js"></script>
//css,拖动时行的样式
<link rel="stylesheet" type="text/css" href="../../css/rowReorder.dataTables.css">

返回web开发教程...