Creating a new percentage Field?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Marcel
    Junior Member
    • Jul 2025
    • 9

    #1

    Creating a new percentage Field?

    Hi all,

    is there a way to create a new percentage Field based on the float field? Just want to see the value like 5,45 % instead of 5,45 in the layout.

    Thanks!
  • item
    Active Community Member
    • Mar 2017
    • 1538

    #2
    Hi,

    i am not specialist of front-end but you can begin with this working code (espoCrm v9.1.8)
    it's a little different because he color if value is < 0 or = 0 or > 0. but you have the direction of how to do.

    create a file : client/custom/src/views/fields/pourcent-float.js

    PHP Code:

    define
    (['views/fields/float'], (FloatFieldView) => {

       return class extends 
    FloatFieldView {

            
    setup () {
                
    super.setup();
            }

            
    afterRender () {
                
    super.afterRender();
                if (
    this.mode == 'search') return;
                
    let value parseFloat(this.model.get(this.name));

                if (
    this.mode =='list'){
                    
    this.$el.text('');
                    
    this.$el
                        
    .addClass(this.getListClass(value))
                        .
    append($('<span>').text(value.toFixed(2) + '%'));
                }else{
                    
    this.$el.text('');
                    
    this.$el.append(this.getSpanTag(value) );
                }

                
    this.$el.on('change', (e) => {
                    
    let value parseFloat(this.model.get(this.name));
                    if (
    this.mode =='list'){
                        
    this.$el.text('');
                        
    this.$el
                            
    .addClass(this.getListClass(value))
                            .
    append($('<span>').text(value.toFixed(2) + '%'));
                    }else{
                        
    this.$el.text('');
                        
    this.$el.append(this.getSpanTag(value) );
                    }
                });
            }

            
    getListClassvalue ){
                if ( 
    value 0) {
                    return 
    'text-primary';
                }

                if (
    value 0.001 && value > -0.001 ) {
                    return 
    'text-success';
                }

                if (
    value 0){
                    return 
    'text-danger'
                
    }

            }

            
    getSpanTag value ){
                if ( 
    value 0) {
                    return $(
    '<span>')
                                .
    addClass('label label-md label-primary')
                                .
    text(value.toFixed(2) + '%');
                }

                if (
    value 0.001 && value > -0.001 ) {
                    return $(
    '<span>')
                                .
    addClass('label label-md label-success')
                                .
    text(value.toFixed(2) + '%');
                }

                if (
    value 0){
                    return $(
    '<span>')
                                .
    addClass('label label-md label-danger')
                                .
    text(value.toFixed(2) + '%');
                }
            }
        }
    }); 
    And in your entityDefs (where the field must be append with %) here a sample field "balance" :

    path : custom/Espo/Custom/Resources/metadata/entityDefs/Contact.json. // for sample in Contact.json

    PHP Code:

            
    "balance": {
                
    "notNull"false,
                
    "type""float",
                
    "default"0,
                
    "readOnly"true,
                
    "isCustom"true,
                
    "duplicateIgnore"true,
                
    "decimalPlaces"2,
                
    "view""custom:views/fields/pourcent-float",   // only add this line
                
    "audited"true
            
    }, 
    clearCache, rebuid... clear browser (maybe) .. and you will see result
    Last edited by item; Yesterday, 07:27 PM.
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

    Comment

    Working...