30
Sep
07

Using a Flex TextArea control as a drop-in item editor

User “Fred” asked a question the other day about using the TextArea as an item editor in an editable, variable line height DataGrid control.

The following example shows how you can use the Flex TextArea control as a drop-in item editor to allow you to easily edit the items in a DataGrid control.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/09/30/using-a-flex-textarea-control-as-a-drop-in-item-editor/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:XML id="itemsXML" source="data/items.xml" />

    <mx:DataGrid id="dataGrid"
            dataProvider="{itemsXML.item}"
            variableRowHeight="true"
            editable="true"
            width="100%"
            height="100%">
        <mx:columns>
            <mx:DataGridColumn id="idColumn"
                    dataField="@id"
                    editable="false"
                    headerText="ID" />
            <mx:DataGridColumn id="descColumnTextInput"
                    dataField="@desc"
                    editable="true"
                    wordWrap="true"
                    headerText="Desc (TextInput)" />
            <mx:DataGridColumn id="descColumnTextArea"
                    dataField="@desc"
                    editable="true"
                    wordWrap="true"
                    itemEditor="mx.controls.TextArea"
                    editorUsesEnterKey="true"
                    headerText="Desc (TextArea)" />
        </mx:columns>
    </mx:DataGrid>

</mx:Application>

items.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/09/30/using-a-flex-textarea-control-as-a-drop-in-item-editor/ -->
<items>
    <item id="1" desc="The quick brown fox jumped over the lazy dog." />
    <item id="2" desc="Lorem ipsum dolor sit amet, consectetuer adipiscing elit." />
    <item id="3" desc="Pellentesque nonummy aliquet metus." />
    <item id="4" desc="Nullam enim." />
    <item id="5" desc="Nullam sollicitudin sodales diam." />
    <item id="6" desc="Praesent quis tellus vel erat tristique placerat." />
    <item id="7" desc="Sed egestas, enim a convallis auctor, nisl tellus tincidunt nibh, vitae tempus mauris risus at arcu." />
    <item id="8" desc="Suspendisse laoreet sem eget ipsum porta consequat." />
    <item id="9" desc="Ut imperdiet felis quis orci." />
    <item id="10" desc="Phasellus tempus ante eu nisl." />
    <item id="11" desc="Donec velit sem, rutrum ac, bibendum sed, mattis ac, odio." />
    <item id="12" desc="Fusce mauris turpis, vehicula nec, mattis et, dapibus quis, arcu." />
    <item id="13" desc="Morbi tincidunt volutpat justo." />
    <item id="14" desc="Nam urna." />
    <item id="15" desc="Mauris est dui, aliquet at, venenatis tempus, eleifend a, pede." />
    <item id="16" desc="Vestibulum porttitor arcu sit amet nulla." />
    <item id="17" desc="Nulla vitae sapien." />
</items>

View source is enabled in the following example.


8 Responses to “Using a Flex TextArea control as a drop-in item editor”


  1. 1 Jed Wood Oct 1st, 2007 at 3:09 am

    Who would have thought it would be so easy? :)

    I actually like the effect the TextArea has of not selecting all the text and putting focus at the end of the line. So I’d consider using the TextArea even for a single line of text.

  2. 2 peterd Oct 1st, 2007 at 5:13 am

    FlexExamples.com, now with Spam Karma!

  3. 3 Jana Oct 31st, 2007 at 9:27 am

    Peter,

    I was interested in your example but the thing I can’t seem to figure out is how to control where the newlines go in a the textarea. I have a list of items within the cell of a row that I want to display.

    Jana

  4. 4 peterd Oct 31st, 2007 at 6:14 pm

    Jana,

    Maybe try using the <br> tag, or “\n”.

    Peter

  5. 5 javlae Jan 29th, 2008 at 1:31 pm

    This is nice, thanks for the example. But the interesting part would be to save the new entered/edited data into a database. How can this be done?

  6. 6 peterd Jan 29th, 2008 at 10:09 pm

    javlae,

    You would need to call a server-side script (ColdFusion, PHP, Java, ASP, whatever) that communicates with the database. So basically whenever you want to update the database with the new values, you would serialize and data from Flex to your server script which would loop over each item and update the database using some simple SQL.

    Hope that helps,
    Peter

  7. 7 Bill Shirley Mar 31st, 2008 at 9:03 am

    Do you know how I can allow the user to enter a new line in the text value?

  8. 8 peterd Mar 31st, 2008 at 3:45 pm

    Bill Shirley,

    In the TextArea item editor, they should be able to hit the Enter key to add a newline.
    In the items.xml file, you can enter newlines by adding \n to the strings.

    Peter

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".