Kendo UI - 如何引用控件?

时间:2016-02-17 21:58:56

标签: javascript kendo-ui

假设我实例化一个剑道控制:

$("#files").kendoUpload({ 
 ...
})

如何在JavaScript中获取对该控件的引用?

3 个答案:

答案 0 :(得分:2)

您有两种方法可以做到这一点。

  1. 使用getKendo *方法:

    var myUpload = $(“#files”)。getKendoUpload();

  2. 使用数据方法:

    var myUpload = $(“#files”)。data(“kendoUpload”);

  3. 官方文档:http://docs.telerik.com/kendo-ui/intro/widget-basics/events-and-methods

答案 1 :(得分:0)

检索小部件实例

要获取对窗口小部件实例的引用,请使用jQuery data方法并将插件名称作为字符串传递,如下所示:

// Create the widget
$("#files").kendoUpload({ 
 ...
})

// retrieve the widget instance
var kendoUpload = $("#files").data("kendoUpload");

您可以找到more info on this at the official documentation

窗口小部件事件

此外,您还可以添加一大堆特定于Kendo的事件处理程序来处理由窗口小部件触发的事件:

function getFileInfo(e) {
    return $.map(e.files, function(file) {
        var info = file.name;
        // File size is not available in all browsers
        if (file.size > 0) {
            info  += " (" + Math.ceil(file.size / 1024) + " KB)";
        }
        return info;
    }).join(", ");
}

$("#files").kendoUpload({
    async: {
        saveUrl: "save",
        removeUrl: "remove",
        autoUpload: true
    },
    cancel: function(e) {
        kendoConsole.log("Cancel :: " + getFileInfo(e));
    },
    complete: function onComplete(e) {
        kendoConsole.log("Complete");
    },
    error: function(e) {
        kendoConsole.log("Error (" + e.operation + ") :: " + getFileInfo(e));
    },
    progress: function(e) {
        kendoConsole.log("Upload progress :: " + e.percentComplete + "% :: " + getFileInfo(e));
    },
    remove: function(e) {
        kendoConsole.log("Remove :: " + getFileInfo(e));
    },
    select: function(e) {
        kendoConsole.log("Select :: " + getFileInfo(e));
    },
    success: function(e) {
        kendoConsole.log("Success (" + e.operation + ") :: " + getFileInfo(e));
    },
    upload: function(e) {
        kendoConsole.log("Upload :: " + getFileInfo(e));
    }
});

您可以找到a fully working demo at the official documentation

答案 2 :(得分:0)

我现在这样做是为了获取上传控件的文件数组:

var logoUploader = $('#files').data('kendoUpload');
var files = logoUploader.options.files;