Announcement

Collapse
No announcement yet.

How to apply a default filter to the list of records shown when editing a link field

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to apply a default filter to the list of records shown when editing a link field

    This tutorial describes how to apply a default filter to the list of entities displayed in a modal window when updating a link field.

    Background Information:

    In our application, we have an entity "CollectionsTracker", which we use to track payments received from rental units, and that entity is linked in a many-to-one relationship to a "Tenancy" entity which represents a lease contract for a given Contact entity and a Property entity.

    Tenancies can have two possible status values: "active" when the lease is current or "inactive" when the lease has expired.

    When a new CollectionsTracker record is created, the Bookkeeper needs to specify to which Tenancy is the tracker linked, thus a list of Tenancies is displayed in a modal window, and we want to make sure that only Tenancies with a status "active" are displayed as default.

    Click image for larger version  Name:	Collections Tracker detail view.png Views:	0 Size:	33.3 KB ID:	90180

    Click image for larger version  Name:	Tenancies Modal List View.png Views:	0 Size:	42.9 KB ID:	90181

    Implementation:

    1) Define the "Active" filter class for theTenancy entity
    custom/Espo/Modules/PropertyManagement/Classes/Select/Tenancy/PrimaryFilters/Active.php
    PHP Code:
    namespace Espo\Modules\PropertyManagement\Classes\Select\Tenancy\PrimaryFilters;

    use 
    Espo\ORM\Query\SelectBuilder;
    use 
    Espo\ORM\Query\Part\Condition as Cond;

    use 
    Espo\Core\Select\Primary\Filter;

    class 
    Active implements Filter
    {
        public function 
    apply(SelectBuilder $queryBuilder): void
        
    {
            
    $queryBuilder->where(
                
    Cond::equal(
                    
    Cond::column('status'),
                    
    'Active'
                
    )
            );
        }
    }
    ​ 

    2) Map the filter class to the Tenancy entity
    custom/Espo/Modules/PropertyManagement/Recources/metadata/selectDefs/Tenancy.json
    Code:
    {
        "primaryFilterClassNameMap": {
            "active": "Espo\\Modules\\PropertyManagement\\Classes\\Select\\Tenancy\\PrimaryFilters\\Active"
        }
    }
    3) Specify the default filter in the Tenancy entity clientDefs
    custom/Espo/Modules/PropertyManagement/Resources/metadata/clientDefs/Tenancy.json
    Code:
    {
        "defaultFilterData": {
               "primary": "active"
        },    
    ​
    }
    4) Create a custom field view class for the tenancy link
    client/custom/src/views/fields/primary-filtered-link.js
    Code:
    define('custom:views/fields/primary-filtered-link', ['views/fields/link'], function (Dep) {
    
        return Dep.extend({
    
            setup: function() {
                Dep.prototype.setup.call(this);
                this.selectPrimaryFilterName = this.getMetadata().get(['clientDefs', this.foreignScope, 'defaultFilterData', 'primary']) || this.selectPrimaryFilterName;            
            }
    
        });
    });
    5) Map the custom link field view to the tenancy link in the Collections Tracker entity
    custom/Espo/Custom/Resources/metadata/entityDefs/CollectionsTracker.json
    Code:
    {
        "fields": {
            "tenancy": {
                "type": "link",
                "tooltip": true,
                "view": "custom:views/fields/primary-filtered-link"
            }​
    ​    }
    }
    6) Clear cache and rebuild
    Last edited by telecastg; 04-04-2023, 12:48 AM.

  • #2
    As of the recent versions it's possible to apply a primary filter (as well as bool filters) to link fields w/o the need to create a custom view.

    In clientDefs:

    Code:
    {
        "relationshipPanels": {
            "tenancy": {
                "selectPrimaryFilter": "active"
            }
        }
    }
    Though, it's called panels, it is applied to link fields too.

    Comment


    • telecastg
      telecastg commented
      Editing a comment
      Hi espcrm, I am going to leave the code the way it is.

      Yuri's suggestion is a different (previously undocumented) way to accomplish steps 4 and 5, but both ways work.

      The suggestion is valuable, but from a learning point of view, I think that having a complete example of how to create and incorporate custom view classes, and how to create and incorporate filters can be more useful for Espo coders in general.
      Last edited by telecastg; 04-05-2023, 07:22 PM.

    • espcrm
      espcrm commented
      Editing a comment
      Thank! I will give this a try, hopefully I can implement it for a feature I been wanting to do: filter Real Estate entity: Place of Birth!

      For late future reader: keep an eye on the Learning Thread!

    • czcpf
      czcpf commented
      Editing a comment
      This is a great feature. Thanks. Does selectDefaultFilter work here as well? I've been struggling with how to show only those results meeting some condition based on a link relationship.

      Example, click the select link and in the modal window it only shows results where selectFilter field = something that needs to be set dynamically based on the parent entity attribute similar to how "createAttributeMap" works... Is this the purpose of "selectHandler" like below?


      "relationshipPanels": {
      "radiationMachineLocations": {
      "createAttributeMap": {
      },
      "selectHandler": "handlers/select-related/same-account"
      },
      }
      Last edited by czcpf; 08-11-2023, 05:24 PM.

  • #3
    Hello telecastg,

    question :
    Contact have many2many Epidemio

    Contact->type = Adult or Child.

    on detailView Contact, bottom panel Epidemio, if i will select some Epidemio, can i pass contact-type as primaryFilter ?

    it's i think the reverse what you do, you select from in my case, from Epidemio to only Contact->Adult
    The code from Yuri work perfect, but it's for me "hard-coded".. i need dynamic

    PS : i have post in wrong post, i have deleted .. and now post on good forum

    Regards​

    Comment


    • telecastg
      telecastg commented
      Editing a comment
      hello @item,

      What do you mean by dynamic?.

      Do you mean, that you would like to be able to select the type of Contact that is displayed in the bottom panel on the fly?.

      For example, have a button of some kind that allows you to select whether a list of Adult or Child Contacts are displayed in the bottom panel?

  • #4
    Hello telecastg

    look print-screen of Contact

    Contact have a enum field with 2 value : Adult or Child
    in bottom panel, Edpidemio :

    If contact->type == Adult , then primaryFilter for Epidemio = Adult
    if contact->type == Child, then primary Filter for Epidemio = Child.

    So the defaultPrimaryFilter of Epidemio is dependant of Contact->type.
    In the print-screen, target for this sample, is for Adult and Child, but some other value is only for Adult or only for Child.
    Nationality = Adult and Child
    Revenue = only Adult (a child have not a revenue)



    ​​​​

    Comment


    • item
      item commented
      Editing a comment
      Got-it
      your sample work, Contact-Epidemio relation when i have check : linkMultiple field in entityManager relation : Link Multiple field provides a handy way ...

      just a little modification, because link-muliple

      define('custom:views/contact/fields/epidemio-filtered-link', ['views/fields/link-multiple'], function (Dep) {

      return Dep.extend({

      setup: function() {
      Dep.prototype.setup.call(this);
      this.selectPrimaryFilterName = this.model.attributes['type'];
      }

      });
      });


      Now the goal is have this fonctionnality in the bottom panel when select new Epidemio.
      Voila, i have try to find but no luck..
      i think is something there for pass primaryFilter : https://github.com/espocrm/espocrm/b...ntact.json#L53
      Last edited by item; 05-13-2023, 05:59 PM.
Working...
X