简体   繁体   中英

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

I am using the DataTable in javascript and jquery in order to setup a interactive table. I want to change the row color based on cell value.

I tried to use the fnRowCallback function and i tried to use rowCallback function.

in both functions are not working and the page is not displaying the table.

if i remove these functions the table is displayed and all data are available.

 $(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;
                        }
                    }


             ]
           }
        });

or the second function.

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

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

            }
           }

i expect to display the data in the destTable and where the opinion has a value equal to DEFAULT VALUE to make the row color red else to keep it white.

You can this in another way

{% for q in queryset %}

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

fnRowCallback feels like the right way to do this, but I notice you're missing a closing curly brace and a comma - which will cause your code to break and not render the table.

"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: {
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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