miunsi63 / smartgwt

Automatically exported from code.google.com/p/smartgwt
0 stars 0 forks source link

ListGrid is created too early when calling getField(String/int), getFields(), and getAllFields() #331

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,

Summary :
Calling getField(), getFields() or getAllFields() on a ListGrid creates the
ListGrid too early.

What steps will reproduce the problem?
(I also describe the "use case" for why this is a problem)
1. Obtain a ListGrid instance with pre-configured properties and fields.
This instance could come from a Factory method or it could be a ListGrid
subclass that configures those properties/fields in its constructor.
(The idea is that you want to re-use this ListGrid in different parts of
your application, and customize it depending on where you show it).
2. Call getField(String fieldName), getField(int colNum), getFields(), or
getAllFields() on the ListGrid, because you want to modify the options of
some fields to further customize the "default" grid (returned by your
factory method) for your use case.
3. Try to customize some other ListGrid parameters, such as
setInitialCriteria(myCustomInitialCriteria).

What is the expected output? What do you see instead?
I expect to be able to fully customize the fields / parameters of the grid,
and *then* create it / draw it.
Instead, I get an error, because calling getField(String/int), getFields(),
getAllFields() seems to *create* the ListGrid. Since the ListGrid is now
created, the setters that do not "allowPostCreate" do not work anymore.

I do not expect getField(int/String), getAllFields(), getFields() to create
the ListGrid too early.

What version of the product are you using? On what operating system?
SmartGwt r760
Ubuntu 8.10

Please provide any additional information below.
Here is the onModuleLoad() that demonstrates the problem:

    public void onModuleLoad() {
        SmartClientUtils.showDeveloperConsoleIfHostedMode();

        final ListGrid countryGrid = new ListGrid();
        countryGrid.toString();
        countryGrid.setWidth(400);
        countryGrid.setHeight(300);
        countryGrid.setAlternateRecordStyles(true);
        countryGrid.setShowAllRecords(true);

        ListGridField countryCodeField = new ListGridField("countryCode",
"Code", 40);
        ListGridField nameField = new ListGridField("countryName",
"Country", 120);
        ListGridField capitalField = new ListGridField("capital", "Capital");

        countryGrid.setFields(countryCodeField, nameField, capitalField);
        countryGrid.setCanResizeFields(true);
        countryGrid.setData(createListGridRecords(countryRecords));

        SC.logWarn("Is grid created before calling getAllFields()? - " +
countryGrid.isCreated());

        countryGrid.getAllFields();

        // Same behavior if calling:
        // countryGrid.getField("capital");
        // countryGrid.getFields();
        // countryGrid.getField(0);

        SC.logWarn("Is grid created after calling getAllFields()? - " +
countryGrid.isCreated());

        countryGrid.draw();
    }

    private static final String[] countryRecords = new String[] {
"countryCode;countryName;capital",
            "US;United States;Washington, DC", "CH;China;Beijing",
"JA;Japan;Tokyo", "IN;India;New Delhi",
            "GM;Germany;Berlin", "UK;United Kingdom;London",
"FR;France;Paris", "IT;Italy;Rome", "RS;Russia;Moscow",
            "BR;Brazil;Brasilia", "CA;Canada;Ottawa", "MX;Mexico;Mexico
(Distrito Federal)", "SP;Spain;Madrid",
            "KS;South Korea;Seoul", "ID;Indonesia;Jakarta" };

    public ListGridRecord[] createListGridRecords(String[] records) {
        ListGridRecord[] result = new ListGridRecord[records.length - 1];
        String[] fieldNames = records[0].split(";");
        for (int recordIndex = 1; recordIndex < records.length;
++recordIndex) {
            String[] fieldValues = records[recordIndex].split(";");
            result[recordIndex - 1] = new ListGridRecord();
            for (int fieldIndex = 0; fieldIndex < fieldValues.length;
++fieldIndex) {
                result[recordIndex -
1].setAttribute(fieldNames[fieldIndex], fieldValues[fieldIndex]);
            }
        }
        return result;
    }

Do not hesitate to ask if you have any question regarding the problem / my
use case :)

Regards,
-Etienne

Original issue reported on code.google.com by nev...@gmail.com on 25 Sep 2009 at 12:06

GoogleCodeExporter commented 9 years ago
The reason that getting fields from a ListGrid forces creation is because of 
how ListGrids can derive their fields from DataSources.

To implement a use case like this we would recommend using a DataSource to 
capture the base fields, rather than a factory method that returns a ListGrid.  

Even if you continue to take your current approach, you can easily work around 
the automatic creation by having the factory return a set of ListGridFields and 
a ListGrid which holds other non-field-related settings, instead of a ListGrid 
instance that has been given fields.

Re-categorizing as a (very) low priority enhancement to avoid this automatic 
creation when DataSources are not being used.

Original comment by smartgwt...@gmail.com on 18 Apr 2012 at 1:03