zoukankan      html  css  js  c++  java
  • DataTables获取指定元素的行数据

    法1:

    用jquey获取,var row = $('.edit').parent().parent();

    缺点:只能获取dom上的东西,不能获取没有渲染的数据

    法2:

    首先绑定行号到元素上

    $('#example').dataTable( {
    
      "columns": [
          {"data":"name", "orderable": false, "searchable": false,"render" : function ( data, type, row, meta) {
            return  '<button id="btnEdit" data-rowindex="'+meta.row+'">编辑</button>';
          }}
       ] } );

    然后根据元素取出行号

    var rowIndex = $('#btnEdit').attr('data-rowindex');

    最后获取数据

    $('#example').DataTable().rows(rowIndex).data()[0];

     如果是单击选择行(多选),示例如下:
    $(document).ready(function() {
        var table = $('#example').DataTable();
     
        $('#example tbody').on( 'click', 'tr', function () {
            $(this).toggleClass('selected');
        } );
     
        $('#button').click( function () {
            alert( table.rows('.selected').data().length +' row(s) selected' );
        } );
    } );

    如果是单击单元格获取数据,示例如下:

    //单击首列,获取该列中单元格数据
    $('#example tr td:first-child').click(function(){ alert($(this).text()) });
  • 相关阅读:
    OC-KVO简介
    注册审核
    应用权限
    关于函数执行的一点知识
    设置权限
    文件操作实例:文件管理器(网页版)
    文件操作
    正则表达式
    全局变量和递归
    案例:简单留言板
  • 原文地址:https://www.cnblogs.com/hdwang/p/7126796.html
Copyright © 2011-2022 走看看