PDA

View Full Version : Restrict editable datagrid column to numbers only


gabrielthegrey
10-02-2005, 09:20 PM
Hi,
I have a data grid with some editable columns which I only want to have numbers input into. I have tried the code below with no luck. Any ideas?

myDataGrid.getColumnAt(4).restrict = "0-9";

madgett
10-03-2005, 05:05 AM
To manipulate the content that is displayed within datagrid, combobox, checkbox, list cells, you would create a custom CellRenderer class. However, it might be a bug, but even when I've used a custom cellrenderer the restrict property will not work in the datagrid...hopefully this can be resolved.

The way around it, is to use the cellEdit event to listen to user input and then check it using a Number cast, and if NaN shows up, delete the text of the cell that called the event.

gabrielthegrey
10-05-2005, 06:30 PM
Thanks madgett. I tried the cell renderer too, what a messy way to control things. I had no luck with the restrict either, so I also tried the isNaN approach, which worked. Here's a sample of what I did for anyone looking for this too:

var nanReplace:Number = 1;//set the number I will turn any non number into.
function quantityUpdate(event:Object) {
var newQuantity = myDatagrid.getItemAt(event.itemIndex)["Quantity"];//gets whatever the user types into the editable cell.
if (isNaN(newQuantity )) {//checks to see if it is not a number, and if not, makes it 1.
myDatagrid.editField((event.itemIndex), ["Quantity"], nanReplace);
}
}
myDatagrid.addEventListener("cellEdit", quantityUpdate);

panel
09-19-2012, 02:07 PM
Hello

Unfortunately you solution wasn't working for me so I been trying to figure this out.

I tried to use custom renderer solution, but it involved hard coded field names and this wasn't option for me.

Finally I got it working using DataGridEvent.ITEM_EDIT_END event.
The idea is simple after editing item chceck if new value os ok for you, if it's ok leave it, if not change it to old value.


_valueDataGrid = new DataGrid();
_valueDataGrid.editable = true;
_valueDataGrid.addEventListener(DataGridEvent.ITEM _EDIT_END, onItemEditEnd);

... //fill it with data

private function onItemEditEnd(event:DataGridEvent):void
{
var row:Number = Number(event.rowIndex);
var col:int = event.columnIndex;
var field:String = event.dataField;

// gets the value (pre-edit) from the grid's dataprovider
var oldValue:Number = Number(_valueDataGrid.dataProvider.getItemAt(row)[field]);

// get the value (post-edit) from the item editor
var newValue:Number = (_valueDataGrid.itemEditorInstance[_valueDataGrid.columns[col].editorDataField]);

// check if the new value is number
if (isNaN(newValue))
{
_valueDataGrid.itemEditorInstance[_valueDataGrid.columns[col].editorDataField] = oldValue;
}
}