【问题标题】:Ajax is not working when using getContext on datatable in Webix在 Webix 中的数据表上使用 getContext 时,Ajax 不起作用
【发布时间】:2020-01-01 20:35:22
【问题描述】:

我正在设置一个文件管理器,当右键单击文件夹时,它将弹出上下文菜单,然后弹出使用 ajax 禁用某些菜单项。但是当我右键单击数据表中排列的文件夹时,ajax调用不起作用。

这是我的弹出菜单项代码。右键单击数据表中排列的文件夹时出现错误。

webix.js?1.0.0:22356 Uncaught TypeError: Cannot read property 'row' of undefined"

    actions: {
            config: function () {
            var t = this.config.templateName;
            return {
            view: "contextmenu",
                    width: 200,
                    padding: 0,
                    autofocus: !1,
                    css: "webix_fmanager_actions",
                    template: function (e, i) {
                    var s = t(e, i);
                    return "<span class='webix_icon fa-" + e.icon + "'></span>" + s
                    },
                    data: "actionsData"
            }
            },
                    oninit: function () {
this.getMenu().attachEvent("onBeforeShow", function (t) { 

                    var e = this.getContext();
                    var folderid = e.id;

                $.ajax({
                    url: "<?php echo base_url()?>/Directorylist/directory_activity",
                    type: "POST",
                    datatype: 'json',
                    async: false,
                    data: {'folderid':folderid},
                    success: function (data) { 
                     var json = JSON.parse(data);
                        permission = [];
                        permission = json;
                    },
                });

            if (permission[0] == 1 && permission[1] == 0 && permission[2] == 0) {
                    $$(this).disableItem('copy');  
                    $$(this).disableItem('cut');  
                    $$(this).disableItem('paste');  
                    $$(this).disableItem('create');  
                    $$(this).disableItem('remove');  
                    $$(this).disableItem('edit');  
                    $$(this).disableItem('upload');  
                 }  
                    })
                    }
            },
                    actionsData: {
                    config: function () {
                    return [{
                    id: "copy",
                            method: "markCopy",
                            icon: "copy",
                            value: copy
                    }, {
                    id: "cut",
                            method: "markCut",
                            icon: "cut",
                            value: cut
                    }, {
                    id: "paste",
                            method: "pasteFile",
                            icon: "paste",
                            value: paste
                    }, {
                    $template: "Separator"
                    }, {
                    id: "create",
                            method: "createFolder",
                            icon: "folder-o",
                            value: create
                    }, {
                    id: "remove",
                            method: "deleteFile",
                            icon: "times",
                            value: remove
                    }, {
                    id: "edit",
                            method: "editFile",
                            icon: "edit",
                            value: rename
                    }, {
                    id: "upload",
                            method: "uploadFile",
                            icon: "upload",
                            value: upload
                    }]

                    }
                    }

但是当我删除这两行并给出直接值时,它工作正常。

var e = this.getContext();
var folderid = e.id;

删除并添加了以上 2 行

var folderid = '1';

【问题讨论】:

    标签: javascript ajax datatable webix


    【解决方案1】:

    在第二个函数之后,您失去了this 的范围。 这是一个久经考验的解决方法:

    oninit: function () {
        var self = this;
        this.getMenu().attachEvent("onBeforeShow", function (t) {
            var e = self.getContext();
            var folderid = e.id;
    

    另请参阅:Javascript "this" scope

    或者如果你使用的是 ES6,那么你可以使用箭头函数:

    oninit: function () {
        this.getMenu().attachEvent("onBeforeShow", (t) => {
            var e = this.getContext();
            var folderid = e.id;
    

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-21
      • 1970-01-01
      • 2017-07-24
      • 2019-09-10
      • 2013-08-23
      • 2018-12-11
      • 1970-01-01
      • 2021-03-06
      相关资源
      最近更新 更多