DataTables对齐文本列
我在应用程序中使用的数据表,它效果很好。要中心文本,我正在使用:
{ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能够找到解决方案。我将在这里发布它,希望能帮助某人。关键是逆转第6和7行;但是,它仅适用于桌子主体,而不是桌子标头。对于表标头,我需要添加className:“ dt-head-left”。
对我有用的最终代码:
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:
我也有同样的问题。但这是我在调查中发现的。根据我的发现,解决方案很简单,但了解问题背后的原因很重要。
我不知道你在第12列中使用的是什么类型的值。
我假设它是
日期
或数字
值。假设我们的列包含一个日期值。
如果是这样,当我们最初加载表时,库会向单元格添加一个附加的类 dt-type-date ,
现在,当我们像这样向数据表对象分配附加配置时,
库会添加单元格的
.dt-left
类但是
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
告知特定的数据表配置对象。它有效。就你而言,就像我猜的那样,
我希望这能解决问题。
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
ornumeric
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,Now, when we assign additional configuration to our datatable object like this,
the library adds the
.dt-left
class to the cellBut 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 filedatatables.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,
I hope that solves the problem.