8 Replies Latest reply on Dec 22, 2009 5:12 PM by saisri2k2

    datagrid visible only if data is there

    Bhulka

      I would like to make datagrid visible only if my search is bringing any data for it..if there is no data after search query, I don't want my datagrid to be displayed as empty datagrid. Can somebody help please?

        • 1. Re: datagrid visible only if data is there
          saisri2k2 Level 4

          //After you get the result from the search  put the following check

          if(arrayCollection.length ==0 ){        // arrayCollection is the dataProvider to the dataGrid(I'm assuming thta you have array collection as data provider)

               dataGridId.visible = false;         //dataGridId is the id of the datagrid

               dataGridId.includeInLayout = false;

          }else{

          //if the length is > 0 then make them visible and at the same time assing the array collection

          if(dataGridId.visible == false){

                dataGridId.visible = true;         //dataGridId is the id of the datagrid

               dataGridId.includeInLayout = true;

          }

           

          dataGridId.dataProvider = arrayCollection;

          }

          • 2. Re: datagrid visible only if data is there
            msakrejda Level 4

            Alternately, you could bind the grid's visible and includeInLayout properties to the length of its data provider.

            • 3. Re: datagrid visible only if data is there
              Bhulka Level 1

              Its not array collection. Its XML I am getting back as search response. How should

              I check the length of XML string?

              • 4. Re: datagrid visible only if data is there
                saisri2k2 Level 4

                You could use XMLList to do that... if my XML is like the following

                 

                var xData:XML = <root>

                <one>

                     <two></two>

                     <two></two>

                     <two></two>

                </one>

                </root>

                 

                i will assign

                 

                var xmlList:XMLList = xData.one.two; // ignore the root element

                var len:int = xmlList.length();  // this wil give the length of the xml

                • 5. Re: datagrid visible only if data is there
                  Bhulka Level 1

                  Thanks buddy..this is really helping me a lot. Looks like I am very close to what I am looking for.  By the way while implementing this code, I am getting error like "TypeError: Error #1009: Cannot access a property or method of a null object reference."  this error is pointing to the particular point where I am calling length() method. What's wroing here? should I have to define anything null before using length()..please advise.

                  • 6. Re: datagrid visible only if data is there
                    saisri2k2 Level 4

                    probably the xmlList is still null.. if you can post your xml here.. even the var xmlList:XMLList  ..... line here.. i'll tell where the problem lies.

                    • 7. Re: datagrid visible only if data is there
                      Bhulka Level 1

                      when I am getting the search event result I am storing that in one XML var  like this:

                       

                      var

                       

                      _scacResult:XML;

                      _scacResult = event.result

                      as XML;

                      modelLocator.scacResult = _scacResult; // modelLocator.scacResult is accessible from other components too..

                       

                      and then in mxml where I want to control datagrid display

                       

                      I am writing a code like:

                       

                      var

                       

                      xmlList:XMLList = modelLocator.scacResult.REQ;  // (REQ is the element of XML) I am getting error at this point...

                       

                      var len:int = xmlList.length();

                       

                      Once I will get the length, I will put a condition like if length greater than 0, display grid else pop up "no data found".

                       

                      Please let me know if you need any further info.

                       

                      • 8. Re: datagrid visible only if data is there
                        saisri2k2 Level 4

                        By looking at your xml i feel that you have to put the second line

                         

                        _scacResult = event.result as XML;

                         

                         

                        as

                         

                        _scacResult = XML(event.result);

                         

                         

                        if this also does not work.. then try to debug the aplication... see what is going in _scarResult..

                         

                        i'm assuming the followin as your xml

                         

                        <root>

                        <REQ></REQ>

                        <REQ></REQ>

                        <REQ></REQ>

                        </root>

                         

                        if not the code wont work.