PDA

View Full Version : Ext.ux.NumberRangeField



mfoley
26 Feb 2008, 8:59 AM
Hey everyone, I tossed this one together yesterday for a project I'm doing. It's an extension of Ext.form.TextField, but accepts numeric ranges like on a print list, e.g "1,3,5-25". It has a get range function that will return an sorted array of integers generated by the range specified.


Ext.namespace('Ext.ux.NumberRangeField');
Ext.apply(Ext.form.VTypes,{
numberRangeMask:/[0-9\,\-]/ ,
numberRangeRe:/^[0-9\,\-]+$/ ,
numberRange:function(v){
return this.numberRangeRe.test(v);
}
});
Ext.ux.NumberRangeField = Ext.extend(Ext.form.TextField,{
vtype:'numberRange',
vtypeText:'Use a Valid Number Range',
getRange:function(){
var sortNumeric = function(a,b){
return a-b;
}
var value = this.getValue();
if(Ext.util.Format.trim(value)=="") return [""];
var initialSets = value.split(",");
var finalSet = [],internalSet=[];
for(var i=0,l=initialSets.length;i<l;i++){
internalSet = initialSets[i].split("-");
if(internalSet.length == 2){
var start=parseInt(internalSet[0]),end=parseInt(internalSet[1]);
for(var j=start;j<=end;j++){
finalSet[finalSet.length] = j;
}
} else if(internalSet.length == 1){
finalSet[finalSet.length] = parseInt(internalSet[0]);
}
}
finalSet.sort(sortNumeric);
return finalSet;
}
});
Ext.reg('numberrangefield',Ext.ux.NumberRangeField);

Not sure who this might help, but I hope it helps somebody :)