Announcement

Collapse
No announcement yet.

Retrieving Selected Record Data in List View with a Custom Button in EspoCRM

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

  • Retrieving Selected Record Data in List View with a Custom Button in EspoCRM

    I created a button on the list view page and need to obtain the selected record data. I tried using this.getSelectedIds(), but it doesn't work.​

    HTML Code:
    "views": {
            "list": "custom:views/Employeework/list"
        }​

    HTML Code:
    define('custom:views/Employeework/list', 'views/list', function (Dep) {
    
        return Dep.extend({
    
            setup: function() {
                Dep.prototype.setup.call(this);
                this.setupCustomButtons();
            },
    
            setupCustomButtons: function () {
                    this.menu.buttons.push({
                    action: 'myAction',
                    html: '<span class="fas fa-chess-rook fa-sm"></span> ' + this.translate('Create Employeewages', 'labels', this.scope),
                    style: 'default',
                    acl: 'read',
                    aclScope: this.entityType || this.scope
                });
            },
    
            actionMyAction: function() {
                var selectedItems = this.getSelectedIds(); // 使用 getSelectedIds 方法
                if (!selectedItems.length) {
                    Espo.Ui.warning(this.translate('请选择出勤对象', 'messages', 'Employeework'));
                    return;
                }
    
            },
        });
    
    });​

  • #2
    > I tried using this.getSelectedIds(), but it doesn't work.​

    The probability that a random method name will work is very low. I recommend reading code. This is the only way to progress in programming.

    Your view extends from views/list. There's a method getRecordView. It will return views/record/list (if kanban is not enabled). That view has a method getSelected.

    Comment


    • #3
      @yuri

      Thank you for your guidance. I have received very helpful pointers. How can I turn this into payroll data? I would like to, after selecting the attendance table, click on a button labeled "Create Payroll" to batch create corresponding payroll entries.

      HTML Code:
      define('custom:views/Employeework/list', 'views/list', function (Dep) {
      
          return Dep.extend({
      
              setup: function() {
                  Dep.prototype.setup.call(this);
                  this.setupCustomButtons();
              },
      
              setupCustomButtons: function () {
                  this.menu.buttons.push({
                      action: 'myAction',
                      html: '<span class="fas fa-comment-dollar fa-sm"></span> ' + this.translate('Create Employeewages', 'labels', this.scope),
                      style: 'default',
                      acl: 'create', // Change 'read' to 'create'
                      aclScope: this.entityType || this.scope
                  });
              },
      
              actionMyAction: function() {
                  var selectedItems = this.getSelected();
                  if (!selectedItems.length) {
                      Espo.Ui.warning(this.translate('请选择出勤对象', 'Employeework'));
                      return;
                  } else {
                      selectedItems.forEach(function(item) {
                          console.log(item);
                          console.log(item.attributes.dateOfWork);
                          console.log(item.attributes.assignedUserId);
                          //corresponding payroll entries
                          // employeework = new Employeework();
                          // Assuming Employeewages is a model, create a new instance
                          // employeework = item.assignedUserId;
                          // dateOfWork = item.dateOfWork;
                          // Perform any necessary operations with employeewages object here
                          // console.log(employeewages);
                      }, this);
                  }
              },
      
               /**
               * Get selected models.
               *
               * @return {module:model[]}
               */
              getSelected() {
                  const list = [];
          
                  this.$el.find('input.record-checkbox:checked').each((i, el) => {
                      const id = $(el).attr('data-id');
                      const model = this.collection.get(id);
          
                      list.push(model);
                  });
          
                  return list;
              }
          });
      });
      ​
      
      
        ​
      Last edited by lj4353; 02-22-2024, 09:37 AM.

      Comment

      Working...
      X