3

I have a problem like my title. Here is the data I want to convert:

enter image description here

I want to convert 1 to Active and 0 to Not Active.

This is my datatables script:

$(function () {
            $('#userTable').DataTable({
                "paging": true,
                "lengthChange": true,
                "searching": true,
                "ordering": true,
                "info": true,
                "autoWidth": true,
                "scrollX": true,
                "ajax": {
                    "url" : "admin/auth/getUserData",
                    "dataSrc" : function(json) {
                        console.log(json);
                        return json.Users
                    }
                },
                "columns": [
                    { "data": "Username"},
                    { "data": "Email"},
                    { "data": "Fullname"},
                    { "data": "IdentityNumber"},
                    { "data": "InstitutionName"},
                    { "data": "UserStatus"},
});
        });

In our logic, it must be

if("data":"UserStatus" == 1)
    document.write("Active");
else
    document.write("Not active");

I've tried the code above but it failed to get the output I want. Can anybody help me fix this? Thanks

5
  • I believe you missed the "columns" part of it, so it doesn't know where to actually check and get the value 1 Could you show us the HTML that you're using for this section as well? Commented Dec 7, 2015 at 5:20
  • 1
    you have some syntax error in your code. [ is not closed. Commented Dec 7, 2015 at 5:23
  • 1
    The line if("data":"UserStatus" == 1) is not valid JavaScript. Commented Dec 7, 2015 at 5:23
  • try to get value by ID or class , then check inside if condition Commented Dec 7, 2015 at 5:26
  • to convert string to integer u can use parseInt. Commented Dec 7, 2015 at 5:27

1 Answer 1

9

You can use a columns render function to do that :

"columns": [
   { "data": "Username"},
   { "data": "Email"},
   { "data": "Fullname"},
   { "data": "IdentityNumber"},
   { "data": "InstitutionName"},
   { "data": "UserStatus",
      render : function(data) {
         return data == '0' ? 'Not Active' : 'Active';
      }
   }
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.