【问题标题】:datatables format values with text align数据表格式值与文本对齐
【发布时间】:2021-10-16 10:26:54
【问题描述】:

我正在使用 jQuery 数据表并通过 ajax 将数据填充为 json: https://datatables.net/examples/ajax/simple.html

var tbl = $('.mytbl').DataTable({
   "ajax": {
      url: "ajax/getData.php",
      type: "POST"
   }
});

getData.php 的响应会是这样的:

$myArray['data'][] = array(
   "Value 1",
   "Value 2",
   "Value 3"
} 
echo json_encode($myArray);

这很好用: 但是我该如何定义——例如——值 2 在我的表格中应该是文本对齐?

【问题讨论】:

  • 你能分享你的html代码吗?

标签: php jquery datatables


【解决方案1】:

试试这个

   var tbl = $('.mytbl').DataTable({
       "ajax": {
          url: "ajax/getData.php",
          type: "POST"
       },
       'columnDefs': [{
            "targets": 1,
            "className": "text-right",
       }]
    });

【讨论】:

    【解决方案2】:

    您可以使用Datatables 提供的渲染方法。

    {
        data: 'value 2 column',
        render: function ( data, type, row ) {
            return `<span style="text-align:right">${data}</span>`;
        }
    }
    

    如果所有列值都右对齐,您也可以使用 css。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-07
      • 2011-04-08
      • 2019-04-24
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-21
      相关资源
      最近更新 更多