I have a combobox in which you can select a specific period of time, for example:
5 minutes 15 minutes 1 hour 2 hours 1 day 2 days 1 week 2 weeks
It always passes the number of minutes to the server, but the user does not understand what β10080β means (before you try to calculate: this is a week).
The new requirement is that the user should be able to enter arbitrary values ββin this field. For instance. β20 minutesβ, β1 hour 5 minutesβ, β2h 5mβ or β1d 6h 120mβ; and that if the field is set to a certain value (for example, 75) programmatically, the field should display the correct line (1 hour 15 minutes)
So, I wrote a parser and a formatting function (see below), but how can I use its combobox?
I already tried to override a couple of rawToValue / valueToRaw functions, similar to what I found in the datefield code:
rawToValue:function(rawValue) { console.log('rawToValue'); console.log(rawValue); return this.parse(rawValue) || rawValue || null; }, valueToRaw:function(value) { console.log('valueToRaw'); console.log(value); return this.format(value); },
but they are not called, I do not get output to the console log.
These are the parser / formatting functions:
Ext.define('AlarmTimeField',{ extend:'Ext.form.field.ComboBox', format:function(minutes) { var a = []; Ext.each(this.units, function(unit) { if(minutes >= unit.minutes) { var unitCount = Math.floor(minutes/unit.minutes); console.log(unitCount); minutes = minutes-unitCount*unit.minutes; a.push("" + unitCount + " " + (unitCount==1?unit.singular:unit.plural)); } }); return a.join(' '); }, parse:function(input) { if(!input) return 0; var me=this, inputSplit = input.split(' '), value = 0, lastNum = 0; Ext.each(inputSplit,function(input) { if(!input) return; else if(Ext.isNumeric(input)) lastNum = input; else if(Ext.isNumeric(input[0])) { var inputUnit = input.slice(-1), inputValue = input.slice(0,-1); Ext.each(me.units,function(unit) { if(inputUnit==unit.abbr) { value+=unit.minutes*inputValue; } }); } else { Ext.each(me.units,function(unit) { if(input==unit.singular || input==unit.plural || input==unit.abbr) { value+=unit.minutes*lastNum; } }); } }); return value; }, units:[{ minutes:10080, abbr:'w', singular:'week', plural:'weeks' },{ minutes:1440, abbr:'d', singular:'day', plural:'days' },{ minutes:60, abbr:'h', singular:'hour', plural:'hours' },{ minutes:1, abbr:'m', singular:'minute', plural:'minutes' }] });