• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 947
  • Last Modified:

Flex datagrid w/editable field disabled

I have a datagrid with an editable column, but I want to be able to disable the edit ability on one particular cell on of the column.  Can I accomplish this with out a Item-render ?

0
sidwelle
Asked:
sidwelle
  • 3
  • 3
  • 2
2 Solutions
 
FrellingCommented:
Dear sidwelle:

    Not knowing the specific UI element that is being used, I'll assume a JTable for this discussion. In Swing, you can either control the editability status of a cell directly from the JTable or the underlying data model using their respective isCellEditable() method. For example the following sample class implements cell disabling in a JTable.

package ee.tablecell;

import java.awt.Point;
import java.util.*;
import javax.swing.JTable;

@SuppressWarnings("serial")
public class MyTable extends JTable {
    Set<Point> offCells = new HashSet<Point>();
   
    // Disable specific cell element by adding Point to disabled set.
    public void disableCellEdit( int row, int col ) {
        offCells.add( new Point( row, col ) );
    }
   
    // Enable specific cell element by removing Point from disabled set, if it exists.
    public void enableCellEdit( int row, int col ) {
        offCells.remove( new Point( row, col ) );
    }
   
    // Check disabled set. If Point exists, return false, otherwise return default value.
    @Override
    public boolean isCellEditable( int row, int col ) {
        if ( offCells.contains( new Point( row, col ) ) )
            return false;
        else
            return super.isCellEditable( row, col );
    }
}
The next example, pretty much shows the same approach with a table's data model.

package ee.tablecell;

import java.awt.Point;
import java.util.*;
import javax.swing.table.DefaultTableModel;

@SuppressWarnings("serial")
public class MyDataModel extends DefaultTableModel {
    Set<Point> offCells = new HashSet<Point>();
   
    // Disable specific cell element by adding Point to disabled set.
    public void disableCellEdit( int row, int col ) {
        offCells.add( new Point( row, col ) );
    }
   
    // Enable specific cell element by removing Point from disabled set, if it exists.
    public void enableCellEdit( int row, int col ) {
        offCells.remove( new Point( row, col ) );
    }
   
    // Check disabled set. If Point exists, return false, otherwise return default value.
    @Override
    public boolean isCellEditable( int row, int col ) {
        if ( offCells.contains( new Point( row, col ) ) )
            return false;
        else
            return super.isCellEditable( row, col );
    }
}
The following uses the above data model in a same table.

  DefaultTableModel model = new MyDataModel();
   JTable table = new JTable( model );
The above samples use a Set to maintain a distinct set of points representing each of the disabled cells. The manner in which you track this will depending on your application. For example, if you only disable one cell at a time (or conversely, enable one cell at a time), the row/col indices could just be stored as attributed of the table or data model.

Best regards,

Frelling
0
 
sidwelleAuthor Commented:
Frelling,  the grid is in Adobe Flex, but the structure and syntax is very close to Java.
Being new to Flex, I don't know how hard it would be to convert.



0
 
FrellingCommented:
Dear sidwelle:

    My apologies, I should have read the title of the question more carefully. In Flex, the easiest way to deal with disabling/enabling cell edits is to use the itemEditBeginning attribute. For example

<mx:DataGrid ... itemEditBeginning="checkEditStatus(event)" ... >
    ... more data grid stuff
</mx:DataGrid>
Then, include the following function, which simple checks for row 3 and column 4.

private function checkEditStatus(event:DataGridEvent) : void {
    if ( event.rowIndex == 3 && event.columnIndex == 4 ) {
        event.preventDefault();
    }
}
You can use the event.rowIndex and event.columnIndex attributes of the event record to examine the row and column indices. If a selected cell is disabled, simply call event.preventDefault() to cancel the edit event. As before, the manner in which you store which cells are disabled depends on your application. Here is the link to the DataGridEvent definition and its parent Event definition.

Best regards,

Frelling
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sidwelleAuthor Commented:
I get an error on the first line of the function "checkEditStatus":
1046: Type was not found or was not a compile-time constant: DataGridEvent.

I tried adding: import mx.controls.DataGrid;              but it didn't help.
0
 
zzynxSoftware engineerCommented:
>> I tried adding: import mx.controls.DataGrid;              but it didn't help.

That should have been

import mx.events.DataGridEvent;

0
 
zzynxSoftware engineerCommented:
>> Then, include the following function, which simple checks for row 3 and column 4.

Correction:
The example posted

>> private function checkEditStatus(event:DataGridEvent) : void {
>>     if ( event.rowIndex == 3 && event.columnIndex == 4 ) {
>>         event.preventDefault();
>>     }
>> }

checks for row 4 and column 5
0
 
sidwelleAuthor Commented:
Thanks
0
 
zzynxSoftware engineerCommented:
thanx 4 axxepting
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now