另一个数据中的数据表数据

Datatable data inside another data

问题

我想在 Datatables..

中定义条件

所以基本上我使用会话来过滤数据的输出。


案例

我以 John 身份登录:

| Name | Quantity  |  Action               |

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

| Mia  | 300       | #CannotSeeEditButton  |

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

| Alex | 900       | #CannotSeeEditButton  |

我想在下面给出条件,我有session = userId,这条记录table也有,所以我想匹配session userId = record userId。下面的 id 是 table 的主键(不是用户 ID)

我对如何调整它感到困惑。这是我的代码

<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>

您可以使用 full 变量访问完整数据集,例如 full['userId'] 访问 userId 字段。

试试下面的 columns 选项:

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