改变数据表中任意单元格的颜色
我有一个数据表。这是我得到的结果的简化版本:
$('#dt_list').html( '<table cellpadding="0" cellspacing="0" border="1" class="content" id="test"></table>' );
$('#test').dataTable( {
"bAutoWidth": true
"aaData": [ 'val1', 'val2', 'val3' ],
"aoColumns": [ 'col1', 'col2', 'col3' ],
});
...stuff here..
<div id='dt_list'></div>
JavaScript 数组是由 Python 脚本根据从 MySQL 数据库中提取的值生成的(否则是无关紧要的,但我只是想确保阅读本文的人知道我' m 在 JavaScript 之外动态生成内容数组)。
正如我所说,该表上显示的数据是 MySQL 查询的结果,然后由 Python 处理并输出到 Python 生成的页面中的 JavaScript 中。
我的 Python 代码中的逻辑不仅可以确定单元格中包含哪些值,还可以确定单元格是否需要突出显示(通过单元格的背景颜色)。我的问题是,如何任意更改某些 DataTable 单元格的颜色?
由于我的大部分逻辑都在 Python 中,因此我实际上不需要 JavaScript 中任何非常奇特或优雅的评估,我只需要 JavaScript 中的某种方式来操作单元格的 CSS 属性或以其他方式指示不同的颜色。
我研究过 fnRender,这似乎是我最大的希望。然而,我目前的两个问题是(a)我无法找到一种方法来从 fnRender 函数内操纵颜色(它似乎更有针对性地操纵内容本身)和(b)我不确定如何我可以让 fnRender 了解 Python 操作的结果,以确定是否需要特殊着色。
我有什么遗漏的吗?或者也许我只需要重新思考我该如何处理这个问题?理想情况下,我想要一个解决方案,不涉及重新设计我这样做的方式,但如果必须的话,我会改变生成数据或传递数据的方式。我正在修改一些现有代码,并且不想弄乱现有设计,而是采用最短、最简单的路径将其表列表输出转换为 DataTables,其中一部分是生成某些具有修改颜色的单元格。
I have a DataTables table. Here's a simplified version of what I have as a result:
$('#dt_list').html( '<table cellpadding="0" cellspacing="0" border="1" class="content" id="test"></table>' );
$('#test').dataTable( {
"bAutoWidth": true
"aaData": [ 'val1', 'val2', 'val3' ],
"aoColumns": [ 'col1', 'col2', 'col3' ],
});
...stuff here..
<div id='dt_list'></div>
The JavaScript arrays are generated by a Python script based on values extracted from a MySQL database (it would be otherwise irrelevant, but I just want to make sure people reading this know that I'm dynamically generating the content arrays outside of JavaScript).
As I've said, the data being displayed on this table is the result of a MySQL query that is then processed by Python and output into the JavaScript in the page generated by Python.
I have logic in my Python code to determine not only what value goes into a cell, but also whether the cell requires any highlighting (via the cell's background color). My question here is, how can I arbitrarily change the color on certain DataTable cells?
Since most of my logic is in Python, I don't really need anything very fancy or elegant evaluations in the JavaScript, I just need some sort of way in JavaScript to manipulate a cell's CSS property or in some other way indicate a different coloring.
I've looked into fnRender and it seems my best hope. However, my two issues at this point are (a) I can't find a way to manipulate the color from within an fnRender function (it seems to be more targeted to manipulating the content itself) and (b) I'm unsure how I'd be able to have fnRender know the result of the Python operation to determine whether special coloring is needed.
Is there something I'm missing? Or maybe I just need to re-think how I am approaching this? Ideally I'd like a solution to this that doesn't involve reworking the way I'm doing this, but I'll change the way I generate the data or pass it if I must. I'm modifying some existing code and would rather not mess with the existing design, but take the shortest, easiest path to converting its table listing output to DataTables, and part of that is generating certain cells as having a modified color.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的“fnRender”回调可以返回用标记
或
(或其他)包裹的单元格内容。然后,您可以使用
fnDrawCallback
函数查找这些元素并使用类标记父元素。
Your "fnRender" callback can return cell contents wrapped in a marker
<span>
or<div>
(or whatever). You can then use thefnDrawCallback
function to find those and tag the parent<td>
element with a class.