0

I have a Datatable plugin working with ajax to load the data. I want get a result in a column based on 2 conditionals. The below code is working good but with just one conditional. I'mnot sue how to add status_token to the equation. I want to add status_token variable the same as status is working.

I've tried status, status_token and { data: {'status': status, 'status_token': status_token}, render: function(status){ but it's not working.

$('#tableEvent').DataTable({
    processing: true,
    serverSide: true,
    ajax: "{!! route('datatables.data') !!}",
    columns: [
{ data: 'status_token', name: 'status_token' },
{ data: 'status', //<-- how can I add another variable status_token?
    render: function(status){ //<-- same here status_token
        if(status == 'hello'){
            return 'aa';

        }else{
            if(status_token == 'bye'){
                return 'bb';

            }else{
                return 'cc';
            } 
        } 
    }
}

As per Rohit.007 suggestion I tried this code

{ data: {'status': 'status', 'status_token': 'status_token'},
    render: function(status, status_token){

But for some reason it's still not working, checking out the variable status_token I get "display" no idea what's that and why it's different from the first column which is returning the correct information.

New whole code:

$('#tableEvent').DataTable({
    processing: true,
    serverSide: true,
    ajax: "{!! route('datatables.data') !!}",
    columns: [

{ data: 'status_token', name: 'status_token' },

{ data: {'status': 'status', 'status_token': 'status_token'},
    render: function(status, status_token){
        if(status == 'hello'){
            return 'aa';

        }else{
            if(status_token == 'bye'){
                return 'bb';

            }else{
                return 'cc';
            } 
        } 
    }
}
],

responsive: true

});
3
  • Can you elaborate more? Commented May 18, 2018 at 18:37
  • Thanks for your comment and answer! I think you got what I wanted, but it's not working. On the other hand, I've updated the question to make it more clear Commented May 18, 2018 at 18:52
  • You must be passing it in function calls somewhere in you code like render('status','display') Commented May 18, 2018 at 19:54

3 Answers 3

1

I didn't get you but still, are you looking for a similar thing?

{
  data: {
    'status': false,
    'status_token': 'asdfasdfasdfasdfasdf'
  },
  render: function(status){
    if(status=='hello'){
      return'aa';
    }else{
      if(status_token=='bye'){
        return'bb';
      }else{
        return'cc';
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

for some reason it's giving me Uncaught ReferenceError: status_token is not defined
Do you have any variable with status_token? OR just try with quotes like { data: {'status': 'status', 'status_token': 'status_token'}, render: function(status){
either way I think it has to be render: function(status, status_token){ right?
Yes you can send it as parameter
question updated. but this is weird, the status_token variable on the first column it's working perfectly fine but on the second if I check it's returning me "display" what's display??
|
0

Add another parameter in your render function. Then using it get the value of status_token field.

render: function(status, type, row){
        if(status == 'hello'){
            return 'aa';

        }else{
            if(row.status_token == 'bye'){
                return 'bb';

            }else{
                return 'cc';
            } 
        } 
    }

Updated whole code:

$('#tableEvent').DataTable({
  processing: true,
  serverSide: true,
  ajax: "{!! route('datatables.data') !!}",
  columns: [{ data: 'status_token', name: "status_token" },
    { data: 'status': 'status', name: 'status',
      render: function(status, type, row) {
        if (status == 'hello') {
          return 'aa';

        } else {
          if (row.status_token == 'bye') {
            return 'bb';

          } else {
            return 'cc';
          }
        }
      }
    }
  ],
  responsive: true
});

https://datatables.net/reference/option/columns.render

Comments

0

$('#tableEvent').DataTable({
  processing: true,
  serverSide: true,
  ajax: "{!! route('datatables.data') !!}",
  columns: [

    {
      data: 'status_token',
      name: 'status_token'
    },

    {
      data: {
        'status': 'status',
        'status_token': 'status_token'
      },
      render: function(data) {
        if (data.status == 'hello') {
          return 'aa';

        } else {
          if (data.status_token == 'bye') {
            return 'bb';

          } else {
            return 'cc';
          }
        }
      }
    }
  ],

  responsive: true

});

It works for me.

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.