Load Primary Filter by default in related url

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • czcpf
    Senior Member
    • Aug 2022
    • 160

    Load Primary Filter by default in related url

    I'm having some trouble selecting a primary filter by default in url like:

    http:/{espoUrl}/#{MyEntity}/related/{myEntityId}/{relationFieldName}


    How does one get this list view to have my custom primary default filter already selected and applied?





  • yuri
    Member
    • Mar 2014
    • 8440

    #2
    In v8.2 this feature is available for the regular list view. You can view what code was added to accomplish that.
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • yuri
      Member
      • Mar 2014
      • 8440

      #3
      Example: #Lead/list/primaryFilter=actual. Will show the list view with the actual primary filter applied. With disabled abilities to change the primary filter and to save a preset. Can be useful in...
      If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

      Comment

      • czcpf
        Senior Member
        • Aug 2022
        • 160

        #4
        Since I am using Espo v7.4.5 still I am going to post my solution to this problem. Curious as to other's thoughts on this approach. Even if I had v8.2, the above solution still allows the end-user to 'clear' the filter. I do not want them to be able to 'clear' the filter. The filter should always be applied.

        Desired Outcome: Want collection results of entity to ALWAYS have primary filter applied. Regardless of user selecting primary filter in drop down, regardless of primary filter even being in dropdown, or even if it is in drop-down but they clear the select filter.

        Things in {} would need to be customized to suit your needs.

        1. Add label for your primary filter (in this case my filter is called 'latest'
        /Espo/Modules/{MyModuleName}/Resources/i8n/en_US/{MyEntityName.json}
        Code:
        {
        ...
        "presetFilters": {
        "latest": "Latest"
        }
        ...
        }​


        2. Define Primary Filter (in this case my filter is called 'latest'). This filter example applies a filter to select the most recent records by account
        /Espo/Modules/{MyModuleName}/Classes/Select/{MyEntityName}/PrimaryFilters/Latest.php
        PHP Code:
        <?php
        
        namespace Espo\Modules\{MyModuleName}\Classes\Select\{MyEntityName}\PrimaryFilters;
        
        use Espo\{
        Core\Select\Primary\Filter,
        Modules\{MyModuleName}\Entities\{MyEntityName},
        ORM\Query\SelectBuilder as QueryBuilder
        };
        
        class Latest implements Filter
        {
        
        public function apply(QueryBuilder $queryBuilder): void
        {
        
        $queryBuilder
        ->leftJoin({MyEntityName}::ENTITY_TYPE,'ct2',
        [
        'ct2.accountId:' => 'accountId',
        'ct2.createdAt>:' => 'createdAt',
        'ct2.deleted' => false
        ]
        )
        ->where(
        [
        'ct2.createdAt' => null,
        ]
        );
        
        }
        }
        
        3. Tell espo where to find your filter
        /Espo/Modules/{MyModuleName}/Resources/metadata/selectDefs/{MyEntityName.json}
        Code:
        {
        ...
        "primaryFilterClassNameMap": {
        "latest": "Espo\\Modules\\{MyModuleName}\\Classes\\Select\\{MyEntityName}\\PrimaryFilters\\Latest"
        }​
        ...
        }​
        ​​

        4. Create a custom collection class for your entity
        /client/modules/{MyModuleName}/src/collections/{my-entity-name.js}
        Code:
        define('{my-module-name}:collections/{my-entity-name}', ['collection'], function (Dep) {
        
        /**
        * @class
        * @name Class
        * @extends module:collection.Class
        * @memberOf module:{my-module-name}:views/collections/{my-entity-name}
        */
        return Dep.extend(/** @lends module:{my-module-name}:views/collections/{my-entity-name}.Class# */{
        
        /**
        * @inheritDoc
        */
        fetch: function (options) {
        
        //console.log('module:{my-module-name}:views/collections/{my-entity-name}.Class#');
        options = options || {};
        
        options.where = _.extend(options.where || [], this.where);
        
        let wherePrimaryFilter = {
        type: 'primary',
        value: 'latest'
        };
        
        //return prototype call if where already contains filter
        if (options.where.some(e => (e.type === 'primary') && (e.value === 'latest') )) {
        return Dep.prototype.fetch.call(this,options);
        }
        
        options.where.push(wherePrimaryFilter);
        
        return Dep.prototype.fetch.call(this,options);
        }​
        5. Tell ESPO to ALWAYS use your custom collection class for your entity
        /Espo/Modules/{MyModuleName}/Resources/metadata/clientDefs/{MyEntityName.json}
        Code:
        {
        ...
        "collection": "{my-module-name}:collections/{my-entity-name}",
        ...
        }​
        ​​​
        Last edited by czcpf; 03-05-2024, 08:07 PM.

        Comment

        • rabii
          Active Community Member
          • Jun 2016
          • 1250

          #5
          Hi

          I think yo could still manage to do it with a custom list view for your entity instead of custom collection, below is what i would do

          1 - define a custom view for under your entity clientDefs

          PHP Code:
          "views": {
                  "list": "custom:views/your-entity/list"
            }

          2 - define the custom view as below

          PHP Code:
          // LEGACY
          
          define('custom:views/your-entity/list', ['views/list'], function (Dep) {
          
              return Dep.extend({
          
                  setup: function () {
                      Dep.prototype.setup.call(this);
          
                      this.collection.data = {
                          boolFilterList: ['followed'],
                      };
                  },
              });
          });
          
          // IF YOU USE ES6
          
          import ListView from 'views/list';
          
          class YourEntityListView extends ListView {
          
          
              setup() {
                  super.setup();
          
                  this.collection.data = {
                      primaryFilterList: ['your-primary-filter'],
                      // you can also specify as below
                      boolFilterList: ['your-bool-filter'],
                  };
              }
          }
          
          export default YourEntityListView;
          Rabii
          Web Dev

          Comment

          • czcpf
            Senior Member
            • Aug 2022
            • 160

            #6
            Well that is certainly easier.

            Comment

            Working...