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

时间:2016-07-20 02:57:01

标签: datatables phalcon volt

问题

我想在Datatables中定义条件..

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

案例

我以John身份登录:

| Name | Quantity  |  Action               |

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

| Mia  | 300       | #CannotSeeEditButton  |

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

| Alex | 900       | #CannotSeeEditButton  |

我想在下面给出条件,我有session = userId,这个记录表也有,所以我想匹配session userId = record userId。并且下面的id是表的主键(不是用户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>

1 个答案:

答案 0 :(得分:1)

您可以使用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> '
              : '';
     }
   }
]