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

标签 javascript jquery jtable

我使用 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 : after property 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 not found 错误:在此服务器上找不到请求的 url/[object object]。有没有人有任何想法如何让这些例子工作有一个不同的例子如何让表格启用\启用编辑,更新按钮?这对我来说是全新的,所以我现在道歉

最佳答案

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();

                                    }

关于javascript - jTable 条件显示\隐藏基于数据所有者的编辑和删除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20542099/

相关文章:

javascript - cordova中的文本字段显示错误

javascript - 如何让Gulp完成一项任务,然后才开始下一项任务

swing - 更新 TableModel 中的列数

javascript - 使用 JavaScript 或 jQuery,如何在 <img> 或 <div> 元素中特别是鼠标移动的地方获取 RGB 颜色

javascript - 跨多个浏览器测试 Javascript

javascript - 读取外部 .msg 文件并解析它时出错

jquery - 如何在Jquery中通过唯一性更改ID

javascript - jquery 在 Internet Explorer 8 中未正确执行

java - 使用 AbstractTableModel 从 JTable 中删除行

java - 如何处理 JTable 单元格中的多个超链接?