开发者

How do I order datagrid columns in AS3?

I'm using the following code to load XML directly into a datagrid:

import flash.events.Event;
import fl.data.DataProvider;
import flash.net.URLRequest;

var loader:URLLoader = new URLLoader();

loader.addEventListener(Event.COMPLETE, directoryXMLLoaded);

function directoryXMLLoaded(e:Event){
    var dp:DataProvider = new DataProvider(new XML(e.target.data));
    directoryGrid.dataProvider = dp;
}

loader.load(new URLRequest("xml/directory.xml"));

How can I reorder my columns based on the title?

EDIT:

My XML looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<contacts>
<contact fi开发者_JS百科rst="Moshe" last="Doe" address="555 5th St" city="Somwhere" state="AA" zip="555555" phone="555-555-5555" />
<contact first="Moshe" last="Doe" address="555 5th St" city="Somwhere" state="AA" zip="555555" phone="555-555-5555" />
<contact first="Moshe" last="Doe" address="555 5th St" city="Somwhere" state="AA" zip="555555" phone="555-555-5555" />
</contacts>

I want the columns to appear in the order of the attributes (As first, last,address, city, state, zip, phone), but they are not.

EDIT2:

TheDarklnl has the correct answer. I implemented it slightly differently though:

function directoryXMLLoaded(e:Event){
    var dp:DataProvider = new DataProvider(new XML(e.target.data));
    directoryGrid.addColumn("first");
    directoryGrid.addColumn("last");
    directoryGrid.addColumn("address");
    directoryGrid.addColumn("city");
    directoryGrid.addColumn("state");
    directoryGrid.addColumn("zip");
    directoryGrid.addColumn("phone");
    directoryGrid.dataProvider = dp;
}


you can sort the DataProvider object the same way as an Array object. so if your column titles are named "columnTitles":

dp.sortOn("columnTitles");

additionally, you can supply a second parameter for additional sorting methods with Array public constants.

Array.CASEINSENSITIVE
Array.DESCENDING
Array.NUMERIC
Array.RETURNINDEXEDARRAY
Array.UNIQUESORT

so if you wanted your columnTitles to be sorted numerically as well as being case insensitive:

dp.sortOn("columnTitles", ARRAY.CASEINSENSITIVE | ARRAY.NUMERIC)

Update

you can order your columns by setting their titles in an array of strings for the columns property of the data grid object. so if you want your columns in the same order as they are in your XML file:

directoryGrid.columns = ["first", "last", "address", "city", "zip", "phone"];

Update 2

if you would like to sort using the columns property based on the XML attributes but still be able to change the header text, for example capitalizing the titles or changing them completely ("phone" could become "Telephone Number:"), you can do so by creating a new DataGridColumn and changing its headerText property before assigning the column to your data grid object: DataGridColumn headerText property.

this is especially useful when you don't have administrative control over the XML data and don't want to transfer the XML into an array of objects that could supply your dataProvider.


Alright, here's how to programmatically sort the DataGrid by multiple columns first, second, third...

testBtn.addEventListener(MouseEvent.CLICK, testBtnClk);
function testBtnClk(e:MouseEvent):void
{
    dataGridMain.dataProvider.sort(sortOnCatDescCust);
}

function sortOnCatDescCust(a:Object, b:Object):Number {
    var aCat:String = a.Category;
    var aDesc:String = a.Description.toUpperCase();
    var aCust:String = a.Customer.toUpperCase();

    var bCat:String = b.Category;
    var bDesc:String = b.Description.toUpperCase();
    var bCust:String = b.Customer.toUpperCase();

    if(aCat > bCat)
    {
             return 1;
    }
    if(aCat < bCat)
    {
            return -1;
    }
    else
    {
        if(aDesc > bDesc)
        {
             return 1;
        }
        if(aDesc < bDesc)
        {
            return -1;
        }
        else
        {
            if(aCust > bCust){
                return 1;
            }
            if(aCust < bCust){
                return -1;
            }
            else
            {
                return 0;
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜