傳統的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的建立我就不在這多說了,直接上事件處理的具體實現代碼:設計
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); } }
代碼很簡單,作的事情也很簡單,接下來咱們就來分析下代碼的具體實現:3d
到此自繪製HT for Web ComboBox下拉框組件的介紹就結束了,HT for Web通用組件的靈活性和易用性還不止如此,在本文中涉及到矢量、ComboBox、拓撲圖組件等幾個關鍵的知識點,在後續的文章中會作具體的闡述,歡迎你們屆時關注。blog