duanbi5906 2019-04-05 07:04
浏览 623
已采纳

datatable createdRow为所有行应用背景颜色,尽管条件设置在要应用的行上

Im trying to set background colors for datatable rows depending on the data returned from the server.

I fetch the data dynamically through ajax and it displays successfully on the table, but the color applies to all rows, even when the other if conditions are met.

Heres my code

"createdRow": function(row, data, index) {
    console.log(data[0]);
    if ( data[0] = '1403') {
        $(row).css('background-color', 'blue');
    } else if (data[0] = '1400'){
        $(row).css('background-color', 'yellow');
    } else {
        $(row).css('background-color', 'white');
    }
},

This instead, makes all rows blue. Even when only 1 row contains 1403

  • 写回答

1条回答 默认 最新

  • dongshan9619 2019-04-05 07:20
    关注

    If your function works well, there are still some logical errors / best writing practices.

    Here is the new code that I would propose:

    "createdRow" : function(row, data, index) {
        console.log(data[0]);
    
        if (data[0] === '1403') {
            $(row).css('background-color', 'blue');
        } elseif (data[0] === '1400') {
            $(row).css('background-color', 'yellow');
        } else {
            $(row).css('background-color', 'white');
        }
    },
    

    Explanation:

    Your previous code did not check if data[0] was equal to '1403' or '1400', but it set the value '1403' or '1400' to data[0].

    As a reminder :

    The = operator assigns a value to a variable.

    The == operator compares the values of two variables.

    The === operator compares the values and types of two variables.

    In your example, 1400 or 1403 will be of type string, so you can make a strict comparison by asking to compare the value (1400 or 1403) and its type (string) using the === operator.

    To illustrate :

    1400 = '1400' would return an error in the console because it is not possible to assign a value to another non-variable value.

    1400 == '1400' would return true, because the values are the same.

    1400 === '1400' would return false, because the values are the same but not their types.

    Another way to write the function would have been to use a switch. I'll let you look at it here https://www.w3schools.com/js/js_switch.asp

    The function would be:

    "createdRow" : function(row, data, index) {
        console.log(data[0]);
    
        switch (data[0]) {
            case '1403':
                $(row).css('background-color', 'blue');
                break;
            case '1400':
                $(row).css('background-color', 'yellow');
                break;
           default:
               $(row).css('background-color', 'white');
        }
    },
    

    This writing could be clearer and has the advantage of not repeating data[0] === for all the checks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?