Adding null checks and fixing values in slickgrid checkbox column plugin (#22050)

* Adding null checks to checkbox column

* Setting default value for undefined checkbox
This commit is contained in:
Aasim Khan
2023-02-27 18:52:16 -08:00
committed by GitHub
parent 7d4ee2d0b8
commit 45d41347ba

View File

@@ -60,7 +60,7 @@ export class CheckboxSelectColumn<T extends Slick.SlickData> implements Slick.Pl
private _handler = new Slick.EventHandler(); private _handler = new Slick.EventHandler();
private _options: ICheckboxSelectColumnOptions; private _options: ICheckboxSelectColumnOptions;
public index: number; public index: number;
private _headerCheckbox: HTMLInputElement; private _headerCheckbox: HTMLInputElement | undefined;
private _onChange = new Emitter<ICheckboxCellActionEventArgs>(); private _onChange = new Emitter<ICheckboxCellActionEventArgs>();
private _onCheckAllChange = new Emitter<ICheckAllActionEventArgs>(); private _onCheckAllChange = new Emitter<ICheckAllActionEventArgs>();
public readonly onChange: vsEvent<ICheckboxCellActionEventArgs> = this._onChange.event; public readonly onChange: vsEvent<ICheckboxCellActionEventArgs> = this._onChange.event;
@@ -193,8 +193,10 @@ export class CheckboxSelectColumn<T extends Slick.SlickData> implements Slick.Pl
break; break;
} }
} }
if (this._headerCheckbox) {
this._headerCheckbox.checked = checked; this._headerCheckbox.checked = checked;
} }
}
public destroy(): void { public destroy(): void {
this._handler.unsubscribeAll(); this._handler.unsubscribeAll();
@@ -217,12 +219,16 @@ export class CheckboxSelectColumn<T extends Slick.SlickData> implements Slick.Pl
const propertyValue = dataItem[this._options.title]; const propertyValue = dataItem[this._options.title];
let checkboxEnabled: boolean = true; let checkboxEnabled: boolean = true;
let checkboxChecked: boolean = false; let checkboxChecked: boolean = false;
if (typeof propertyValue === 'boolean') { if (typeof propertyValue === 'boolean') {
checkboxEnabled = true; checkboxEnabled = true;
checkboxChecked = propertyValue; checkboxChecked = propertyValue;
} else if (propertyValue !== undefined) { } else if (propertyValue !== undefined) {
checkboxEnabled = propertyValue.enabled === undefined ? true : propertyValue.enabled; checkboxEnabled = propertyValue.enabled === undefined ? true : propertyValue.enabled;
checkboxChecked = propertyValue.checked === undefined ? false : propertyValue.checked; checkboxChecked = propertyValue.checked === undefined ? false : propertyValue.checked;
} else if (propertyValue === undefined) { // If the value is undefined the checkbox will be enabled and unchecked
checkboxEnabled = true;
checkboxChecked = false;
} }
return { return {
@@ -234,7 +240,8 @@ export class CheckboxSelectColumn<T extends Slick.SlickData> implements Slick.Pl
private setCheckboxPropertyValue(row: number, value: boolean): void { private setCheckboxPropertyValue(row: number, value: boolean): void {
const dataItem = this._grid?.getDataItem(row); const dataItem = this._grid?.getDataItem(row);
const propertyValue = dataItem[this._options.title]; const propertyValue = dataItem[this._options.title];
if (typeof propertyValue === 'boolean') { // If property value is undefined we treat the cell value as a boolean
if (propertyValue === undefined || typeof propertyValue === 'boolean') {
(<any>dataItem)[this._options.title] = value; (<any>dataItem)[this._options.title] = value;
} else { } else {
(<any>dataItem)[this._options.title] = { (<any>dataItem)[this._options.title] = {