1 (function(angular
, $, _
) {
4 angular
.module('crmSearchDisplayTable').component('crmSearchDisplayTable', {
11 templateUrl
: '~/crmSearchDisplayTable/crmSearchDisplayTable.html',
12 controller: function($scope
, crmApi4
, searchDisplayUtils
) {
13 var ts
= $scope
.ts
= CRM
.ts(),
17 this.selectedRows
= [];
18 this.allRowsSelected
= false;
20 this.$onInit = function() {
21 this.apiParams
= _
.cloneDeep(this.apiParams
);
22 this.apiParams
.limit
= parseInt(this.settings
.limit
|| 0, 10);
23 this.columns
= searchDisplayUtils
.prepareColumns(this.settings
.columns
, this.apiParams
);
26 this.getResults = function() {
27 var params
= searchDisplayUtils
.prepareParams(ctrl
.apiParams
, ctrl
.filters
, ctrl
.settings
.pager
? ctrl
.page
: null);
29 crmApi4(ctrl
.apiEntity
, 'get', params
).then(function(results
) {
30 ctrl
.results
= results
;
31 ctrl
.rowCount
= results
.count
;
35 $scope
.$watch('$ctrl.filters', ctrl
.getResults
, true);
38 * Returns crm-i icon class for a sortable column
42 $scope
.getOrderBy = function(col
) {
43 var dir
= ctrl
.apiParams
.orderBy
&& ctrl
.apiParams
.orderBy
[col
.key
];
45 return 'fa-sort-' + dir
.toLowerCase();
47 return 'fa-sort disabled';
51 * Called when clicking on a column header
55 $scope
.setOrderBy = function(col
, $event
) {
56 var dir
= $scope
.getOrderBy(col
) === 'fa-sort-asc' ? 'DESC' : 'ASC';
57 if (!$event
.shiftKey
|| !ctrl
.apiParams
.orderBy
) {
58 ctrl
.apiParams
.orderBy
= {};
60 ctrl
.apiParams
.orderBy
[col
.key
] = dir
;
64 $scope
.formatResult = function(row
, col
) {
65 var value
= row
[col
.key
];
66 return searchDisplayUtils
.formatSearchValue(row
, col
, value
);
69 $scope
.selectAllRows = function() {
71 if (ctrl
.allRowsSelected
) {
72 ctrl
.allRowsSelected
= false;
73 ctrl
.selectedRows
.length
= 0;
77 ctrl
.allRowsSelected
= true;
78 if (ctrl
.page
=== 1 && ctrl
.results
[1].length
< ctrl
.apiParams
.limit
) {
79 ctrl
.selectedRows
= _
.pluck(ctrl
.results
[1], 'id');
82 // If more than one page of results, use ajax to fetch all ids
83 $scope
.loadingAllRows
= true;
84 var params
= _
.cloneDeep(ctrl
.apiParams
);
85 params
.select
= ['id'];
86 crmApi4(ctrl
.apiEntity
, 'get', params
, ['id']).then(function(ids
) {
87 $scope
.loadingAllRows
= false;
88 ctrl
.selectedRows
= _
.toArray(ids
);
92 $scope
.selectRow = function(row
) {
93 var index
= ctrl
.selectedRows
.indexOf(row
.id
);
95 ctrl
.selectedRows
.push(row
.id
);
96 ctrl
.allRowsSelected
= (ctrl
.rowCount
=== ctrl
.selectedRows
.length
);
98 ctrl
.allRowsSelected
= false;
99 ctrl
.selectedRows
.splice(index
, 1);
103 $scope
.isRowSelected = function(row
) {
104 return ctrl
.allRowsSelected
|| _
.includes(ctrl
.selectedRows
, row
.id
);
110 })(angular
, CRM
.$, CRM
._
);