7 Replies Latest reply on Feb 7, 2012 9:15 AM by bre_an

    Iterate through itemRenderer in Flex DataGrid

    bre_an

      Hi,

       

      I have an ItemRenderer for my first DataGrid column of a message board containing a spark CheckBox.

      I want to iterate through all these itemRenderer and set the Checkbox to true (when the user clicks on "check all messages")

      In Flex 3 with a mx-DataGrid this was possible using dataGrid.getListItems(), is there a counterpart for spark?

       

      Andreas

        • 1. Re: Iterate through itemRenderer in Flex DataGrid
          andrerdp

          Hi bre_an

           

          i create headerRenderer in my column to select all values.

           

          ex:

           

          <s:Datagrid >

           

               <s:columns>

                    <s:ArraList>

                         <s:GridColumn dataField="select" headerRenderer=GridCheckHeader sortable=false

                                                itemRenderer=CheckGridColumn />

                    </s:ArraList>

               </s:columns>

           

          </s:DataGrid>

           

          inside my GridCheckHeader i change layout HGroup from default headerRenderer

           

          <s:Hgroup>

           

                <s:CheckBox id="checkAll" click="checkAll_click(event)">

          ....           

           

          </s:Hgroup>

           

           

          protected function checkAll_click(event:mouseEvent):void{

           

               for(var obj:Object in grid.dataProvider){

           

                    grid.dataProvider[obj][column.dataField] = true;

               }

          }

          • 2. Re: Iterate through itemRenderer in Flex DataGrid
            bre_an Level 1

            Hi andrerdp,

             

            thank you very much for your response, but that is not quite what i meant.

            I have an itemrenderer containing a checkbox:

            <s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                xmlns:s="library://ns.adobe.com/flex/spark" 
                focusEnabled="true">
                <fx:Script>
                <s:Group width="100%" >
                    <s:CheckBox id="chk"
                        horizontalCenter="0" verticalCenter="0" />
                </s:Group>
            </s:GridItemRenderer>
            

             

            and now I want to check all checkboxes for evey row.

            this is for a messaging system, where the user can select messages to delete them (or click the button "select all" to check all messages).

            The model for the message data does not have a chk-value, and I think it is clearer to keep the model that way, but I can modify it so that it stores the value if a message is selected if it is not possible to iterate through the itemRenderer.

             

            Andreas

            • 3. Re: Iterate through itemRenderer in Flex DataGrid
              bre_an Level 1

              anyway... extending my data model works:

              class Message {
                 public var chk:Boolean = false;
              ...
              }
              

               

              than I can call this function on click:

              private function markAll():void { 
                  for each (var obj:Object in dataGrid.dataProvider){
                      obj.chk = true;
                  }
                  (dataGrid.dataProvider as ArrayCollection).refresh();
              }
              

               

              and this is how the ItemRenderer looks like:

              <s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                  xmlns:s="library://ns.adobe.com/flex/spark" >
                  <fx:Script>
                  <![CDATA[
                          public override function set data(value:Object):void {
                              super.data = value
                              if (value == null)
                                  chk.selected = false
                              else
                                  chk.selected = value.chk as Boolean;
                          }
              
                          override public function setFocus():void {
                              chk.setFocus();
                          }
                  ]]>
                  </fx:Script>
                      <s:CheckBox id="chk"
                          horizontalCenter="0" verticalCenter="0" />
              </s:GridItemRenderer>
              

              as i wrote, not the nicest way, but it works... so if anyone knows a nicer way without modifying the model please tell me.

              • 4. Re: Iterate through itemRenderer in Flex DataGrid
                drkstr_1 Level 4

                as i wrote, not the nicest way, but it works... so if anyone knows a nicer way without modifying the model please tell me.


                While your method requires more typing than others, I think most (including myself) would consider it the preferred method for updating an item renderer view.

                • 5. Re: Iterate through itemRenderer in Flex DataGrid
                  bre_an Level 1

                  It is not the lines of code i meant, for this I have to extend my data model.

                  In my case the original model is generated from my database model which does not need to know anything about the checkboxes.

                  So I had to modify my model generator (that creates the code from the database model into actionscript code) for this to add a variable that does not exist as database-column.

                  • 6. Re: Iterate through itemRenderer in Flex DataGrid
                    drkstr_1 Level 4

                    This is what's called a Presentation Model (PM). Usually I encapsulate my domain objects in a PM, or use some kind of controller to handle the translation between domain objects and the PMs. Extending the domain object is also an option, but I'm a bit wary of extension as a practice, in most instances.

                    • 7. Re: Iterate through itemRenderer in Flex DataGrid
                      bre_an Level 1

                      ah, ok, I'll create a pm for my data than.

                      Thank you for your response! I definitively learned something here!