繁体   English   中英

如何将一个类添加到json数据表中?

[英]How do i add an class to a json datatable?

我想在最后一个数据记录上找到一个按钮,叫做factuur。 我想在那里找到一个div或按钮,但是如何在这样的数据数组中做到这一点。 我当时想我应该在td中添加一个类,并用类似以下的内容替换此处的文本:

$('.className').each(function(index, item){
  $(item).html(yourButton html);
})

有人知道该怎么做或更好的方法吗? 我调查了一下,但无法弄清楚如何应用在我的json上。

这是我的脚本现在的样子:

  <table  id="oTable" border="1" class="table_tag">

  <tr><thead>
    <th>Ordernummer</th>
    <th>Besteldatum</th>
    <th>BTW</th>
    <th>Totaalbedrag</th>
    <th>Status</th>
    <th>Verzonden</th>
    <th>Factuur</th>
  </thead></tr>
 </table> 

脚本:

$(function(){
  // Basic Setup
  var $tableSel = $('#oTable');
  $tableSel.dataTable({
    'aaData': data,
    'aoColumns': [
      {'mData': 'EAN'},
      {'mData': 'Besteldatum'},
      {'mData': 'BTW'},
      {'mData': 'Totaalbedrag'},
      {'mData': 'Status'},
      {'mData': 'Verzonden'},
      {'mData': 'Factuur'}
    ],
    'sDom': ''
  });

  $('#filter').on('click', function(e){
    e.preventDefault();
    var startDate = $('#start').val(),
        endDate = $('#end').val();

    filterByDate(1, startDate, endDate);

    $tableSel.dataTable().fnDraw();
  });

  // Clear the filter. Unlike normal filters in Datatables,
  // custom filters need to be removed from the afnFiltering array.
  $('#clearFilter').on('click', function(e){
    e.preventDefault();
    $.fn.dataTableExt.afnFiltering.length = 0;
    $tableSel.dataTable().fnDraw();
  });

});

/* Our main filter function
 * We pass the column location, the start date, and the end date
 */
var filterByDate = function(column, startDate, endDate) {
  // Custom filter syntax requires pushing the new filter to the global filter array
        $.fn.dataTableExt.afnFiltering.push(
            function( oSettings, aData, iDataIndex ) {
                var rowDate = normalizeDate(aData[column]),
              start = normalizeDate(startDate),
              end = normalizeDate(endDate);

          // If our date from the row is between the start and end
          if (start <= rowDate && rowDate <= end) {
            return true;
          } else if (rowDate >= start && end === '' && start !== ''){
            return true;
          } else if (rowDate <= end && start === '' && end !== ''){
            return true;
          } else {
            return false;
          }
        }
        );
    };

// converts date strings to a Date object, then normalized into a YYYYMMMDD format (ex: 20131220). Makes comparing dates easier. ex: 20131220 > 20121220
var normalizeDate = function(dateString) {
  var date = new Date(dateString);
  var normalized = date.getFullYear() + '' + (("0" + (date.getMonth() + 1)).slice(-2)) + '' + ("0" + date.getDate()).slice(-2);
  return normalized;
}
var data = [
        {
            "EAN": "180199",
            "Besteldatum": "2003-09-22",
            "BTW": "€19,00",
            "Totaalbedrag": "€109,00",
            "Status": "Verzonden",
            "Verzonden": "2003-09-22",
            "Factuur": "here"

        },
        {
            "EAN": "180200",
            "Besteldatum": "2004-11-12",
            "BTW": "€19,00",
            "Totaalbedrag": "€109,00",
            "Status": "Verzonden",
            "Verzonden": "2003-09-22",
            "Factuur": "here"
        },

     ];

每个TR的最后一个孩子的选择器呢?

$('tr:last-child').each(()=>{
    $(this).append("<button>");
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM