datatablesphalconvolt

Datatable data inside another data


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>

Solution

  • 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> '
                  : '';
         }
       }
    ]