繁体   English   中英

如何根据值更改jquery dataTable中的行颜色

[英]how to change the row color in jquery dataTable based on the value

我在javascript和jquery中使用DataTable来设置交互式表格。 我想基于单元格值更改行颜色。

我尝试使用fnRowCallback函数,并且尝试使用rowCallback函数。

这两个功能都无法正常工作,页面也没有显示表格。

如果我删除这些功能,则会显示该表,并且所有数据都可用。

 $(function(){

        var destsData=[
        ]
        var sections={}
        var theTable = $('#SearchT2chiraTable').DataTable({
            language: {
                search: 'ﺑﺤﺚ : ',
                lengthMenu:'ﻣﺸﺎﻫﺪﺓ _MENU_ ﺑﻴﺎﻧﺎﺕ',
                paginate: {
                    first:      "اﻻﻭﻝ",
                    previous:   "اﻟﺴﺎﺑﻖ",
                    next:       "اﻟﺘﺎﻟﻲ",
                    last:       "اﻻﺧﻴﺮ"
                }
            },
            select: 'single'
        })
        var destsTable = $('#DestsTable').DataTable({    
            "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull){
                if ( aData[2] == "DEFAULT VALUE" )
                {
                    $('td', nRow).css('background-color', 'red' );
                }
                else
                {
                    $('td', nRow).css('background-color', 'white');
                }

            language: {
                search: 'ﺑﺤﺚ : ',
                lengthMenu:'ﻣﺸﺎﻫﺪﺓ _MENU_ ﺑﻴﺎﻧﺎﺕ',
                paginate: {
                    first:      "اﻻﻭﻝ",
                    previous:   "اﻟﺴﺎﺑﻖ",
                    next:       "اﻟﺘﺎﻟﻲ",
                    last:       "اﻻﺧﻴﺮ"
                }
            },

            select: 'single',
            data: destsData,
            columns: [
                { "data": "destination_id","title":'اﻟﺮﻣﺰ' },
                { "data": "te2chira_id_id","title":'ﺭﻣﺰ اﻟﺘﺄﺷﻴﺮﺓ' },
                { "data": "opinion", "title": 'اﻻﻗﺘﺮاﺡ' },
                { "data": "destination_date","title":'اﻟﺘﺎﺭﻳﺦ' },

                { "data": "section","title":'اﻟﻘﻄﻌﺔ' ,

                        "render":function(val,type,row,meta){
                            console.log('the Value is ',val)
                            if (type == 'set'){
                                console.log('doing here ')
                                row.section = val
                                row.section_display=sections[row.section]
                                row.section_filter=sections[row.section]
                                return
                            }else if (type === 'display',val) {
                                console.log('display')
                                return sections[val];
                            }
                            else if (type === 'filter') {
                                console.log('filter',val)
                                return row.section_filter;
                            }
                            // 'sort', 'type' and undefined all just use the integer
                            return row.section;
                        }
                    }


             ]
           }
        });

或第二个功能。

"rowCallback": function( row, data, index ) {

                if ( data.opinion == "DEFAULT VALUE" )
                {
                    $('td', row).css('background-color', 'Red');
                }
                else
                {
                    $('td', row).css('background-color', 'white');
                }

            }
           }

我希望在destTable中显示数据,并且意见的值等于DEFAULT VALUE,以使行颜色变为红色,否则将行保持白色。

你可以用另一种方式

{% for q in queryset %}

   {% if q.id == 1 %}
     <tr style="background: #fff;>
   {% else %}
     <tr style="background: #000;>
   {% endif %}
        <td></td>
    </tr>
{% endfor %}

fnRowCallback感觉是执行此操作的正确方法,但是我注意到您缺少右花括号和逗号-这将导致您的代码中断而不呈现表。

"fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull){
    if ( aData[2] == "DEFAULT VALUE" )
    {
        $('td', nRow).css('background-color', 'red' );
    }
    else
    {
        $('td', nRow).css('background-color', 'white');
    }
},  // Make sure you add the closing brace and a comma
language: {
...

暂无
暂无

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

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