Welcome!

Adobe Flex Authors: Matthew Lobas, PR.com Newswire, Shelly Palmer, Kevin Benedict

Related Topics: Adobe Flex, ColdFusion

Adobe Flex: Article

Adobe Flex 2: Advanced DataGrid

Part 1: Destination-awareness formatters & renderers

More on Customizing the DataGridColumn
We'll put our DataGridColumn in the dataGridClasses subfolder as a sign of our respect for the well-thought-out directory structure of the Flex framework:

As mentioned above, the "dirty" job of locating and assigning the proper label function has been delegated to the helper class FormattingManager. This class, presented in Listing 4, should be put in our theriabook project.

Tada! And the winner is...the developer. Once we add theriabook.swc (with DataGrid, DataGridColumn, and FormattingManager) to the library path, the application code gets reduced to Listing 5.

Improving FormattingManager
In the previous examples we've used SwitchSymbolFormatter for both phone and ssn formatting. As soon as we start formatting numbers or currency values, it's natural to use NumberFormatter or CurrencyFormatter - descendants of mx.formatters.Formatter. In fact, Flex offers a dedicated formatter even for the phone formatting.

While SwitchSymbolFormatter derives from Object, all the rest of the formatters descend from Formatter. By encapsulating this specific of SwitchSymbolFormatter in the custom class MaskFormatter we'll help ourselves in basing the next version of FormattingManager entirely on Formatters as in Listing 6.

Look how this MaskFormatter simplifies our FormattingManager: we can replace all the private methods with an anonymous function, as shown in Listing 7. Please note that the reference to the appropriate formatter is preserved with the closure.

The testing application FormatStringDemo is in Listing 8. If you run it, the DataGrid dg will be formatted as shown in Figure 4:

Let's focus on the hard-coding that we allowed in case of the money value:

     case "money":
        formatter = new CurrencyFormatter();
        CurrencyFormatter(formatter).precision=2;
        break;

This hard-coding reflects, perhaps, the most "popular" case. But what if we want to have the full advantage of the properties of the corresponding formatter, such as precision, in case of CurrencyFormatter? To address these cases we're going to introduce one more fx:DataGridColumn property - formatData. Here's how it will be used in the application MXML:

    <fx:DataGridColumn dataField="SALARY" >
      <fx:formatData>
        <mx:Object formatString="money" precision="0"/>
      </fx:formatData>
    </fx:DataGridColumn>

The elegance of MXML lets us implement this extension with just a few lines of extra code in com.theriabook.controls.dataGridClasses.DataGridColumn:

    public function set formatData(fd :Object) : void{
      FormattingManager.setFormat(this, fd);
    }

Then, to accommodate the change on the FormattingManager side, we'll iterate through all the properties of the formatData object and attempt to assign them to the appropriate properties of the formatter with an emphasis on the word appropriate. The MXML compiler isn't going to help us check the properties of the unsealed <mx:Object> against the properties of the formatter. Accordingly, to protect ourselves from the no-such-property-exceptions, we surround property assignments with try/catch:

    public static function setFormat(
        dgc:mx.controls.dataGridClasses.DataGridColumn,         formatData:Object
    ):void {
      . . . . .
      if (!(formatData is String)) {
        for (var property:String in formatData) {
        try {
            formatter[property] = formatData[property];
          } catch (err:Error) {
            // Property does not match formatter type
          }
        }
      }
      . . . . .
    }

The complete listing of renewed FormattingManager is in Listing 9. While maintaining your own framework, you would transform this class to accommodate your requirements

Finally, Listing 10 has the sample application to test our changes, FormatDataDemo.

When you run the application it will produce the DataGrid shown in Figure 5.

We'll continue beefing up our custom DataGridColumn after a short detour into CheckBox and RadioButton controls.

CheckBox as a Drop-In Renderer
As we warned the reader at the beginning of this article, DataGrids rarely come alone. In this section we're going to suggest customizimg the CheckBox, which will help us illustrate custom DataGridColumns from a different perspective.

The state of a CheckBox control is managed by the Boolean property selected. At the same time, many business systems use either Y/N or, sometimes, 0/1 flags. As a result, translating business-specific values into selected and vice versa burdens the application code. Listing 11 presents the custom CheckBox, which supports application-specific on and off values along with the current value.

So, using this CheckBox, we could have written:

<fx:CheckBox value="Y" onValue="Y" offValue="N" />

to have selected CheckBox, or

<fx:CheckBox value="N" onValue="Y" offValue="N" />

to set selected to false.

DataGridColumn as ItemRenderer's Knowledge Base
Now let's get back to the DataGrid world. What if we wanted to use our CheckBox as the DataGrid item renderer? Here's the suggested use case example:

<fx:DataGridColumn dataField="BENE_DAY_CARE"
       itemRenderer="com.theriabook.controls.CheckBox" >
    </fx:DataGridColumn>

Obviously, we need to modify the CheckBox more to take care of the value within the data setter:

    override public function set data(item:Object):void
    {
      super.data = item;
      if( item!=null ) {
        value = item[DataGridListData(listData).dataField];
      }
    }

But how will we communicate the offValue and onValue properties to our CheckBox-turned-itemRenderer? Ideally, we would need something like:

    <fx:DataGridColumn dataField="BENE_DAY_CARE"
    itemRenderer="com.theriabook.controls.CheckBox" >
      <fx:extendedProperties>
        <mx:Object onValue="Y" offValue="N" />
      </fx:extendedProperties>
    </fx:DataGridColumn>


More Stories By Victor Rasputnis

Dr. Victor Rasputnis is a Managing Principal of Farata Systems. He's responsible for providing architectural design, implementation management and mentoring to companies migrating to XML Internet technologies. He holds a PhD in computer science from the Moscow Institute of Robotics. You can reach him at [email protected]

More Stories By Yakov Fain

Yakov Fain is a Java Champion and a co-founder of the IT consultancy Farata Systems and the product company SuranceBay. He wrote a thousand blogs (http://yakovfain.com) and several books about software development. Yakov authored and co-authored such books as "Angular 2 Development with TypeScript", "Java 24-Hour Trainer", and "Enterprise Web Development". His Twitter tag is @yfain

More Stories By Anatole Tartakovsky

Anatole Tartakovsky is a Managing Principal of Farata Systems. He's responsible for creation of frameworks and reusable components. Anatole authored number of books and articles on AJAX, XML, Internet and client-server technologies. He holds an MS in mathematics. You can reach him at [email protected]

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


IoT & Smart Cities Stories
Dion Hinchcliffe is an internationally recognized digital expert, bestselling book author, frequent keynote speaker, analyst, futurist, and transformation expert based in Washington, DC. He is currently Chief Strategy Officer at the industry-leading digital strategy and online community solutions firm, 7Summits.
Digital Transformation is much more than a buzzword. The radical shift to digital mechanisms for almost every process is evident across all industries and verticals. This is often especially true in financial services, where the legacy environment is many times unable to keep up with the rapidly shifting demands of the consumer. The constant pressure to provide complete, omnichannel delivery of customer-facing solutions to meet both regulatory and customer demands is putting enormous pressure on...
IoT is rapidly becoming mainstream as more and more investments are made into the platforms and technology. As this movement continues to expand and gain momentum it creates a massive wall of noise that can be difficult to sift through. Unfortunately, this inevitably makes IoT less approachable for people to get started with and can hamper efforts to integrate this key technology into your own portfolio. There are so many connected products already in place today with many hundreds more on the h...
The standardization of container runtimes and images has sparked the creation of an almost overwhelming number of new open source projects that build on and otherwise work with these specifications. Of course, there's Kubernetes, which orchestrates and manages collections of containers. It was one of the first and best-known examples of projects that make containers truly useful for production use. However, more recently, the container ecosystem has truly exploded. A service mesh like Istio addr...
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As automation and artificial intelligence (AI) power solution development and delivery, many businesses need to build backend cloud capabilities. Well-poised organizations, marketing smart devices with AI and BlockChain capabilities prepare to refine compliance and regulatory capabilities in 2018. Volumes of health, financial, technical and privacy data, along with tightening compliance requirements by...
Charles Araujo is an industry analyst, internationally recognized authority on the Digital Enterprise and author of The Quantum Age of IT: Why Everything You Know About IT is About to Change. As Principal Analyst with Intellyx, he writes, speaks and advises organizations on how to navigate through this time of disruption. He is also the founder of The Institute for Digital Transformation and a sought after keynote speaker. He has been a regular contributor to both InformationWeek and CIO Insight...
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
To Really Work for Enterprises, MultiCloud Adoption Requires Far Better and Inclusive Cloud Monitoring and Cost Management … But How? Overwhelmingly, even as enterprises have adopted cloud computing and are expanding to multi-cloud computing, IT leaders remain concerned about how to monitor, manage and control costs across hybrid and multi-cloud deployments. It’s clear that traditional IT monitoring and management approaches, designed after all for on-premises data centers, are falling short in ...
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, discussed how Dice leverages data insights and tools to help both tech professionals and recruiters better understand how skills relate to each other and which skills are in high demand using interactive visualizations and salary indicator tools to maximize earning potential. Manish Dixit is VP of Product and Engineering at Dice. As the leader of the Product, Engineering and Data Sciences team at D...
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...