3.2.0

External sort model?

I'd like to define a custom sort function for certain columns. Is that possible? Is that what the external sort model is for?
Kevin
August 25,
Sorting is controlled by AW.Formats classes. The following example shows sorting with the Number formatter vs the default string formatter. Also shows a custom formatter which sorts by string lengths.
<html>
<head>
    <title></title>
    <style>body {font: 12px Tahoma}</style>
    <script src="../../runtime/lib/aw.js"></script>
    <link href="../../runtime/styles/xp/aw.css" rel="stylesheet"></link>
<style>
    #myGrid .aw-column-0 {width: 100px;}
    #myGrid .aw-column-1 {width: 150px}
</style>
</head>
<body>
    <span id="myGrid"></span>
<script>
var myData = [
    ["1",		"1",    "zd"],
    ["12",		"12",   "yyy"],
    ["23",		"23",   "ggggg"],
    ["100",		"100",  "555t"]
];
// custom sort to compare string lengths
StringLengthSort = AW.Formats.String.subclass();
StringLengthSort.create = function() {
    var obj = this.prototype;
    obj.comparator = function(values, greater, less, equal, error){
        return function(i, j){
            var v1 = values[i];
            if (v1 == null) {
                v1 = "";
            }
            var v2 = values[j];
            if (v2 == null) {
                v2 = "";
            }
            if (v1.length > v2.length) {
                return greater * -1;
            }
            else if (v1.length < v2.length) {
                return greater * 1;
            }
            return 0;
        }
    };
}
var myHeaders = ["string", "number", "string length"];
var grid = new AW.UI.Grid;
grid.setId("myGrid");
grid.setHeaderText(myHeaders);
grid.setCellData(myData);
grid.setCellFormat(new AW.Formats.Number, 1);
grid.setCellFormat(new StringLengthSort, 2);
grid.setColumnCount(3);
grid.setRowCount(4);
grid.refresh();
</script>
</body>
</html>
Bryn
August 26,
Great, I'll give that a shot. Thanks.
Kevin
August 26,

This topic is archived.

See also:


Back to support forum