datatables 获取隐藏列的值

datatables 通过 "visible": false, 属性控制列的隐藏

datatables 如何获取隐藏列的 data 属性值,看如下代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="js/jquery-3.2.1.min.js"></script>
        <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <link href="http://cdn.datatables.net/plug-ins/28e7751dbec/integration/bootstrap/3/dataTables.bootstrap.css" rel="stylesheet" type="text/css"  />
        <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js" type="text/javascript"></script>
        <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
        <script src="http://cdn.datatables.net/plug-ins/28e7751dbec/integration/bootstrap/3/dataTables.bootstrap.js" type="text/javascript"></script>
    </head>
    <body>
        <table id="table_id" class="table table-bordered table-hover dataTable">
            <thead>
                <tr>
                    <th class="text-center">隐藏id</th>
                    <th class="text-center">编号</th>
                    <th class="text-center">作者</th>
                    <th class="text-center">操作</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
        <script>
            $(document).ready( function () {
                $('#table_id').DataTable({
                    language : {
                        "lengthMenu" : '每页显示<select class="form-control input-xsmall">'
                                        + '<option value="10">10</option>'
                                        + '<option value="20">20</option>'
                                        + '<option value="30">30</option>'
                                        + '<option value="40">40</option>'
                                        + '<option value="50">50</option>' + '</select>条',
                        "paginate" : {
                                        "first" : "首页",
                                        "last" : "尾页",
                                        "previous" : "上一页",
                                        "next" : "下一页"
                                    },
                        "processing" : "加载中...",  //DataTables载入数据时,是否显示‘进度’提示  
                        "emptyTable" : "暂无数据",
                        "info" : "共 _PAGES_ 页  _TOTAL_ 条数据  ",
                        "infoEmpty" : "暂无数据",
                        "emptyTable" : "暂无要处理的数据...",  //表格中无数据
                        "search": "搜索:",
                        "infoFiltered" : " —— 从  _MAX_ 条数据中筛选",
                        "zeroRecords":    "没有找到记录"
                                },
                    serverSide : true,//是否开启服务器模式
                    ajax : {
                        url: 'http://localhost:8089/d/getList',
                        type: 'post',
                        data : function(d){
                            var book = {};	
                            book.pageSize = d.length;
                            book.draw = d.draw;
                            book.offset = d.start;
                            return book;
                        }
                            },	
                    columns: [
                        {
                            "data": "id",
                            "name" : "id",
                            "visible": false,
                            "sDefaultContent":"",  //默认空字符串
                            "sClass": "text-center"
                        },
                        {
                            "orderable" : false,
                            "data": "author",		        	
                            'sClass': "text-center"		         		        	
                        },		        
                        {
                            "orderable" : false,
                            "data": "name",	        	
                            'sClass': "text-center"	 	         	       	
                        },
                        {
                            "orderable" : false,
                            "targets" : 3,//操作按钮目标列
                            "data" : null,
                            "sWidth" :"350px",
                            'sClass': "text-center",
                            "render" : function(data, type,row,meta) {
                                        var html = "";
                                        html += "<button id='testButton' style='margin-right:10px;'  "+
                                        "class='delete btn btn-success'  ><i class='fa  icon-key'></i> 获取隐藏id</button>"
                                        return html;
                            }
                        }
                            ]
                    });
            });
			
            //获取隐藏列的值的方法
            $("#table_id tbody").on("click", "#testButton", function () {
                //获取行
                var row = $("table#table_id tr").index($(this).closest("tr"));
                var id = $('#table_id').DataTable().row(row-1).data().id;
                alert(id)
            });
        </script>  
    </body>
</html>

 

数据库数据如下

 

运行效果

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悟世君子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值