創建自定義的Widget在劍道我的js文件看起來像劍道自定義部件返回空實例
(function(kendo, $) {
var ui = kendo.ui,
Widget = ui.Widget
kendo.generateUUID = function() {
var d = new Date().getTime();
if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
d += performance.now(); //use high-precision timer if available
}
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d/16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
var ArcDropDownTreeView = kendo.ui.Widget.extend({
_treeView: null,
_uid: null,
_dropdown: null,
init: function(element, options) {
var that = this;
kendo.ui.Widget.fn.init.call(that, element, options);
// Widget.fn.init.call(this.element, options);
var newDivId,
treeElementId,
treeElement,
treeview,
dropDownElementId,
dropDownElement,
dropdown
uid = kendo.generateUUID();
newDivId = ("treeDropDown{0}").replace("{0}", uid);
treeElementId = ("treeViewElement{0}").replace("{0}", uid);
dropDownElementId = ("dropDownElement{0}").replace("{0}", uid);
var newDiv = $("<div></div>").prop("id", newDivId);
dropDownElement = $("<input/>").prop("id", dropDownElementId);
treeElement = $(element).clone().prop("id", treeElementId);
$(newDiv).append(dropDownElement).change();
$(newDiv).append(treeElement).change();
$(element).append(newDiv).change();
dropdown = $(dropDownElement).kendoDropDownList({
dataSource: [{
text: "",
value: ""
}],
dataTextField: "text",
dataValueField: "value",
}).data("kendoDropDownList");
dropdown.bind("open", function(e) {
e.preventDefault();
// If the treeview is not visible, then make it visible.
if (!$treeviewRootElem.hasClass("k-custom-visible")) {
$treeviewRootElem.slideToggle('fast', function() {
dropdown.close();
$treeviewRootElem.addClass("k-custom-visible");
});
}
});
var $dropdownRootElem = $(dropDownElement).closest("span.k-dropdown");
treeview = $(treeElement).kendoTreeView(options.treeview).data("kendoTreeView");
treeview.bind("select", function(e) {
// When a node is selected, display the text for the node in the dropdown and hide the treeview.
$dropdownRootElem.find("span.k-input").text($(e.node).children("div").text());
$treeviewRootElem.slideToggle('fast', function() {
$treeviewRootElem.removeClass("k-custom-visible");
});
});
var $treeviewRootElem = $(treeElement).closest("div.k-treeview");
// Hide the treeview.
var listBackgroundCss = dropdown.list.css("background-color");
$treeviewRootElem
.width($dropdownRootElem.width())
.css({
"border": "1px solid grey",
"display": "none",
"position": "absolute",
"background-color": listBackgroundCss,
"z-index": "999"
});
$(document).click(function(e) {
// Ignore clicks on the treetriew.
if ($(e.target).closest("div.k-treeview").length == 0) {
// If visible, then close the treeview.
if ($treeviewRootElem.hasClass("k-custom-visible")) {
$treeviewRootElem.slideToggle('fast', function() {
$treeviewRootElem.removeClass("k-custom-visible");
});
}
}
});
that._treeView = treeview;
that._dropdown = dropdown;
that._uid = uid;
},
treeView: function() {
console.log("Request for treeview");
return this._treeView;
},
dropDown: function() {
return this._dropdown;
},
dataSource: function() {
return this.treeview.datasource;
},
selectElement: function(id) {
this._treeView.select(id);
},
options: {
name: "ArcDropDownTreeView"
}
});
ui.plugin(ArcDropDownTreeView);
})(window.kendo, window.kendo.jQuery);
當我執行該代碼
var dropDownTreeView = $("#treeview").kendoArcDropDownTreeView({
treeview: {
dataTextField: "Name",
dataValueField: "ID",
loadOnDemand: true,
dataSource: new kendo.data.HierarchicalDataSource({
transport: {
read: {
url: '/Inventory/GetInventoryLocation',
dataType: "json",
data: rootData(),
contentType: 'application/json; charset=utf-8'
}
},
schema: {
model: {
id: "ID",
name: "Name",
hasChildren: "hasChildren"
}
}
})
}
});
var instance = dropDownTreeView.data("ArcDropDownTreeView");
instance.selectElement(258);
我得到instance.selectElement空誤差(258 );看起來dropDownTreeView.data(「ArcDropDownTreeView」)返回一個未定義的實例,我錯過了什麼?把我的頭髮拉出來,我創建的任何新窗口小部件都具有相同的行爲,但所有窗口小部件框架都基於https://github.com/jsExtensions/kendoui-extended-api,它們將運行。
這只是一個錯字嗎?嘗試數據(「ArcDropDownTreeView」)而不是數據(「ArcExtDropDownTreeView」)? – NigelK
謝謝,那是我一直試圖讓這個工作的許多事情之一。這是非常奇怪的行爲,就像我錯過了某個地方的鉤子。 –
這可能是一個不同的錯字?你已經調用了你的插件'ArcDropDownTreeView',但是然後調用'kendoArcDropDownTreeView'來在元素上初始化它。 – TLS