Announcement

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

    ListGrid ignores NumberUtil.setDecimalSymbol and setGroupingSymbol

    Hi,

    If you want to use a language from one locale (in my case English) in combination with formatting rules from another locale (in my case German/Europen)... you can use NumberUtil.setDecimalSymbol( "," ); and NumberUtil.setGroupingSymbol( "." ) (e.g. for German/European format rules, Doc => https://www.smartclient.com/smartgwt...umberUtil.html).

    This is working perfect for DynamicForms. But ListGrids are ignoring those settings (see screenshot). Furthermore you can not edit values in listGrids (listGrid.setCanEdit(true); with the german number notation...only english notation is working.

    Used Build: https://www.smartclient.com/builds/S...GPL/2016-08-23

    Thanks
    Andy


    Screenshot with settings umberUtil.setDecimalSymbol( "," ); NumberUtil.setGroupingSymbol( "." ) ;):
    Click image for larger version

Name:	numberUtil.jpg
Views:	423
Size:	71.1 KB
ID:	239891


    You can use the following test case:

    Code:
    import java.util.Date;
    import java.util.LinkedHashMap;
    
    import com.google.gwt.i18n.client.NumberFormat;
    import com.google.gwt.user.datepicker.client.CalendarUtil;
    import com.smartgwt.client.data.Criteria;
    import com.smartgwt.client.data.DataSource;
    import com.smartgwt.client.data.Record;
    import com.smartgwt.client.data.RecordList;
    import com.smartgwt.client.data.events.DataChangedEvent;
    import com.smartgwt.client.data.events.DataChangedHandler;
    import com.smartgwt.client.data.fields.DataSourceDateField;
    import com.smartgwt.client.data.fields.DataSourceFloatField;
    import com.smartgwt.client.data.fields.DataSourceSequenceField;
    import com.smartgwt.client.data.fields.DataSourceTextField;
    import com.smartgwt.client.types.Alignment;
    import com.smartgwt.client.types.AutoFitWidthApproach;
    import com.smartgwt.client.types.FetchMode;
    import com.smartgwt.client.types.GroupStartOpen;
    import com.smartgwt.client.types.ListGridEditEvent;
    import com.smartgwt.client.types.Overflow;
    import com.smartgwt.client.types.RecordDropAppearance;
    import com.smartgwt.client.types.SummaryFunctionType;
    import com.smartgwt.client.util.NumberUtil;
    import com.smartgwt.client.widgets.Canvas;
    import com.smartgwt.client.widgets.Window;
    import com.smartgwt.client.widgets.grid.CellFormatter;
    import com.smartgwt.client.widgets.grid.ListGrid;
    import com.smartgwt.client.widgets.grid.ListGridField;
    import com.smartgwt.client.widgets.grid.ListGridRecord;
    import com.smartgwt.client.widgets.grid.events.EditorExitEvent;
    import com.smartgwt.client.widgets.grid.events.EditorExitHandler;
    import com.smartgwt.client.widgets.layout.VLayout;
    import com.smartgwt.client.widgets.viewer.DetailViewer;
    
    public class GTestGrid2
    {
    
        private ListGrid listGrid;
    
        public GTestGrid2()
        {
            // sets Number Seperators
            NumberUtil.setDecimalSymbol( "," );
            NumberUtil.setGroupingSymbol( "." );
    
            initPopUp();
        }
    
        private void initPopUp()
        {
            initListGrid();
    
            VLayout layout = new VLayout();
            layout.setSize( "100%", "100%" );
            layout.setMembers( listGrid );
            layout.setOverflow( Overflow.HIDDEN );
    
            Window window = new Window();
            window.setOverflow( Overflow.HIDDEN );
            window.setShowShadow( true );
            window.setShadowSoftness( 10 );
            window.setShadowOffset( 5 );
            window.setSize( "100%", "100%" );
            window.setCanDragResize( true );
            window.setShowMaximizeButton( true );
            window.setShowMinimizeButton( true );
            window.setShowCloseButton( false );
            window.setAnimateMinimize( true );
            window.setShowCloseButton( true );
            window.setModalMaskOpacity( 50 );
            window.setIsModal( true );
            window.setShowModalMask( true );
            window.centerInPage();
            window.addItem( layout );
    
            window.draw();
    
            listGrid.getResultSet().addDataChangedHandler( new DataChangedHandler()
            {
    
                @Override
                public void onDataChanged( DataChangedEvent event )
                {
                    double totalAmount = 0;
    
                    // Gets all records of the listGrid
                    Record[] records = getAssignments();
    
                    for ( int i = 0; i < records.length; i++ )
                    {
                        totalAmount = totalAmount + records[i].getAttributeAsDouble( CAssignedPaymentsRec.total_amount );
                    }
                }
            } );
    
            // add Records
            addRecords( 10 );
        }
    
        private void initListGrid()
        {
            final DataSource dataSource = getDataSource();
            listGrid = new ListGrid()
            {
                @Override
                protected Canvas getCellHoverComponent( Record record, Integer rowNum, Integer colNum )
                {
                    Criteria criteria = new Criteria();
                    criteria.addCriteria( dataSource.getPrimaryKeyFieldName(),
                                          record.getAttribute( dataSource.getPrimaryKeyFieldName() ) );
    
                    DetailViewer viewer = new DetailViewer();
                    viewer.setEmptyCellValue( "---" );
                    viewer.setDataSource( dataSource );
                    viewer.setWrapValues( false );
                    viewer.setWrapLabel( false );
                    viewer.setShowShadow( true );
                    viewer.setShowEmptyField( false );
                    viewer.setShowHiddenFields( true );
                    viewer.setShowEmptyField( false );
                    viewer.setShowDetailFields( true );
                    viewer.fetchData( criteria );
    
                    return viewer;
                }
            };
    
            listGrid.setMargin( 25 );
            listGrid.setDataSource( dataSource );
            listGrid.setAutoFetchData( true );
    
            listGrid.setShowFilterEditor( true );
            listGrid.setAllowFilterExpressions( true );
            listGrid.setShowRowNumbers( true );
            listGrid.setFastCellUpdates( true );
            listGrid.setEmptyCellValue( "---" );
            listGrid.setAllowFilterExpressions( true );
            listGrid.setDataFetchMode( FetchMode.BASIC );
            listGrid.setWrapHeaderTitles( true );
            listGrid.setCanResizeFields( true );
            listGrid.setCellPadding( 4 );
            listGrid.setFixedRecordHeights( false );
            listGrid.setGroupByMaxRecords( 1000 );
            listGrid.setBodyOverflow( Overflow.AUTO );
            listGrid.setOverflow( Overflow.AUTO );
            listGrid.setLeaveScrollbarGap( false );
            listGrid.setAutoFitWidthApproach( AutoFitWidthApproach.BOTH );
            listGrid.setAutoFitHeaderHeights( true );
            listGrid.setAutoSizeHeaderSpans( true );
            listGrid.setCanAutoFitFields( false );
            listGrid.setRecordDropAppearance( RecordDropAppearance.BOTH );
            listGrid.setCanHover( true );
            listGrid.setShowHover( true );
            listGrid.setShowHoverComponents( true );
            listGrid.setHoverDelay( 100 );
            
            listGrid.setCanEdit( true );
            listGrid.setEditEvent( ListGridEditEvent.CLICK );
            listGrid.addEditorExitHandler( new EditorExitHandler()
            {
                
                @Override
                public void onEditorExit( EditorExitEvent event )
                {
                   listGrid.validateRow( event.getRowNum() );
                }
            } );
    
            listGrid.setCanAddFormulaFields( true );
            listGrid.setCanAddSummaryFields( true );
    
            // Sets Columns
            setColumns( listGrid );
    
        }
    
        private void setColumns( ListGrid listGrid )
        {
            ListGridField idField = new ListGridField( "id" );
            idField.setWidth( "10%" );
            idField.setAlign( Alignment.CENTER );
            idField.setShowGroupSummary( false );
            idField.setHidden( true );
            idField.setCanEdit( false );
    
            ListGridField descriptionField = new ListGridField( "description" );
            descriptionField.setWidth( "60%" );
            descriptionField.setCanEdit( false );
    
            ListGridField netField = new ListGridField( "netAmount" );
            netField.setSummaryFunction( SummaryFunctionType.SUM );
            netField.setShowGroupSummary( true );
            netField.setShowGridSummary( true );
            netField.setWidth( "10%" );
            netField.setAlign( Alignment.CENTER );
            netField.setCellFormatter( new CellFormatter()
            {
                public String format( Object value, ListGridRecord record, int rowNum, int colNum )
                {
    
                    if ( value == null )
                        return null;
                    try
                    {
                        NumberFormat fmt = NumberFormat.getFormat( "#,###,##0.00" );
                        return fmt.format( Double.valueOf( String.valueOf( value ) ) );
                    }
                    catch ( Exception e )
                    {
                        System.out.println( "Double: " + value.toString() );
                        return value.toString();
                    }
                }
            } );
    
            ListGridField taxField = new ListGridField( "taxAmount" );
            taxField.setWidth( "10%" );
            taxField.setSummaryFunction( SummaryFunctionType.SUM );
            taxField.setShowGroupSummary( true );
            taxField.setShowGridSummary( true );
            taxField.setAlign( Alignment.CENTER );
            taxField.setCellFormatter( new CellFormatter()
            {
                public String format( Object value, ListGridRecord record, int rowNum, int colNum )
                {
    
                    if ( value == null )
                        return null;
                    try
                    {
                        NumberFormat fmt = NumberFormat.getFormat( "#,###,##0.00" );
                        return fmt.format( Double.valueOf( String.valueOf( value ) ) );
                    }
                    catch ( Exception e )
                    {
                        System.out.println( "Double: " + value.toString() );
                        return value.toString();
                    }
                }
            } );
    
            ListGridField grossField = new ListGridField( "grossAmoumnt" );
            grossField.setSummaryFunction( SummaryFunctionType.SUM );
            grossField.setShowGroupSummary( true );
            grossField.setShowGridSummary( true );
            grossField.setAlign( Alignment.CENTER );
            grossField.setWidth( "10%" );
            grossField.setCellFormatter( new CellFormatter()
            {
                public String format( Object value, ListGridRecord record, int rowNum, int colNum )
                {
    
                    if ( value == null )
                        return null;
                    try
                    {
                        NumberFormat fmt = NumberFormat.getFormat( "#,###,##0.00" );
                        return fmt.format( Double.valueOf( String.valueOf( value ) ) );
                    }
                    catch ( Exception e )
                    {
                        System.out.println( "Double: " + value.toString() );
                        return value.toString();
                    }
                }
            } );
    
            ListGridField currencyField = new ListGridField( "currency" );
            currencyField.setAlign( Alignment.CENTER );
            currencyField.setWidth( "8%" );
            currencyField.setCanEdit( false );
    
            ListGridField dateField = new ListGridField( "date" );
            dateField.setWidth( "15%" );
            dateField.setAlign( Alignment.LEFT );
    
            listGrid.setFields( idField, descriptionField, netField, taxField, grossField, dateField );
    
            // Group ListGrid
            listGrid.setGroupByField( "currency" );
            listGrid.setGroupStartOpen( GroupStartOpen.ALL );
            listGrid.setShowGroupSummary( true );
            listGrid.setShowGridSummary( true );
    
        }
    
        private DataSource getDataSource()
        {
            DataSource ds = new DataSource();
            ds.setID( "payments" );
            ds.setClientOnly( true );
    
            DataSourceSequenceField idField = new DataSourceSequenceField( "id", "ID" );
            idField.setPrimaryKey( true );
            idField.setHidden( true );
    
            DataSourceTextField descriptionField = new DataSourceTextField( "description", "Description" );
            descriptionField.setRequired( true );
            
            DataSourceFloatField netField = new DataSourceFloatField( "netAmount", "Net Amount" );
            netField.setRequired( true );
            netField.setDecimalPrecision( 2 );
            netField.setDecimalPad( 2 );
    
            DataSourceFloatField taxField = new DataSourceFloatField( "taxAmount", "Tax Amount" );
            taxField.setRequired( true );
            taxField.setDecimalPrecision( 2 );
            taxField.setDecimalPad( 2 );
    
            DataSourceFloatField grossField = new DataSourceFloatField( "grossAmoumnt", "Gross Amount" );
            grossField.setRequired( true );
            grossField.setDecimalPrecision( 2 );
            grossField.setDecimalPad( 2 );
    
            DataSourceTextField currencyField = new DataSourceTextField( "currency", "Currency" );
            currencyField.setValueMap( getCurrencys() );
            currencyField.setRequired( true );
    
            DataSourceDateField dateField = new DataSourceDateField( "date", "Date" );
    
            ds.setFields( idField, descriptionField, netField, taxField, grossField, currencyField, dateField );
    
            return ds;
        }
    
      
        public Record[] getAssignments()
        {
            RecordList recordList = null;
            if ( listGrid.isGrouped() )
            {
                recordList = listGrid.getOriginalRecordList();
            }
            else
            {
                recordList = listGrid.getResultSet();
            }
    
            Record[] result = (Record[]) recordList.toArray();
    
            return result;
        }
    
        public LinkedHashMap<String, String> getCurrencys()
        {
            LinkedHashMap<String, String> items = new LinkedHashMap<String, String>();
    
            items.put( "1", "EUR" );
            items.put( "2", "GBP" );
            items.put( "3", "CHF" );
            items.put( "4", "SEK" );
            items.put( "5", "DKK" );
            items.put( "6", "PLN" );
            items.put( "7", "BGN" );
            items.put( "8", "CZK" );
            items.put( "9", "HUF" );
            items.put( "10", "HRK" );
            items.put( "11", "RUB" );
            items.put( "12", "RON" );
    
            return items;
        }
    
        private void addRecords( int counter )
        {
            Date date = getFirstDayOfMonth( new Date() );
            Date date2 = getFirstDayOfMonth( new Date() );
    
            for ( int i = 0; i < counter; i++ )
            {
                ListGridRecord record = new ListGridRecord();
                record.setAttribute( "id", 1 + i );
                record.setAttribute( "description", "Test " + 1 + i );
                record.setAttribute( "netAmount", new Double( 1000 + i ) );
                record.setAttribute( "taxAmount", new Double( 0 ) );
                record.setAttribute( "grossAmoumnt", new Double( 1000 + i ) );
                record.setAttribute( "currency", "1" );
    
                CalendarUtil.addDaysToDate( date, 1 );
                record.setAttribute( "date", date );
    
                listGrid.addData( record );
            }
    
            for ( int i = 0; i < counter; i++ )
            {
                ListGridRecord record = new ListGridRecord();
                record.setAttribute( "id", ( ( counter + 1 ) + i ) );
                record.setAttribute( "description", "Test " + 1 + i );
                record.setAttribute( "netAmount", new Double( 1 + i ) );
                record.setAttribute( "taxAmount", new Double( 0 ) );
                record.setAttribute( "grossAmoumnt", new Double( 1 + i ) );
                record.setAttribute( "currency", "1" );
    
                CalendarUtil.addDaysToDate( date2, 1 );
                record.setAttribute( "date", date2 );
    
                listGrid.addData( record );
            }
        }
    
        // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        /**
         * Returns the first day of the current month
         */
        // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        public static Date getFirstDayOfMonth( Date date )
        {
            CalendarUtil.resetTime( date );
            CalendarUtil.setToFirstDayOfMonth( date );
    
            return date;
        }
    
    }

    #2
    You are doing your own formatting via GWT's NumberFormat class - this obviously will not respond to SmartGWT settings.

    Instead, see dataSourceField.format.

    For parsing, if you were to actually set the locale to somewhere in Europe, you could use the "localeFloat" field type and get automatic, locale-sensitive formatting and parsing. Since you're not doing this, the parsing logic is still the US logic, which will of course reject German-style input or treat it as US input where applicable. So for your case, you need to define your own SimpleType that has formatters and parsers that agree, and use that pervasively as your field type.

    Comment


      #3
      Ah... okay thank you... seems to work!!

      I changed the DataSourceFloatField as follows:
      Code:
       DataSourceFloatField grossField = new DataSourceFloatField( "grossAmoumnt", "Gross Amount" );
              grossField.setRequired( true );
              grossField.setDecimalPrecision( 2 );
              grossField.setDecimalPad( 2 );
              grossField.setType( FieldType.LOCALEFLOAT );
      Furthermore I changed the ListGridFields as follows:
      Code:
       ListGridField grossField = new ListGridField( "grossAmoumnt" );
              grossField.setSummaryFunction( SummaryFunctionType.SUM );
              grossField.setShowGroupSummary( true );
              grossField.setShowGridSummary( true );
              grossField.setAlign( Alignment.CENTER );
              grossField.setWidth( "10%" );
              grossField.setFormat( "#,###,##0.00" );
      Worked for me!!!

      Comment


        #4
        Is there a way to get the german notation plus adding a "%" behind the value? The following code still produces the english notation. It appears as 25.56% instead of 25,56%

        Code:
                DataSourceFloatField grossField = new DataSourceFloatField( "grossAmoumnt", "Gross Amount" );
                field.setDecimalPrecision( 2 );
                field.setDecimalPad( 2 );
                field.setType( FieldType.LOCALEFLOAT );
                field.setFormat( "#,###,##0.00");
        
        
                ListGridField grossField = new ListGridField( "grossAmoumnt" );
                grossField.setCellFormatter( new CellFormatter()
                {
                    public String format( Object value, ListGridRecord record, int rowNum, int colNum )
                    {
                        if ( value == null )
                            return null;
                        try
                        {
                            return value + "%";
                        }
                        catch ( Exception e )
                        {
                            System.out.println( "Double: " + value.toString() );
                            return value.toString();
                        }
                    }
                } );
        Last edited by andyx1975; 25 Aug 2016, 05:04.

        Comment


          #5
          Note: the filters will not always work with a cellFormatter where you change values (e.g. add "%" or change "." to ","). The filter works on the unchanged values. So if you filter for something ending in "%" you won't get any result.

          Comment


            #6
            Ah okay... thanks for this information

            Comment


              #7
              See the FormatString docs - you can put in literal characters.

              Comment


                #8
                Thanks.... this worked for me:

                Code:
                field.setFormat( "#,###,##0.00'%'");

                Comment

                Working...
                X