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

    评论

报告相同问题?