jc.021286
jc.021286

Reputation: 220

as3 reuse datagrid cellrenderer with different variables

I have a couple very large datagrids with custom cell renderers. The issue I am facing is that I now have > 80 of these, 1 for each column per data grid.

I'm curious if there is a way I can get these reduced down into 1 global cell renderer that I could pass variables into to define what is allowed by the cell renderer for that column.

ie:

...
col1 = new DataGridColumn("PurchaseStartDate");
        col1.headerText = "Purchase Date (YYYY-MM)";
        dg.addColumn(col1);
            col1.width = 110;
            col1.editable = false;
            col1.sortable = false;
            col1.cellRenderer = Alternating_Row_Colours_editable36;
                Alternating_Row_Colours_editable36._dg = dg;
                Alternating_Row_Colours_editable36.__enabled = true;
                Alternating_Row_Colours_editable36._myCol = 12;
                Alternating_Row_Colours_editable36._isNum = 3;
                Alternating_Row_Colours_editable36._stage = this;
                Alternating_Row_Colours_editable36._maxChars = 9;

within the cell renderer I'm using all of those variables to customize what is allowed. ie:

...
public function Alternating_Row_Colours_editable36()
    {
        super();
            if(_isNum == 0){
                restrict = "a-z A-Z @_,.0-9//-";
                maxChars = 64;
            }else if (_isNum == 1){
                restrict = ".0-9";
                maxChars = 9;
            }else if (_isNum == 2){
                restrict = "0-9";
                maxChars = 2;
            }else if (_isNum == 3){
                restrict = "0-9 \\-";
                maxChars = 9;
            }else if (_isNum == 4){
                restrict = "0-9. %";
                maxChars = 9;
            }
            if(_maxChars != -1){
                maxChars = _maxChars;
            }

The issue if you look at the above, I just noted that I had an error. I used "//-" to escape the hyphen instead of "\-". Now I've got 80+ changes to make and every time I try to add something new, for a callback, restricts, max chars, making it editable, scrubbing the input, changing it from dynamic to input and back again...

I would love to know if there is a way, to create an instance of the class and use that cell renderer. Or to be able to pass variables that affect only that column and not all columns. I'm not the best, but I was under the impression that it might just be a set/get function I need to use, or change the variables to protected, private or something to get this desired result.

Anyone have any suggestions on how to bring these 80+ cell renderers under control. As I am not looking forward to needing to make the changes to them for sorting down the road...

jc

Upvotes: 1

Views: 437

Answers (1)

John Whiteman
John Whiteman

Reputation: 31

I know this is a very late reply and you've more than likely moved on by now!

You can do it by using the information in 'listData' property of the CellRenderer class:

// Create a private class level variable called _dataField...
private var _dataField:String;

// Assign the dataField...
public function set listData(value:ListData):void {
    _listData = value;                  

    this._dataField = DataGridColumn(DataGrid(this._listData.owner).columns[this._listData.column]).dataField;

    // set the data again now...
    this.data = _data;          

    invalidate(InvalidationType.DATA);
    invalidate(InvalidationType.STATE);
}

// Use the dataField when setting value from DataProvider...
public function set data(value:Object):void {                        
    _data = value;          
    if (this._dataField != "")
    {
        this.text = value[this._dataField]; 
    }                       
}

Hope that satisfies any curiosity. Shame they don't just pass that property to the CellRenderer in the first place!

Cheers

Upvotes: 1

Related Questions