DataTables对齐文本列

发布于 2025-01-17 17:09:33 字数 621 浏览 1 评论 0原文

我在应用程序中使用的数据表,它效果很好。要中心文本,我正在使用:

{ className: 'dt-center', targets: '_all' },

但是,对于最后一列,我想对准剩下的文本而不是中心,但是以下内容没有效果:

{ targets: [ 12 ], className: 'dt-left' }

这是我的完整代码:

$('#audits-datatable').DataTable({
    columnDefs: [
        { width: '70px', targets: [ 0, 1, 11 ] },
        { width: '125px', targets: [ 8 ] },
        { width: '600px', targets: [ 12 ] },
        { className: 'dt-center', targets: '_all' },
        { targets: [ 12 ], className: 'dt-left' }
    ],
});

为什么“ dt-left”不是第12列中的“ DT中心”覆盖?它仍然显示为中心而不是向左。

I'm using DataTables in my app and it works great. To center the text, I'm using:

{ className: 'dt-center', targets: '_all' },

However, for the last column, I want to align the text left instead of center, but the following is having no effect:

{ targets: [ 12 ], className: 'dt-left' }

Here is my complete code:

$('#audits-datatable').DataTable({
    columnDefs: [
        { width: '70px', targets: [ 0, 1, 11 ] },
        { width: '125px', targets: [ 8 ] },
        { width: '600px', targets: [ 12 ] },
        { className: 'dt-center', targets: '_all' },
        { targets: [ 12 ], className: 'dt-left' }
    ],
});

Why is "dt-left" not overriding "dt-center" in column 12? It still shows up aligned centered instead of left.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狂之美人 2025-01-24 17:09:33

我能够找到解决方案。我将在这里发布它,希望能帮助某人。关键是逆转第6和7行;但是,它仅适用于桌子主体,而不是桌子标头。对于表标头,我需要添加className:“ dt-head-left”。

对我有用的最终代码:

$('#audits-datatable').DataTable({
        columnDefs: [
        { width: '70px', targets: [ 0, 1, 11 ] },
        { width: '125px', targets: [ 8 ] },
        { width: '600px', targets: [ 12 ] },
        { targets: [ 12 ], width: '650px', className: 'dt-left', className: 'dt-head-left' },
        { targets: '_all', className: 'dt-center' }
    ],
});

I was able to find a solution. I'll post it here in hopes of helping someone. The key was reversing lines 6 and 7; however, it only worked for the table body, not the table header. For the table header, I needed to add className: 'dt-head-left'.

Final code that worked for me:

$('#audits-datatable').DataTable({
        columnDefs: [
        { width: '70px', targets: [ 0, 1, 11 ] },
        { width: '125px', targets: [ 8 ] },
        { width: '600px', targets: [ 12 ] },
        { targets: [ 12 ], width: '650px', className: 'dt-left', className: 'dt-head-left' },
        { targets: '_all', className: 'dt-center' }
    ],
});
随遇而安 2025-01-24 17:09:33

我也有同样的问题。但这是我在调查中发现的。根据我的发现,解决方案很简单,但了解问题背后的原因很重要。

我不知道你在第12列中使用的是什么类型的值。
我假设它是日期数字值。
假设我们的列包含一个日期值。

如果是这样,当我们最初加载表时,库会向单元格添加一个附加的类 dt-type-date ,

<td class="dt-type-date">2024-03-06 15:42:33</td>

现在,当我们像这样向数据表对象分配附加配置时,

columnDefs: [
  {
     targets: '_all',
     className: 'dt-left'
  }
],

库会添加单元格的 .dt-left

<td class="dt-type-date dt-left">2024-03-06 15:42:33</td>

但是 dt-type-date 类会覆盖 .dt-left中的 css。 dt-右

如果我们查看这些类,它们都具有相同的 css 属性,即 text-align,如果我们查看 css 文件 datatables.min.css 文件,< code>.dt-type-date 类在之后定义
.dt-left.dt-right。这就是 .dt-left 配置不起作用的原因。

解决方案:

我必须明确地将.dt-head-left.dt-body-left告知特定的数据表配置对象。它有效。

就你而言,就像我猜的那样,

{ targets: [ 12 ], className: 'dt-head-left dt-body-left' }

我希望这能解决问题。

I also had the same problem. But this is what I found in my investigation. From my finding, the solution is simple but the reason behind the problem is important to know.

I don't know in your 12th column, what kind of value you are using.
I'm assuming, it would either date or numeric value.
Let's assume, our column holds a date value.

If so, when we initially load the table, the library adds an additional class dt-type-date to the cell,

<td class="dt-type-date">2024-03-06 15:42:33</td>

Now, when we assign additional configuration to our datatable object like this,

columnDefs: [
  {
     targets: '_all',
     className: 'dt-left'
  }
],

the library adds the .dt-left class to the cell

<td class="dt-type-date dt-left">2024-03-06 15:42:33</td>

But the dt-type-date class overrides the css from .dt-left or .dt-right.

If we look at these class, they all have the same css property which is text-align and if we look at the css file datatables.min.css file, the .dt-type-date class is defined after the
.dt-left and .dt-right. This is why the .dt-left configuration is not working.

Solution:

I had to explicitly tell, .dt-head-left and .dt-body-left to the specific datatable configuration object. And it works.

In your case, it'd be like I guess,

{ targets: [ 12 ], className: 'dt-head-left dt-body-left' }

I hope that solves the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文