jTable条件显示隐藏基于数据所有者的编辑和删除按钮

jTable Conditional showhide edit and delete buttons based on owner of data

本文关键字:所有者 编辑 按钮 删除 数据 显示 条件 隐藏 于数据 jTable      更新时间:2023-09-26

我使用jTable来显示CD信息,并使用子表来显示该CD的评论。我希望只能为登录的用户显示行上的编辑''删除按钮。我一直在努力遵循以下建议:https://github.com/hikalkan/jtable/issues/113

https://github.com/hikalkan/jtable/issues/893

https://github.com/hikalkan/jtable/issues/620

老实说,我在这些例子中运气都不好。我们被告知要在作业中包含一些jquery,所以我选择将其用于表数据。我希望现在我刚刚做了一些非常基本的事情!

无条件工作jTable:

display: function (reviewData) {
                    //Create an image that will be used to open child table
                    var $img = $('<img class="child-opener-image" src="/Content/images/Misc/list_metro.png" title="List Reviews" />');
                    //Open child table when user clicks the image
                    $img.click(function () {
                        $('#ReviewTableContainer').jtable('openChildTable',
                                $img.closest('tr'),
                                {
                                    title: "Your reviews on this album",
                                    actions: {
                      listAction: 'childReviewActions.php?action=list&ID=' + reviewData.record.CDID,
                                          deleteAction: 'childReviewActions.php?action=delete&ID=' + reviewData.record.CDID,
                                          updateAction: 'childReviewActions.php?action=update&ID=' + reviewData.record.CDID
                                    },  
                                    fields: {
                                        userID: {
                                        key: true,
                                        create: false,  
                                        edit: false,
                                        list: false
                                        },
                                        userName: {
                                            title: 'User',
                                            edit: false,
                                            width: '20%'
                                        },
                                        reviewDate: {
                                            title: 'Review date',
                                            width: '20%',
                                            type: 'date',
                                            edit: false,
                                            displayFormat: 'dd-mm-yy'
                                        },
                                        reviewText: {
                                            title: 'Review',
                                            type: 'textarea',
                                            width: '40%'
                                        }
                                    },

问题620尝试:

actions: {
    listAction: 'childReviewActions.php?action=list&ID=' + reviewData.record.CDID,
    @if (reviewData.record.userID == <?php echo mysql_real_escape_string($_SESSION['ID']);?>)
    {
        deleteAction: 'childReviewActions.php?action=delete&ID=' + reviewData.record.CDID,
        updateAction: 'childReviewActions.php?action=update&ID=' + reviewData.record.CDID
    }
},

这种方式会导致编译错误:IF语句上的属性id无效。如果我去掉If语句中的@,我会得到:missing:在属性id之后。

第113期&893次尝试:

actions: {
    listAction: {
        url:'http://localhost/childReviewActions.php?action=list&ID=' + reviewData.record.CDID
//updateAction: {
        //url:'childReviewActions.php?action=update&ID=' + reviewData.record.CDID,
    //enabled: function (data) {
            //return data.record.userID = <?php echo mysql_real_escape_string($_SESSION['ID']);?>;
        //}
    //}
},                                      

在这个问题上,我甚至无法列出子表的内容。它不断返回404未找到错误:在该服务器上未找到请求的url/[对象对象]。有人知道如何让这些例子发挥作用吗?有一个不同的例子说明如何让表格启用编辑、更新按钮吗?这对我来说都是新的,所以我现在道歉

rowInserted: function (event, data) { 
                                        //After child row loads. Check if the review belongs to the member logged in. If not remove the edit/delete buttons
                                        if (data.record.userID != $user) { 
                                            data.row.find('.jtable-edit-command-button').hide(); 
                                            data.row.find('.jtable-delete-command-button').hide();
                                        }
                                        else{
                                            //If a review record does belong to the user set variable to true so the add new review link can be hidden after all records have been loaded
                                            $memberReviewExists = true;
                                            //Also needed here for when a new record is inserted
                                            $(".jtable-add-record").hide();
                                        }
                                    },
                                    recordsLoaded: function (event, data) {
                                        if (typeof $memberReviewExists != 'undefined' && $memberReviewExists == true){
                                            $(".jtable-add-record").hide();
                                            $memberReviewExists = null;
                                        }
                                        else {
    //No review currently exists for this user so show the Add review link                                      $(".jtable-add-record").show();
                                        }
                                    },
                                    recordDeleted: function (event, data) {
                                        //User has deleted their review. Re-show the add new review link
                                        $(".jtable-add-record").show();
                                    }

以下操作对我很有效。它隐藏了当前用户不是授权用户的行上的编辑/删除按钮。注意:我在mysql表中为authorizedUser添加了一列,并使用它来确定是否允许该用户。

rowInserted: function(event, data){
     var $currentUser='<?php echo $_SESSION['email']?>';
     if (data.record.authorizedUser != $currentUser) {
        data.row.find('.jtable-edit-command-button').hide();
        data.row.find('.jtable-delete-command-button').hide();
     }
  },

@Toni您的代码也包含asp.net代码。@是ASP.NET指令。