Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Foreign fields in listgrid and Export

    Using (SC_SNAPSHOT-2012-02-27_v8.2p/PowerEdition Deployment 2012-02-27)


    Hello, I have a foreign key in my ListGrid, which can be edited with a SelectItem with a different optionDataSource. Of course, the value field is the id of this foreign table, and the display field is `someForeignField` out of `MyForeignTable`.

    Can I get the display value to be shown in the Excel (XLS) export, instead of the value field as is used now? I tried both exportClientData and exportData on the ListGrid object.

    Thanks in advance for any help.
    Last edited by Sytematic; 3 Apr 2012, 01:19.

    #2
    Okay, with the following standalone playground app I am able to get good results using grid.exportClientData, but now (of course) limits apply. Is it possible to export EVERYTHING, but WITH client side filtering/formatting?

    Here is my playground app:
    Code:
    package nl.sytematic.projects.SmartGWTTestCase.client;
    
    import com.allen_sauer.gwt.log.client.Log;
    import com.google.gwt.core.client.EntryPoint;
    import com.google.gwt.core.client.GWT;
    import com.smartgwt.client.data.DSRequest;
    import com.smartgwt.client.data.DataSource;
    import com.smartgwt.client.data.SortSpecifier;
    import com.smartgwt.client.types.Alignment;
    import com.smartgwt.client.types.ExportDisplay;
    import com.smartgwt.client.types.ExportFormat;
    import com.smartgwt.client.types.ListGridEditEvent;
    import com.smartgwt.client.types.RowEndEditAction;
    import com.smartgwt.client.types.SortDirection;
    import com.smartgwt.client.util.EnumUtil;
    import com.smartgwt.client.widgets.IButton;
    import com.smartgwt.client.widgets.events.ClickEvent;
    import com.smartgwt.client.widgets.events.ClickHandler;
    import com.smartgwt.client.widgets.form.fields.FormItem;
    import com.smartgwt.client.widgets.form.fields.SelectItem;
    import com.smartgwt.client.widgets.grid.ListGrid;
    import com.smartgwt.client.widgets.grid.ListGridField;
    import com.smartgwt.client.widgets.layout.VLayout;
    
    /**
     * Entry point for the SocialCars application. Acts as an instantiator for the SocialCarsApplication class.
     * Also, if this application is SAAS-enabled, that is, resellable by us, it will control the instantiation of 
     * the portal and the super admin panel, to monitor how many customers we have.
     */
    public class MainEntry implements EntryPoint {
    	private VLayout mainPanel = new VLayout(5);
    
    	public void onModuleLoad() {
    		mainPanel.setWidth100();
    		mainPanel.setHeight100();
    		
    		final ListGrid grid = new ListGrid();
    		
    		grid.setAlternateRecordStyles(false);
    		grid.setCellHeight(32);
    		grid.setDataSource(DataSource.get("Test"));
    		grid.setAutoFetchData(true);
    		grid.setCanEdit(true); 
    		grid.setModalEditing(true);
    		grid.setShowFilterEditor(true);
    		grid.setCanSelectText(true);
    		grid.setCanDragSelectText(true);
    		grid.setDoubleClickDelay(30);
    		grid.setEditEvent(ListGridEditEvent.DOUBLECLICK);
    		grid.setFilterOnKeypress(true);
    		grid.setAllowFilterExpressions(true);
    		grid.setListEndEditAction(RowEndEditAction.DONE);
    		grid.setCanRemoveRecords(false); // we have our own delete button, with extra functionality   
    		grid.setAutoSaveEdits(true);
    		grid.setShowRecordComponents(true);
    		grid.setShowRecordComponentsByCell(true);
    		
    		ListGridField test = new ListGridField("test", "Test.test");
    		ListGridField test2 = new ListGridField("test2","Test2.test2"); //foreign field
    
    		final SelectItem Test2SelectItem = new SelectItem("Test2_id", "test2");
    		Test2SelectItem.setOptionDataSource(DataSource.get("Test2"));
    		Test2SelectItem.setValueField("Test2_id");
    
    		ListGrid CourseEditionListGrid = new ListGrid();
    		CourseEditionListGrid.setShowFilterEditor(true);
    
    		Test2SelectItem.setDisplayField("test2");
    
    		test2.setEditorType(Test2SelectItem);
    		test2.setOptionDataSource(DataSource.get("Test2"));
    		test2.setDisplayField("test2");
    
    		test2.setFilterEditorType(Test2SelectItem);
    		
    		grid.setFields(test, test2);
    		
    		IButton exportButton = new IButton("Export");
    	
    		exportButton.addClickHandler(new ClickHandler() {
    			public void onClick(ClickEvent event) {
    				DSRequest dsRequestProperties = new DSRequest();
    				dsRequestProperties.setExportAs(ExportFormat.XLS);
    				dsRequestProperties.setExportDisplay(ExportDisplay.DOWNLOAD);
    				grid.exportClientData(dsRequestProperties);
    			}
    		});
    		
    		mainPanel.addMember(grid);
    		mainPanel.addMember(exportButton);
    		mainPanel.show();
    	}
    }
    ds.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <DataSource ID="Test" serverType="sql" tableName="Test"> 
      <fields> 
        <field name="id" type="sequence" primaryKey="true"/>  
        <field name="test" type="text" required="false" length="128"> 
          <title>name</title>  
          <validators></validators> 
        </field>  
        <field name="Test2_id" foreignKey="Test2.Test2_id" type="integer" />
       </fields>  
      <operationBindings> 
        <operationBinding operationType="add" requiresAuthentication="false" requiresRole=""> 
        </operationBinding>  
        <operationBinding operationType="fetch" requiresAuthentication="false" requiresRole="">
        	<selectClause>*</selectClause> 
        	<tableClause>Test INNER JOIN Test2 ON Test.Test2_id = Test2.Test2_id</tableClause>
        </operationBinding>  
        <operationBinding operationType="update" requiresAuthentication="false" requiresRole=""> 
        </operationBinding>  
        <operationBinding operationType="remove" requiresAuthentication="false" requiresRole=""> 
        </operationBinding> 
      </operationBindings> 
    </DataSource>
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <DataSource ID="Test2" serverType="sql" tableName="Test2"> 
      <fields> 
        <field name="Test2_id" type="sequence" primaryKey="true"/>  
        <field name="test2" type="text" required="false" length="128"> 
          <title>name</title>  
          <validators></validators> 
        </field>  
      </fields>  
       <operationBindings> 
        <operationBinding operationType="add" requiresAuthentication="false" requiresRole=""> 
        </operationBinding>  
        <operationBinding operationType="fetch" requiresAuthentication="false" requiresRole="">
        	
         
        </operationBinding>  
        <operationBinding operationType="update" requiresAuthentication="false" requiresRole=""> 
        </operationBinding>  
        <operationBinding operationType="remove" requiresAuthentication="false" requiresRole=""> 
        </operationBinding> 
      </operationBindings>  
    </DataSource>
    MainEntry.html
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <!-- The HTML 4.01 Transitional DOCTYPE declaration-->
    <!-- above set at the top of the file will set     -->
    <!-- the browser's rendering engine into           -->
    <!-- "Quirks Mode". Replacing this declaration     -->
    <!-- with a "Standards Mode" doctype is supported, -->
    <!-- but may lead to some differences in layout.   -->
    
    <html>
      <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <meta name="gwt:property" content="locale=nl_NL">
        <title>Test Case</title>
    
      </head>
    
      <body>
    
    	 
       <!-- IMPORTANT : You must set the variable isomorphicDir to [MODULE_NAME]/sc/ so that the SmartGWT resource are correctly resolved -->	
       <script> var isomorphicDir = "generatedcode/sc/"; </script>
       <script type="text/javascript" language="javascript" src="generatedcode/generatedcode.nocache.js"></script>
    
       <!-- load datasources from server -->
       <script src="sc/DataSourceLoader?dataSource=Test,Test2"></script>
    
       <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
       
      </body>
    </html>
    If it is possible, could someone give a hint to a solution, on what to modify on this test case to export ALL while still using client side formatting?

    I see the complexity of things, but I still hope there is a work-around, without having to write an export servlet or whatever...

    Thanks in advance!
    Last edited by Sytematic; 3 Apr 2012, 01:29.

    Comment


      #3
      I kind of got around the issue by doing the export on the server, and use dsRequestProperties.setExportFields to configure the fields.

      This works well enough for now.

      Comment


        #4
        If you use dataSourceField.includeFrom, that's going to show up in exportData. Just declaring a foreignKey won't, but you can create an operationBinding in which you add in the value from the other table, via either DMI or customized SQL to do a join.

        Comment

        Working...
        X