Announcement

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

    Custom sort order with paging and enums

    I have some questions on sorting best practices with enums and custom sorting with and without paging in a list grid. We are using Smart Client v8.3p_2013-07-18/Pro Deployment and have a data source that has a field 'aStatusField' of type enum. When I sort this column in the ListGrid, it sorts them alphabetically - "In Progress," "Resolved," and "Unresolved." However, I want to have them logically sorted as "Unresolved," "In Progress," and "Resolved." The data source and the java enum for representing the Status is copied below.

    Code:
    <DataSource ID="MyPageDS" serverConstructor="spring:myPageDataSource"
    	schemaBean="com.mycompany.MyPage" validateRelatedRecords="true">
    	<fields>
    		<field primaryKey="true" name="id" title="ID" type="sequence" />
    		…
    		<field name="aStatusField" type="enum" valueMapEnum="com.mycompany.StatusEnum" />
    	</fields>
    
    </DataSource>
    Code:
    public enum StatusEnum {
    
    	Unresolved("Unresolved"),
    	InProgress("In Progress"),
    	Resolved("Resolved");
    
    	private String value;
    
    	private int order;
    
    	private StatusEnum(final String value) {
    
    		this.value = value;
    	}
    }
    I have been reading up a lot on sorting and I understand that sorting has to be done differently on the server-side and client-side.

    If sorting is done on the client-side, I can have a sortNormalizer function on the status field in the ListGrid and could have a map representing the text value and the order similar to the java enum.
    Code:
    var statusSortMap = {
    	"Unresolved": 1,
    	"In Progress": 2,
    	"Resolved": 3
    };
    …
    sortNormalizer: function(record, fieldName) {
    	var status = record.aStatusField;
    	return statusSortMap[status];
    }
    However, it would be ideal if I could use the enum itself for this. Is there a way to do this? Below is the snippet for the data source generated.

    Code:
    isc.DataSource.create({
        ID:"MyPageDS",
        ...
    	fields: [
    		...
    		{
    			valueMap:{
    				Resolved:"Resolved",
    				Unresolved:"Unresolved",
    				InProgress:"In Progress"
    			},
    			valueMapEnum:"com.mycompany.StatusEnum",
    			name:"aStatusField",
    			type:"enum"
    		}
    	]
    })
    Thanks for your help in advance.
    Last edited by mgreenberg; 15 Aug 2013, 11:53. Reason: Removed the server-side question

    #2
    Seems like your question boils down to how to do an alphabetic sort in SQL for a numeric column.

    This differs a bit by browser but is generally a CASE statement (MySQL, SQLServer) or DECODE statement (Oracle).

    SQLDataSource automatically handles this, so you can take a look at various samples and watch the server logs to see some sample syntax.

    Comment


      #3
      I have modified our code to use intEnums in the datasource in the hope that I could sort based on the ordinal value for the enum. I tried setting sortByMappedValue to false in the ListGridField 'aStatusField' but it doesn't sort based on the enum ordinal. Am I missing something?

      Here is the DS.xml
      Code:
      <DataSource ID="MyPageDS" serverConstructor="spring:myPageDataSource"
      	schemaBean="com.mycompany.MyPage" validateRelatedRecords="true">
      	<fields>
      		<field primaryKey="true" name="id" title="ID" type="sequence" />
      		…
      		<field name="aStatusField" type="intEnum" valueMapEnum="com.mycompany.StatusEnum" />
      	</fields>
      
      </DataSource>
      and here is the data source created when I view source on the page. For some reason, there seems to be two different representations for aStatusField - one with the ordinals and one without. This seemed suspicious and maybe it is using that field definition instead of the field definition for the enum ordinals for client-side sorting.
      Code:
      isc.DataSource.create({
      allowAdvancedCriteria:true,
      validateRelatedRecords:true,
      ID:"MyPageDS",
      operationBindings:[
      	{
      		operationType:"fetch"	
      	}
      ],
      inheritsFrom:isc.DataSource.create({
      	allowAdvancedCriteria:true,
      	serverType:null,
      	generatedBy:"v8.3p_2013-07-18/Pro Deployment 2013-07-18",
      	xmlFromConfig:"true",
      	ID:"MyPageDS_inheritsFrom",
      	dataSourceVersion:"1",
      	fields:[
      		....
      		{
      			valueMap:{
      				Resolved:"Resolved",
      				InProgress:"In Progress",
      				Unresolved:"Unresolved"
      			},
      			name:"aStatusField",
      			type:"enum"
      		},
      		....
      	]
      })
      ,
      fields:[
      	....
      	{
      		valueMap:{
      			"0":"Unresolved",
      			"1":"In Progress",
      			"2":"Resolved"
      		},
      		valueMapEnum:"com.mycompany.StatusEnum",
      		name:"aStatusField",
      		type:"intEnum"
      	},
      	....
      ]
      })

      Comment


        #4
        Thanks to the responses to my other forum post at http://forums.smartclient.com/showthread.php?p=108602, the solution to this was to set the enumTranslationStrategy in the datasource to "ordinal" and that sorted this column based on the ordinal value instead of the string value.

        Comment

        Working...
        X