ComboTree.cs代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web.UI;
using Coolite.Ext.Web;
[assembly: WebResource("Coolite.Ext.Web.Build.Resources.Coolite.ux.extensions.ComboTree.ComboTree.js", "text/javascript")]
namespace Coolite.Ext.Web
{
[Designer(typeof(EmptyDesigner))]
[DefaultProperty("")]
[Xtype("combotree")]
[InstanceOf(ClassName = "Ext.ux.ComboTree")]
// FilePath = "/ux/extensions/combotree/combotree.js",
[ClientScript(Type = typeof(ComboTree),
WebResource = "Coolite.Ext.Web.Build.Resources.Coolite.ux.extensions.ComboTree.ComboTree.js")]
[ToolboxData("<{0}:ComboTree runat=\"server\" Title=\"Combo tree\" Height=\"300\"></{0}:ComboTree>")]
[Description("Combobox with tree functionality")]
public class ComboTree : ComboBox
{
private ItemsCollection<TreePanel> tree;
[ClientConfig("tree", typeof(ItemCollectionJsonConverter))]
[Category("Config Options")]
[NotifyParentProperty(true)]
[DefaultValue(null)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public virtual ItemsCollection<TreePanel> Tree
{
get
{
if (this.tree == null)
{
this.tree = new ItemsCollection<TreePanel>();
}
return this.tree;
}
}
protected override void OnLoad(EventArgs e)
{
this.Controls.Add(Tree[0]);
if (!this.LazyItems.Contains(Tree[0]))
{
this.LazyItems.Add(Tree[0]);
}
base.OnLoad(e);
}
}
}
ComboTree.js 代码:
Ext.namespace('Ext.ux');
/**
*
* @class ComboTree
* @extends Ext.form.ComboBox
*/
Ext.ux.ComboTree = Ext.extend(Ext.form.ComboBox, {
tree: null,
treeId: 0,
setValue: function(v) {
var text = v;
if (this.valueField) {
var r = this.findExtRecord(this.valueField, v);
if (r) {
text = r.data[this.displayField];
} else if (this.valueNotFoundText !== undefined) {
text = this.valueNotFoundText;
}
}
Ext.ux.ComboTree.superclass.setValue.call(this, text);
this.lastSelectionText = text;
if (this.hiddenField) {
this.hiddenField.value = v;
}
this.value = v;
},
initComponent: function() {
this.treeId = Ext.id();
this.focusLinkId = Ext.id();
Ext.apply(this, {
store: new Ext.data.SimpleStore({
fields: ["id", "text"],
data: [["", ""]]
}),
editable: false,
shadow: false,
mode: 'local',
triggerAction: 'all',
maxHeight: 200,
tpl: '<tpl for="."><div style="height:200px"><div id="'
+ this.treeId + '"></div></div></tpl>',
selectedClass: '',
onSelect: Ext.emptyFn,
valueField: 'id',
displayField: 'text'
});
this.tree = new Coolite.Ext.TreePanel(this.tree);
this.on('expand', this.onExpand);
this.tree.on('click', this.onClick, this);
Ext.ux.ComboTree.superclass.initComponent.apply(this, arguments);
},
findExtRecord: function(prop, value) {
var record;
if (this.store != null) {
if (this.store.getCount() > 0) {
this.store.each(function(r) {
if (r.data[prop] == value) {
record = r;
return false;
}
});
}
}
return record;
},
onClick: function(node) {
if (node.attributes.parameter == 9) {
} else {
this.setValue(node.text);
this.hiddenField.value = node.id;
this.collapse();
this.store = new Ext.data.SimpleStore({
fields: ["id", "text"],
data: [[node.id, node.text]]
});
}
},
onExpand: function() {
if (!this.tree.rendered)
this.tree.render(this.treeId);
else this.tree.show();
}
});
Ext.reg("combotree", Ext.ux.ComboTree);