传统HTML5的下拉框select仅仅能实现简单的文字下拉列表,而HT for Web通用组件中ComboBox不只能够实现传统HTML5下拉框效果,而且能够在文本框和下拉列表中加入本身定义的小图标,让整个组件看起来更直观。今天我就怎样制定ComboBox本身定义下拉框作一番探讨。javascript
首先咱们先来目击下效果:java
看起来跟普通的ComboBox好像也没什么特殊的,是的,依照规范的ComboBox设计,全然可以实现相同的效果,但是今天的主要任务并不是讨论有多少实现方案,今天的首要任务是介绍HT for Web的ComboBox本身定义下拉列表的使用方法。数组
那么接下来就開始详细的方案介绍,废话很少说,上代码:网络
function createGradientComboBox(dataModel){ var sm = dataModel.sm(), gradientComboBox = new ht.widget.ComboBox(), gradients = ['linear.southwest','linear.southeast','linear.northwest','linear.northeast', 'linear.north','linear.south','linear.west','linear.east', 'radial.center','radial.southwest','radial.southeast','radial.northwest','radial.northeast', 'radial.north','radial.south','radial.west','radial.east', 'spread.horizontal','spread.vertical','spread.diagonal','spread.antidiagonal', 'spread.north','spread.south','spread.west','spread.east'], gradientImages = [], indent = gradientComboBox.getIndent(), height = 18, padding = 2; gradients.forEach(function (gradient) { gradientImages[gradient] = { width: indent, height: height, comps: [ { type: 'rect', rect: [padding, padding, indent - 2 * padding, height - 2 * padding], background: 'red', gradient: gradient, gradientColor: 'white' } ] }; }); gradientComboBox.setValues(gradients); gradientComboBox.setValue('linear.southwest'); gradientComboBox.setWidth(90); gradientComboBox.setDropDownWidth(140); gradientComboBox.drawValue = function(g, value, selected, x, y, w, h){ var self = this, indent = self.getIndent(), label = self.toLabel(value), icon = gradientImages[value]; if(icon){ ht.Default.drawCenterImage(g, icon, x+indent/2, y+h/2); x += indent; } if(label){ ht.Default.drawText(g, label, self.getLabelFont(value, selected), self.getLabelColor(value, selected), x, y, 0, h); } }; gradientComboBox.onValueChanged = function(oldValue, newValue){ onComboBoxValueChanged(dataModel, oldValue, newValue, 'shape.gradient', this); }; return gradientComboBox; }
这是背景渐变效果列表的详细构建代码,我来描写叙述下详细的设计思路:this
整体思路就是这样子的。对应的图形ComboBox组件也是如此的设计思路,接下来咱们来了解下ComboBox和HT for Web网络拓扑图组件GraphView的联动效果实现吧。spa
经过两张图的对照,我相信你们均可以感觉到变化吧。设计
前一张是GraphView的初始状态,后一张是经过选中图元后改动gradient渐变选择框后的效果,咱们来看看详细的代码实现,GraphView和Node的建立我就不在这多说了。直接上事件处理的详细实现代码:code
function onComboBoxValueChanged(dataModel, oldValue, newValue, style, scope){ var sm = dataModel.sm(); if(sm.size() === 0){ dataModel.each(function(data){ data.setStyle(style, newValue); }, scope); }else{ sm.each(function(data){ data.setStyle(style, newValue); }, scope); } }
代码很是easy,作的事情也很是easy。接下来咱们就来分析下代码的详细实现:blog
到此自绘制HT for Web ComboBox下拉框组件的介绍就结束了。HT for Web通用组件的灵活性和易用性还不止如此,在本文中涉及到矢量、ComboBox、几个关键知识点的拓扑结构部件等。在可能的文章会作详细,欢迎你们关注。事件