Home > Mobile >  How to apply if statement to DataTable option
How to apply if statement to DataTable option

Time:11-04

I am using the DataTable plugin. When accessing the page with the table, if the currently logged in account rank is 'student', I want the currently logged in account username to be the default value of the search box, so I set it up as follows.

$('#study-list').DataTable( {
    "ordering": false,
    'rowsGroup': [1,2,3],

    if('{{ request.user.groups.all.0 }}' == 'student'){
        'oSearch': {'sSearch': '{{request.user.first_name}}'},
    }

    initComplete: function () {
    ....

But the if statement doesn't apply. What do I need to modify to add an If statement to the DataTable options?

CodePudding user response:

Yep, that's an object so you can't add if logic in there, but you can add a ternary condition, something like

    'rowsGroup': [1,2,3],

    'search' : {{ request.user.groups.all.0 }} == 'student'? 
      {{request.user.first_name}} :
      '',

    initComplete: function () {
  • Related