Skip to content

Commit 49cde88

Browse files
authored
feat: honoured allowAdd and allowDelete in SimpleTableManual Component (#296)
1 parent 20e3b01 commit 49cde88

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

packages/angular-sdk-components/src/lib/_components/template/simple-table-manual/helpers.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,19 @@ export const createPConnect = (contextName, referenceList, pageReference) => {
294294

295295
return getPConnect();
296296
};
297+
298+
/**
299+
* This method evaluates whether a row action is allowed based on the provided conditions.
300+
* @param {string|boolean|undefined} allowRowDelete - The condition for allowing row deletion.
301+
* @param {object} rowData - The data of the row being evaluated.
302+
* @returns {boolean} - Returns true if the row action is allowed, false otherwise.
303+
*/
304+
export const evaluateAllowRowAction = (allowRowDelete, rowData) => {
305+
if (allowRowDelete === undefined || allowRowDelete === true) return true;
306+
if (allowRowDelete.startsWith?.('@E ')) {
307+
const expression = allowRowDelete.replace('@E ', '');
308+
// @ts-ignore - Expected 3 arguments, but got 2
309+
return PCore.getExpressionEngine().evaluate(expression, rowData);
310+
}
311+
return false;
312+
};

packages/angular-sdk-components/src/lib/_components/template/simple-table-manual/simple-table-manual.component.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { ComponentMapperComponent } from '../../../_bridge/component-mapper/comp
1616
import { AngularPConnectData, AngularPConnectService } from '../../../_bridge/angular-pconnect';
1717
import { DatapageService } from '../../../_services/datapage.service';
1818
import { getReferenceList } from '../../../_helpers/field-group-utils';
19-
import { buildFieldsForTable, filterDataByCommonFields, filterDataByDate, getContext } from './helpers';
19+
import { buildFieldsForTable, evaluateAllowRowAction, filterDataByCommonFields, filterDataByDate, getContext } from './helpers';
2020
import { Utils } from '../../../_helpers/utils';
2121
import { getSeconds } from '../../../_helpers/common';
2222

@@ -36,7 +36,9 @@ interface SimpleTableManualProps {
3636
contextClass?: string;
3737
propertyLabel?: string;
3838
fieldMetadata?: any;
39+
allowActions?: any;
3940
allowTableEdit?: boolean;
41+
allowRowDelete?: any;
4042
editMode?: string;
4143
addAndEditRowsWithin?: any;
4244
viewForAddAndEditModal?: any;
@@ -231,7 +233,9 @@ export class SimpleTableManualComponent implements OnInit, OnDestroy {
231233
renderMode,
232234
children, // destructure children into an array var: "resolvedFields"
233235
presets,
236+
allowActions,
234237
allowTableEdit,
238+
allowRowDelete,
235239
label: labelProp,
236240
propertyLabel,
237241
fieldMetadata,
@@ -245,12 +249,22 @@ export class SimpleTableManualComponent implements OnInit, OnDestroy {
245249
targetClassLabel
246250
} = this.configProps$;
247251

252+
const simpleTableManualProps: any = {};
253+
if (this.checkIfAllowActionsOrRowEditingExist(allowActions) && editMode) {
254+
simpleTableManualProps.hideAddRow = allowActions?.allowAdd === false;
255+
simpleTableManualProps.hideDeleteRow = allowActions?.allowDelete === false;
256+
simpleTableManualProps.hideEditRow = allowActions?.allowEdit === false;
257+
simpleTableManualProps.disableDragDrop = allowActions?.allowDragDrop === false;
258+
} else if (allowTableEdit === false) {
259+
simpleTableManualProps.hideAddRow = true;
260+
simpleTableManualProps.hideDeleteRow = true;
261+
simpleTableManualProps.disableDragDrop = true;
262+
}
263+
248264
this.referenceListStr = getContext(this.pConn$).referenceListStr;
249265
this.label = labelProp || propertyLabel;
250266
this.parameters = fieldMetadata?.datasource?.parameters;
251267
this.targetClassLabel = targetClassLabel;
252-
const hideAddRow = allowTableEdit === false;
253-
const hideDeleteRow = allowTableEdit === false;
254268
let { contextClass } = this.configProps$;
255269
this.referenceList = referenceList;
256270
if (!contextClass) {
@@ -287,10 +301,10 @@ export class SimpleTableManualComponent implements OnInit, OnDestroy {
287301
this.readOnlyMode = renderMode === 'ReadOnly';
288302
this.editableMode = renderMode === 'Editable';
289303
const isDisplayModeEnabled = displayMode === 'DISPLAY_ONLY';
290-
this.showAddRowButton = !this.readOnlyMode && !hideAddRow;
304+
this.showAddRowButton = !this.readOnlyMode && !simpleTableManualProps.hideAddRow;
291305
this.allowEditingInModal =
292306
(editMode ? editMode === 'modal' : addAndEditRowsWithin === 'modal') && !(renderMode === 'ReadOnly' || isDisplayModeEnabled);
293-
const showDeleteButton = this.editableMode && !hideDeleteRow;
307+
const showDeleteButton = this.editableMode && !simpleTableManualProps.hideDeleteRow && evaluateAllowRowAction(allowRowDelete, this.rowData);
294308
this.defaultView = editModeConfig ? editModeConfig.defaultView : viewForAddAndEditModal;
295309
this.bUseSeparateViewForEdit = editModeConfig ? editModeConfig.useSeparateViewForEdit : useSeparateViewForEdit;
296310
this.editView = editModeConfig ? editModeConfig.editView : viewForEditModal;
@@ -356,6 +370,10 @@ export class SimpleTableManualComponent implements OnInit, OnDestroy {
356370
// ties the 3 data structures together.
357371
}
358372

373+
checkIfAllowActionsOrRowEditingExist(newflagobject) {
374+
return (newflagobject && Object.keys(newflagobject).length > 0) || this.pConn$.getComponentConfig().allowRowEdit;
375+
}
376+
359377
initializeDefaultPageInstructions() {
360378
if (this.isInitialized) {
361379
this.isInitialized = false;

0 commit comments

Comments
 (0)