如何在TD标签中添加id?

科拉德

我正在使用 DataTables 插件(我是初学者),我想在<td>HTML 标签中添加一个“id” 我做了这篇文章中的内容,但这不是我想要的。

我也看到了这个帖子,但我不知道如何使用这段代码。

JS:

$('#datatable').dataTable({
    "sPaginationType" : "full_numbers",
    "createdRow" : function ( row, data_use, dataIndex ) {
        $(row).attr('id', dataIndex);
    },
    data : data_use,
    columns : column_name,
    dom : 'Bfrtip',
    select : 'single',
    responsive : true,
    altEditor : true,
    destroy : true,
    searching: true,
    buttons : [{
        extend : 'selected',
        text : 'Edit',
        name : 'edit'
    }],
});
乔希

您可以通过向columnDefsDataTable 配置中添加一个数组并添加一个createdCell 函数来实现此目的例如:

$(document).ready(function() {
  var table = $('#example').DataTable({
    'columnDefs': [{
      'targets': "_all",    // this will invoke the below function on all cells
      'createdCell': function(td, cellData, rowData, row, col) {
        // this will give each cell an ID
        $(td).attr('id', 'cell-' + cellData);
      }
    }]
  });

运行下面的代码片段以查看完整示例。您可以右键单击一个单元格(即 a td)并单击“检查”以查看id添加到每个单元格属性。

$(document).ready(function() {
  var table = $('#example').DataTable({
    'columnDefs': [{
      'targets': "_all",
      'createdCell': function(td, cellData, rowData, row, col) {
        $(td).attr('id', 'cell-' + cellData);
      }
    }]
  });

  // Add some data to the table
  table.row.add(['Airi Satou', 'Accountant', 'Tokyo', '5407', '2008/11/28', '$162,700']).draw();

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdn.datatables.net/r/dt/dt-1.10.9/datatables.min.js"></script>
<link href="https://cdn.datatables.net/r/dt/dt-1.10.9/datatables.min.css" rel="stylesheet" />
<table id="example" class="display" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Extn.</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Extn.</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </tfoot>
</table>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在TD中添加没有文本的类?

来自分类Dev

如何在PySide中添加标签

来自分类Dev

如何在VB 6中添加3个标签

来自分类Dev

如何在CakePHP HtmlHelper中添加span标签?

来自分类Dev

如何在GridView中的<td>标签中添加类

来自分类Dev

如何在div中添加标签

来自分类Dev

如何在html标签中添加类?

来自分类Dev

如何在Go中向图像添加简单的文本标签?

来自分类Dev

如何在jQuery中添加ID?

来自分类Dev

如何在果园替代品中向标签添加id属性

来自分类Dev

如何在Flutter中的TextFormField标签中添加红色星号

来自分类Dev

如何在图像标签中添加onclick

来自分类Dev

如何在BeautifulSoup中的tr标签之前添加多个td标签

来自分类Dev

如何在fortran中添加双标签?

来自分类Dev

如何在标签中添加滑动

来自分类Dev

如何在JTabbedPane的标签栏中添加不是标签的按钮?

来自分类Dev

如何在div中添加标签

来自分类Dev

如何在Jquery中添加ID?

来自分类Dev

如何在PDB中添加链ID

来自分类Dev

如何在php输出中添加id标签?

来自分类Dev

javascript如何在<td>中搜索字符串,然后将其添加到该ID?

来自分类Dev

如何在jQuery中添加ID?

来自分类Dev

table 标签会自动在 tr 标签中添加额外的 td

来自分类Dev

如何在javascript中为每个td元素自动添加递增的数字ID

来自分类Dev

在PHP中如何在td标签内使用简单的if语句

来自分类Dev

如何在 html 文件的 td 标签中应用 if 和 OR if 或条件

来自分类Dev

如何在标签中添加图标?

来自分类Dev

如何在 JavaScript 中获取标签的 ID

来自分类Dev

如何在 ios 中的 UIScrollView 中添加动态标签?