3.2.0

Refresh an XML-backed grid *without* losing sort/row selection/etc?

The response() method of AW.XML.Table clears a bunch of grid models -- sort order, row selection, etc. How would I go about implemeting an override that *doesn't* do this? I want to refresh the underlying data while preserving the sort order and row selection. It's easy enough to override the method with one that just doesn't clear the models, but I imagine they're being cleared for a reason -- what do I need to handle differently?

The code below exhibits the current behavior; it's just the "xml - simple.htm" sample included with the distribution, with the addition of a button which refreshes the table data.

<html>
<head>
    <title>ActiveWidgets Grid :: Examples</title>
    <style> body, html {margin:0px; padding: 0px; overflow: hidden;} </style>

    <!-- ActiveWidgets stylesheet and scripts -->
    <link href="../../runtime/styles/xp/aw.css" rel="stylesheet" type="text/css" ></link>
    <script src="../../runtime/lib/aw.js"></script>

    <!-- grid format -->
    <style>
        .aw-grid-control {height: 80%; width: 100%; border: none; font: menu;}

        .aw-column-0 {width:  80px;}
        .aw-column-1 {width: 200px; background-color: threedlightshadow;}
        .aw-column-2 {text-align: right;}
        .aw-column-3 {text-align: right;}
        .aw-column-4 {text-align: right;}

        .aw-grid-cell {border-right: 1px solid threedshadow;}
        .aw-grid-row {border-bottom: 1px solid threedlightshadow;}
    </style>
</head>
<body>
    <script>

    //	create ActiveWidgets data model - XML-based table
    var table = new AW.XML.Table;

    //	provide data URL
    table.setURL("../data/companies-simple.xml");

    //	start asyncronous data retrieval
    table.request();

    //	define column labels
    var columns = ["Ticker", "Company Name", "Market Cap.", "$ Sales", "Employees"];

    //	create ActiveWidgets Grid javascript object
    var obj = new AW.UI.Grid;

    obj.setColumnCount(5);

    //	provide column labels
    obj.setHeaderText(columns);

    //	enable row selectors
    obj.setSelectorVisible(true);
    obj.setSelectorText(function(i){return this.getRowPosition(i)});
    obj.setSelectorWidth(25);

    //	set row selection
    obj.setSelectionMode("single-row");

    //	define data formats
    var str = new AW.Formats.String;
    var num = new AW.Formats.Number;

    obj.setCellFormat([str, str, num, num, num]);

    //	provide external model as a grid data source
    obj.setCellModel(table);

    //	write grid html to the page
    document.write(obj);

    var button = new AW.UI.Button;
    button.setControlText("Refresh Table Data");
    button.onClick = function() {
        table.request();
    }
    document.write(button);

    </script>
</body>
</html>
June 12,
Most (?) people are calling xml.request() to load a different set of data, so for them it make sense to clear everything related to previous set, i.e. selection, sort order, etc.

In your case, when you are sending request to see if there are any changes in the same dataset - it make sense to keep all parameters, so the response method should not clear anything.

I was struggling for a while how to cover both scenarios but finally gave up and left only the first one.
Alex (ActiveWidgets)
June 13,
Thanks for the reply.

However, it appears that just leaving the models intact doesn't work. I modified the above code to override the response() method of AW.XML.Table like so:

table.response = function(xml){
        this.setXML(xml);

        if (this.$owner) {
/*
            this.$owner.clearScrollModel();
            this.$owner.clearSelectionModel();
            this.$owner.clearSortModel();
            this.$owner.clearRowModel();
*/
            this.$owner.setRowCount(this.getCount());
            this.$owner.refresh();
        }
    };


Some observations (using the sample XML data from the AW distribution):

1) Sort the grid on the Company Name column. Note that Adobe is at the top. Modify the underlying XML to change the Adobe's company name to something else (say, "zzzAdobe"). Click the "Refresh Table Data" button. Note that zzzAdobe is still at the top of the list, and the column indicates that it's sorted, even though zzzAdobe should be at the bottom.
2) Refresh the page. Note that there are 20 rows in the grid. Sort the grid on the Company Name column. Delete some of the records in the underlying XML file. Click the "Refresh Table Data" button. Note that there are now blank entries in the XML, and some items that should still be there are missing. Also note that the sort order is wrong.

I suspect this is all due to the models being out of sync with the data in the grid. How do I handle this?
Trevor
June 13,

This topic is archived.

See also:


Back to support forum