Dynamic related fields

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • talles.amadeu
    Junior Member
    • Jan 2020
    • 27

    Dynamic related fields

    So i in the opportunities entity I related a Products entity, from which I created a few related fields, like product weight and price.

    Is there a way to make these related fields appear as I select the product record? Because right now they only appear when I hit the save button.

    I have websocket enabled and working.


    Click image for larger version

Name:	image.png
Views:	274
Size:	16.0 KB
ID:	95028
  • abidoss
    Senior Member
    • Mar 2023
    • 230

    #2
    Hello, in the path, select the file Opportunités.json

    custom/Espo/Custom/Resources/metadata/entityDefs
    PHP Code:
    
            "products ": {
                "type": "link",
                "view": "custom:views/fields/profil",
                "isCustom": true
            },
    
            "opportunités": {
                "readOnly": true,
                "type": "foreign",
                "link": "products",
                "field": "name",
                "view": "views/fields/foreign-varchar",
                "isCustom": true
            },​​ 
    
    --------

    and add this file to the path.

    PHP Code:
    
    define('custom:views/fields/profil', ['views/fields/link', 'helpers/record-modal'], function (Dep) {
    
        return Dep.extend(/** @lends module:views/fields/link.Class# */{
      _foreignLinksAttributeHashMap: [],
    
        
        setup: function () {
                console.log("custom:views/digital-imaging-qa-qc-record/fields/digital-imaging-qa-qc-protocol this.model = ", this.model);
    
    
    
                Dep.prototype.setup.call(this);
    
                //setup foreign link hash map
                this._setupForeignLinkHashMap();
    
                // reRender the foreign field views anytime the link change event fires
                this.listenTo(this, 'change', () => {
                    this._foreignLinksAttributeHashMap.forEach(function (obj, index, array) {
                        let fieldView = this.getParentView().getFieldView(obj.entityFieldName);
                        if (fieldView.isRendered()) {
                            fieldView.reRender();
                        }
                    }, this);
                });
            },
    
            /**
             * Select.
             *
             * @param {module:model.Class} model A model.
             * @protected
             */
    select: function (model) {
    
    this._foreignLinksAttributeHashMap.forEach(function(obj,index,array) {
    
    let fieldView = this.getParentView().getFieldView(obj.entityFieldName);
    fieldView.model.set(fieldView.name,model.get(obj.linkFieldName),{silent:true});
    
    },this);
    
    Dep.prototype.select.call(this,model);
    },
    
            /**
             * Clear.
             */
          clearLink: function () {
    
    this._foreignLinksAttributeHashMap.forEach(function(obj,index,array) {
    
    let fieldView = this.getParentView().getFieldView(obj.entityFieldName);
    fieldView.model.unset(fieldView.name,{silent:true});
    
    },this);
    
    Dep.prototype.clearLink.call(this);
    },
    
    
            _setupForeignLinkHashMap: function () {
                let thisObj = this;
                thisObj._foreignLinksAttributeHashMap = [];
                thisObj.mandatorySelectAttributeList = this.mandatorySelectAttributeList || [];
    
                //get list of foreign fields of the entity this field belongs to
                $.each(this.getMetadata().get(['entityDefs', this.entityType, 'fields']), function (entityFieldName, entityFieldAttributes) {
                    if ('type' in entityFieldAttributes && entityFieldAttributes.type === 'foreign' &&
                        'link' in entityFieldAttributes && entityFieldAttributes.link === thisObj.name &&
                        'field' in entityFieldAttributes) {
                        thisObj._foreignLinksAttributeHashMap.push({
                            entityFieldName: entityFieldName,
                            linkFieldName: entityFieldAttributes.field
                        });
                        thisObj.mandatorySelectAttributeList.push(entityFieldAttributes.field);
                    }
                });
            },
        });
    });
    

    Comment


    • esforim
      esforim commented
      Editing a comment
      Wow a new pro-coder!

    • item
      item commented
      Editing a comment
      Just for information :

      When copy > past from forum, something add some special char, you need to remove theses special char else the code don’t work.
      It seems the requester have this issue
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #3
    Originally posted by talles.amadeu
    So i in the opportunities entity I related a Products entity, from which I created a few related fields, like product weight and price.

    Is there a way to make these related fields appear as I select the product record? Because right now they only appear when I hit the save button.

    I have websocket enabled and working.


    Click image for larger version

Name:	image.png
Views:	274
Size:	16.0 KB
ID:	95028
    This might help

    Hi, everyone. I am looking for a way to display foreign field values while changing the related field link. I have create new foreign field in entity, but the value never show while create / edit. Is there any solution for this need? Thank you
    Rabii
    Web Dev

    Comment

    Working...