17
Dec
07

Setting selectable ranges in the Flex DateField control

The following example shows the various usages of the selectableRange property for the DateField control in Flex.

Full code after the jump.

The first usage only sets the rangeStart value within the selectableRange property Object. This allows you to have a calendar which allows the user to only select dates after the specified date. Similarly, the second usage only sets the rangeEnd value within the selectableRange property Object, which only allows the user to select dates before the specified date. Finally, the third usage sets both the rangeStart and rangeEnd values within the selectableRange property Object which allows you to limit the selectable dates to a certain range.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/12/17/setting-selectable-ranges-in-the-flex-datefield-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">

    <mx:Form>
        <mx:FormItem label="rangeStart only:">
            <mx:DateField id="dateField1"
                    showToday="false"
                    selectableRange="{{rangeStart:new Date()}}" />
        </mx:FormItem>
        <mx:FormItem label="rangeEnd only:">
            <mx:DateField id="dateField2"
                    showToday="false"
                    selectableRange="{{rangeEnd:new Date()}}" />
        </mx:FormItem>
        <mx:FormItem label="rangeStart/rangeEnd:">
            <mx:DateField id="dateField3"
                    showToday="false"
                    selectableRange="{{rangeStart:new Date(2007, 11, 3),
                                      rangeEnd:new Date(2007,11,28)}}" />
        </mx:FormItem>
    </mx:Form>

</mx:Application>

View source is enabled in the following example.


5 Responses to “Setting selectable ranges in the Flex DateField control”


  1. 1 Raul Riera Dec 17th, 2007 at 10:36 pm

    Can this be set where the second datefield will only accept dates higher than the date selected in the first one? Does it support bindable data in that object you pass to the component?

  2. 2 ysbostan Feb 15th, 2008 at 9:31 pm

    Nice! very thanx

  3. 3 News Apr 5th, 2008 at 11:42 pm

    peterd - It’s helpfull, thanks

  4. 4 Nate May 21st, 2008 at 1:24 pm

    Is there a way to dynamically assign the rangeStart and rangeEnd? For instance, I’m using the DateField as the itemEditor in a DataGridColumn, and I need to be able to assign a selectableRange that isn’t hard coded.

  5. 5 Nate May 23rd, 2008 at 10:25 am

    Okay, I figured out how to assign a selectableRange that isn’t hard coded to a DateField that is used as an itemRenderer. The key piece I was missing was to use outerDocument to reference the object that would be used as the range:

    <mx:Script>
    <![CDATA[
    private var dateRange:Object = new Object();
    dateRange["rangeStart"] = startDate;
    dateRange["rangeEnd"] = endDate;
    ]]>
    </mx:Script>
    
    ...
    
    <mx:DataGrid>
      <mx:columns>
        <mx:DataGridColumn>
          <mx:itemRenderer>
            <mx:Component>
              <mx:DateField selectableRange="{outerDocument.dateRange}" />
            </mx:Component>
          </mx:itemRenderer>
        </mx:DataGridColumn>
      </mx:columns>
    </mx:DataGrid>
    

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;".