【问题标题】:DataTables get class of specific columnDataTables 获取特定列的类
【发布时间】:2019-03-16 07:48:22
【问题描述】:

我想恢复特定列的类

确实我希望计算特定列的唯一类的数量

在 DataTables 文档中,我找到了如何从列中检索数据:

  var table = $('#myTable').DataTable({
    "aaSorting": []
  });

  var nbr = table.column( 2 ).data().unique().count();
  console.log (nbr)

但我没有找到如何检索特定列的类

编辑:

<table id="myTable">
  <thead>
    <tr>
      <th>Column 1</th>
      <th>Column 2</th>
      <th>Column 3</th>
      <th>Column 4</th>
      <th>Column 5</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>
      <td class="867">Msg 1</td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td class="867">Msg 2</td>
      <td></td>
      <td></td>
      <td></td>
    </tr> 
    <tr>
      <td></td>
      <td class="1087">Msg 3</td>
      <td></td>
      <td></td>
      <td></td>
    </tr> 
    <tr>
      <td></td>
      <td class="867">Msg 4</td>
      <td></td>
      <td></td>
      <td></td>
    </tr>  
  </tbody>
</table>

在这个例子中,我应该有数字 2(对于两个不同的类) -- 类是通过foreach(PHP)添加的

【问题讨论】:

  • “列的类别”是什么意思?表中的列是单元格的集合,可能有多个类。
  • @RoryMcCrossan 我编辑了我的帖子

标签: jquery datatables


【解决方案1】:

既然你已经有了数据列,你可以使用attr来获取类的属性值

var getTable = $('#myTable tbody tr');

getTable.each(function(){
  console.log(this).find('td:nth-child(2)').attr('class');
});
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<table id="myTable">
  <thead>
    <tr>
      <th>Column 1</th>
      <th>Column 2</th>
      <th>Column 3</th>
      <th>Column 4</th>
      <th>Column 5</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>
      <td class="867">Msg 1</td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td class="867">Msg 2</td>
      <td></td>
      <td></td>
      <td></td>
    </tr> 
    <tr>
      <td></td>
      <td class="1087">Msg 3</td>
      <td></td>
      <td></td>
      <td></td>
    </tr> 
    <tr>
      <td></td>
      <td class="867">Msg 4</td>
      <td></td>
      <td></td>
      <td></td>
    </tr>  
  </tbody>
</table>

它应该给你属性类的值

【讨论】:

  • 谢谢,但它不起作用,我有这个错误:Uncaught TypeError: table.column(...).attr is not a function
【解决方案2】:

您可以使用$("table tr td:nth-child(2)") 获取特定列中的所有单元格。

然后使用.mapreturn this.className 提取类名

随后对数组执行“唯一”/“不同”操作以限制它们:请参阅 https://stackoverflow.com/a/14438954/2181514

function onlyUnique(value, index, self) { 
    return self.indexOf(value) === index;
}

var clsnames = $("table tr td:nth-child(2)")
                   .map(function() { 
                       return this.className
                   })
                   .toArray()
                   .filter(onlyUnique);

console.log(clsnames.length)
$("#result>span").text(clsnames.length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="myTable">
  <thead>
    <tr>
      <th>Column 1</th>
      <th>Column 2</th>
      <th>Column 3</th>
      <th>Column 4</th>
      <th>Column 5</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>
      <td class="867">Msg 1</td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td class="867">Msg 2</td>
      <td></td>
      <td></td>
      <td></td>
    </tr> 
    <tr>
      <td></td>
      <td class="1087">Msg 3</td>
      <td></td>
      <td></td>
      <td></td>
    </tr> 
    <tr>
      <td></td>
      <td class="867">Msg 4</td>
      <td></td>
      <td></td>
      <td></td>
    </tr>  
  </tbody>
</table>
<div id='result'>Result: <span></span></div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    相关资源
    最近更新 更多