3 Replies Latest reply on Oct 13, 2006 9:41 AM by dimival

    DateFormatter and a DataGrid

    KomputerMan.com Level 1
      A little confuised here. I have a DateFormatter function that I want to work with a datagrid. I can use this function in other areas of the template without any problems. I just can't get it to format the data in the datagrid. So far this is what I have:

      // the action script that calls the formatter.
      public function formatDate(date:Date):String
      {
      if(date!=null)
      {
      return KDLDateFormat.format(date);
      }else{
      return '';
      }
      }

      <!-- My format string -->
      <mx:DateFormatter id="KDLDateFormat" formatString="DD-MMM-YYYY" />

      <!-- The data grid itself. -->
      <mx:DataGrid id="dependantGrid" width="460" left="10" top="10" height="155" dataProvider="{myDepData_Obj}" >

      <!-- The column I want formatted -->
      <mx:DataGridColumn dataField="Dep_DOB" headerText="Date Of Birth" labelFunction="{formatDate()}" />


      This is the error message I receive when I try to run the data grid. By the way... If I take out the labelfunction I get the correct datetimestamp value in the datagrid.

      ArgumentError: Error #1063: Argument count mismatch on components::DepInfo/formatDate(). Expected 1, got 2.
      at mx.controls.dataGridClasses::DataGridColumn/itemToLabel()
      at mx.controls::DataGrid/::setupRendererFromData()
      at mx.controls::DataGrid/mx.controls:DataGrid::commitProperties()
      at mx.core::UIComponent/validateProperties()
      at mx.managers::LayoutManager/::validateProperties()
      at mx.managers::LayoutManager/::doPhasedInstantiation()
      at Function/ http://adobe.com/AS3/2006/builtin::apply()
      at mx.core::UIComponent/::callLaterDispatcher2()
      at mx.core::UIComponent/::callLaterDispatcher()

      I tried to use this in place of the column and it worked to a point. I want the date formatted as DD-MMM-YYYY and this gave me DD/MM/YYYY, but it did work.

      <mx:DataGridColumn dataField="Dep_DOB" headerText="DOB1">
      <mx:itemRenderer>
      <mx:Component>
      <mx:Text text="{(data.Dep_DOB.getMonth()+1)+'/'+(data.Dep_DOB.getDate())+'/'+data.Dep_DOB.getFullY ear()}"/>
      </mx:Component>
      </mx:itemRenderer>
      </mx:DataGridColumn>


      Thanks in advance for the help!!!

      KomputerMan ~|:-)

        • 1. Re: DateFormatter and a DataGrid
          dimival Level 1
          The problem is in the function the dataGridColumn's labelFunction is referring to, the signature is wrong. A labelFunction's function should have this signature:

          labelFunction(item:Object, column:DataGridColumn):String

          so yours will be like this:
          public function formatDate(item:Object, column:DataGridColumn):String
          {
          if(date!=null) {
          return KDLDateFormat.format(item.Dep_DOB);
          }
          else {
          return '';
          }
          }

          You also need to remove the dataField from your DataGridColumn definition, you can't have a labelFunction and a dataField together, cause both determine the content of the column, you should only use one. Finally when using the labelFunction you don't use {} or (), just the name of the function. It should be like this:

          <mx:DataGridColumn headerText="Date Of Birth" labelFunction="formatDate" />

          This should work, tell me if something goes wrong
          • 2. Re: DateFormatter and a DataGrid
            KomputerMan.com Level 1
            That was it. It also explained the error message a bit better to me. a BIG THANKS for the help... Oh and to anybody else who is doing what I just did,,. The line

            if(date!=null) {

            in the action script should have read

            if(item.Dep_DOB!=null) {

            But that is pretty easy to figure out... even for me!!! :)

            Have an Ordinary Day...
            KomputerMan ~|:-)
            • 3. Re: DateFormatter and a DataGrid
              dimival Level 1
              Dont forget to mark my post as the correct answer :P