10 Replies Latest reply on May 24, 2011 2:51 PM by Zolotoj

    Programmatically activate a cell

    Zolotoj Level 3

      I have a Spark editable datagrid. A user has an ability to add a new row. I want to activate a cell for editing in the new row without clicking on a cell. Is that possible?

      Here is my code:

      var engine:XMLList = newEngine.copy();
      engines.dataProvider.addItemAt(engine, engines.dataProvider.length);
      engines.setSelectedIndex(engines.dataProvider.length - 1);     
      engines.validateNow();
      engines.startItemEditorSession(engines.dataProvider.length - 1, 0);
      

       

      I still have to click on a cell in the first column to start editing.

       

      Thanks

        • 1. Re: Programmatically activate a cell
          Flex harUI Adobe Employee

          In many browsers, the Flex app is not activated at startup.  Is this problem

          happening if you've already clicked somewhere in the app?

          • 2. Re: Programmatically activate a cell
            Zolotoj Level 3

            O, I have clicked already, more then once before I click on a header to add a new row.

            • 3. Re: Programmatically activate a cell
              Darrell Loverin Level 4

              startItemEditorSession() should start up a cell editor without having to click on the cell.

               

              How are you invoking the startItemEditorSession() code? From a button click? Does the data grid have focus before you call startItemEditorSession()? Changes in focus can cause a cell editor to close.

               

               

               

              -Darrell

              • 4. Re: Programmatically activate a cell
                Zolotoj Level 3

                The code I have provided in my original message is the only code I have:

                 

                var engine:XMLList = newEngine.copy();
                engine.header_id = consistHeaderModel(model).id;
                engines.dataProvider.addItemAt(engine, engines.dataProvider.length);
                engines.setSelectedIndex(engines.dataProvider.length - 1);   
                engines.validateNow();
                engines.setFocus(); // Added this. No difference.
                engines.startItemEditorSession(engines.dataProvider.length - 1, 0);

                 

                I am running this when a user clicks on column's header. I have a custom header renderer for that. here is some code:

                override protected function createChildren() : void
                        {       
                            var addRow:Image = new Image();
                            addRow.buttonMode = true;
                            addRow.useHandCursor = true;
                            addRow.toolTip = toolTipStr;
                            addRow.addEventListener("click", clickHandler);
                            addRow.source = imgClass;
                            addElement(addRow);
                            validateDisplayList();
                        }
                        protected function clickHandler(event:Event):void
                        {       
                            var dynEvent:DynamicEvent = new DynamicEvent(eventToRise, true);
                            dispatchEvent(dynEvent);           
                        }

                 

                More details.

                I am using a custom itemEditor for the column:

                <s:itemEditor>
                     <fx:Component>
                     <local:gridTextEditor restrict="^0-9"/>                                   
                     </fx:Component>
                </s:itemEditor>

                 

                where gridTextEditor is the following:

                package
                {
                    import spark.components.DataGrid;
                    import spark.components.TextInput;
                    import spark.components.gridClasses.GridItemEditor;
                    import spark.components.gridClasses.IGridItemEditor;

                 

                    public class gridTextEditor extends GridItemEditor implements IGridItemEditor
                    {
                        private var valueDisplay:TextInput;
                        public var restrict:String;
                        public function gridTextEditor()
                        {
                            //TODO: implement function
                            super();
                            valueDisplay = new TextInput();
                            valueDisplay.setStyle("borderVisible", false);
                            valueDisplay.setStyle("unfocusedTextSelectionColor", "#70b2ee");
                            //valueDisplay.selectionHighlighting = "always";
                            addElement(valueDisplay);
                            valueDisplay.x += 4;
                            valueDisplay.y += 3;
                        }
                        override public function prepare():void
                        {           
                            super.prepare();
                            valueDisplay.restrict = restrict;
                            valueDisplay.width = column.width - 6;
                            valueDisplay.selectRange(0, valueDisplay.text.length);
                        }
                        override public function save():Boolean
                        {
                            data[column.dataField] = value;
                            DataGrid(owner).validateNow();
                            return true;
                        }
                        override public function get value():Object
                        {
                            return valueDisplay.text;           
                        }
                       
                        override public function set value(newValue:Object):void
                        {
                            valueDisplay.text = newValue.toString();
                        }
                    }
                }

                • 5. Re: Programmatically activate a cell
                  Darrell Loverin Level 4

                  I don't see anything obviously wrong. I'll take a look at the problem if you can post a small test case. Otherwise try tracing into the startItemEditorSession() to see why an item editor is not created.

                   

                   

                  -Darrell

                  • 6. Re: Programmatically activate a cell
                    Zolotoj Level 3

                    I have traced startItemEditorSession and it looks like it retuns editorStartted  =  true.

                    I will put together a test case.

                    • 7. Re: Programmatically activate a cell
                      Zolotoj Level 3

                      How can I send you a zip file?

                      • 8. Re: Programmatically activate a cell
                        Zolotoj Level 3

                        I think I know what's causing the problem. This code:


                        super();
                        valueDisplay = new TextInput();

                        addElement(valueDisplay);

                         

                        Adds a text box in order to control user entries. Without adding it the caret appears in the cell correctly.

                        Now a new question: How get the same functionality without the text box?

                        • 9. Re: Programmatically activate a cell
                          Darrell Loverin Level 4

                          You can email me a zip file at dloverin@adobe.com. I'm not sure if the zip file will striped off the email or not. There is a "Attach File" control in the forum's email application below where you compose a reply.

                          • 10. Re: Programmatically activate a cell
                            Zolotoj Level 3

                            I think I got it. I am extending DefaultGridItemEditor and I am not adding a text box. Instead, I am using DefaultGridItemEditor.textArea. Somehow, I overlooked it.

                             

                            Thanks for the help.