Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow disable of row selection #406

Merged
merged 4 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@
<ng-template pTemplate="body" let-rowObject>
<tr>
<td *ngIf="selectionChangedObserved">
<p-tableCheckbox [value]="rowObject"></p-tableCheckbox>
<p-tableCheckbox
[value]="rowObject"
[disabled]="!!selectionEnabledField && !fieldIsTruthy(rowObject, selectionEnabledField)"
></p-tableCheckbox>
</td>
<ng-container *ngIf="actionColumnPosition === 'left';">
<ng-container *ngTemplateOutlet="actionColumn; context: {localRowObject: rowObject}"></ng-container>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,31 @@ describe('DataTableComponent', () => {
expect(selectedCheckBoxes.length).toBe(1)
expect(selectionChangedEvent).toEqual([mockData[0]])
})

it('should not change selection if selection disabled', async () => {
let selectionChangedEvent: Row[] | undefined

component.selectionEnabledField = 'selectionEnabled'

component.rows = mockData.map((m) => ({
...m,
selectionEnabled: false,
}))

component.selectionChanged.subscribe((event) => (selectionChangedEvent = event))
unselectedCheckBoxes = await dataTable.getHarnessesForCheckboxes('unchecked')
selectedCheckBoxes = await dataTable.getHarnessesForCheckboxes('checked')
expect(unselectedCheckBoxes.length).toBe(5)
expect(selectedCheckBoxes.length).toBe(0)
expect(selectionChangedEvent).toBeUndefined()

const firstRowCheckBox = unselectedCheckBoxes[0]
await firstRowCheckBox.checkBox()
unselectedCheckBoxes = await dataTable.getHarnessesForCheckboxes('unchecked')
selectedCheckBoxes = await dataTable.getHarnessesForCheckboxes('checked')
expect(unselectedCheckBoxes.length).toBe(5)
expect(selectedCheckBoxes.length).toBe(0)
})
})

describe('Frozen action column', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class DataTableComponent extends DataSortBase implements OnInit {
@Input() viewActionEnabledField: string | undefined
@Input() editActionVisibleField: string | undefined
@Input() editActionEnabledField: string | undefined
@Input() selectionEnabledField: string | undefined
@Input() paginator = true
@Input() page = 0
@Input()
Expand Down
Loading