weixin_33721427 2018-08-16 07:27 采纳率: 0%
浏览 22

Ajax:评估If中的值

My USE_IsInactive variable is either 1 or 0, depending on the user. I would like that in the display of my Users (via datatables), if the user is activated, the link 'Deactive is displayed', and if the user is already disabled, then the link 'Activate' must s' display.

I thought it would be very simple with a small if, but he can not find the value of 'USE_IsInactive'. I tried with == 0 ... I also tried to put a # before the value, nothing makes it.

A little help please?

            columns: [
            { data: "USE_FirstName" },
            { data: "USE_LastName" },
            { data: "USE_Gender" },
            { data: "Country" },
            { data: "USE_PhoneNumber" },
            { data: "USE_FirstName" },
            { data: "USE_IsInactive" },
            {
                date: "USE_Id", "render": function (data) {
                    if ('USE_IsInactive' == true)
                        return '<a class="btn btn-primary btn-sm edit" id="' + data + '">Edit</a> <a class="btn btn-success btn-sm acUser" id="' + data + '">Activate</a>';
                    else
                        return '<a class="btn btn-primary btn-sm edit" id="' + data + '">Edit</a> <a class="btn btn-danger btn-sm deacUser" id="' + data + '">Deactivate</a>';
                }
            }


        ]

Link to the screenshot code

Thank you in advance!

Jon

  • 写回答

1条回答 默认 最新

  • weixin_33721344 2018-08-16 07:57
    关注

    You're comparing a boolean value with string here, hence desired result never happens because it always returns false:

    if ('USE_IsInactive' == true)
        return '<a class="btn btn-primary btn-sm edit" id="' + data + '">Edit</a> <a class="btn btn-success btn-sm acUser" id="' + data + '">Activate</a>';
    

    What you should do to check against row values is using row argument and use column name definition from there:

    columns: [
        // other columns
        { data: "USE_IsInactive" },
        {
          data: "USE_Id", 
          "render": function (data, type, row) {
    
               // row["USE_IsInactive"] may also valid, check them both
               if (row.USE_IsInactive === true) {
                   return '<a class="btn btn-primary btn-sm edit" id="' + data + '">Edit</a> <a class="btn btn-success btn-sm acUser" id="' + data + '">Activate</a>';
               }
               else {
                   return '<a class="btn btn-primary btn-sm edit" id="' + data + '">Edit</a> <a class="btn btn-danger btn-sm deacUser" id="' + data + '">Deactivate</a>';
               }
           }
        }
    ]
    

    Reference:

    If and else condition with DataTables

    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)