0

Problem

I want to define conditions inside the Datatables..

So basically I use a session to filter the output of the Data.


Case

I logged in as John :

| Name | Quantity  |  Action               |

| John | 400       | #**CanSeeEditButton** |

| Mia  | 300       | #CannotSeeEditButton  |

| John | 200       | #**CanSeeEditButton** |

| Alex | 900       | #CannotSeeEditButton  |

I want to give condition below, I have session = userId, and this record table has it too, so I want to match session userId = record userId. and id below is primary Key of table (not userID)

I'm stuck on how to condition it. Here is my code

<script>
    $(document).ready(function () {
        $('#dataTable').DataTable({
            serverSide : true,
            ajax : {
                url     : '{{ this.url.getBaseUri() }}productionstocklog/read',
                method  : 'POST'
            },
            columns: [
                {data: "name"},
                {data: "qty"},
                // I want to give condition below, I have session = userId, and this record table have it too, so I want to match session userId = record userId. and id below is primary Key of table (not userID)

            {% if this.session.get('auth')['userId'] == data: "userId" // <-- how to get data:userId in Datatables ? %}
                {data: "id",  render  : function( data, type, full, meta){
                    return  ' <a data-toggle="modal" data-target="#edit" class="btn btn-xs btn-success icon-pencil icon" ' +
                            ' onclick="EditShow(\''+data+'\');"> Edit </a>' +
                            ' <a href="{{ this.url.getBaseUri() }}productionstocklog/delete/'+data+'" class="btn btn-xs btn-danger icon-ban icon" ' +
                            ' onclick="return confirm(\'Delete?\');"> Delete </a> '
                }}
            {% endif %}
            ]
        }); 
    });
</script>
Timothy
  • 2,004
  • 3
  • 23
  • 29
code K.O.
  • 171
  • 1
  • 1
  • 14

1 Answers1

1

You can access full data set using full variable, for example full['userId'] to access userId field.

Try the columns option below:

columns: [
   { data: "name"},
   { data: "qty"},
   { 
     data: "id",  
     render: function(data, type, full, meta){               
        return  
           (full['userId'] === "{% this.session.get('auth')['userId']|escape_js %}")
              ? '<a data-toggle="modal" data-target="#edit" class="btn btn-xs btn-success icon-pencil icon" ' +
                ' onclick="EditShow(\''+data+'\');"> Edit </a>' +
                ' <a href="{{ this.url.getBaseUri() }}productionstocklog/delete/'+data+'" class="btn btn-xs btn-danger icon-ban icon" ' +
                ' onclick="return confirm(\'Delete?\');"> Delete </a> '
              : '';
     }
   }
]
Gyrocode.com
  • 57,606
  • 14
  • 150
  • 185
  • thanks, would u mind to little explain what is it escape_js ? – code K.O. Jul 20 '16 at 03:44
  • @codeK.O., I'm not familiar with Volt template engine but after reading [documentation](https://docs.phalconphp.com/en/latest/reference/volt.html#filters) on how to show variables, I saw a filter for JavaScript `escape_js` that escapes certain characters so that variable can be used in JavaScript code. Hope that the code works since I've not tested it. – Gyrocode.com Jul 20 '16 at 13:02
  • its work like a charm.. thank you for the appreciate – code K.O. Jul 20 '16 at 14:02