Smart Table Props Color
32x0lf opened this issue · 16 comments
@32x0lf what you're looking for is to overwrite css variables for .table-striped
For example, in src/scss/_custom.scss
file:
// Here you can add other styles
.table-striped {
--cui-table-striped-bg: lightpink;
--cui-table-striped-color: darkred;
}
Yes,all sorted column only. It will change only when sort header column is clicked.
Do you have sample code on how to implement this? I tried putting color inside that event but it didn't work.
html file
(sorterValueChange)="handleSorterValueChange($event)"
ts file
handleSorterValueChange(sorterValue: any) {
const sorter = Object.keys(sorterValue).length
? { color: "success"}
: { color: 'info'};
console.log('handleSorterValueChange', sorter);
Let's try this way:
- in your component - add props
- in event handler - store required style and column name
- in html - style the
<td>
accordingly for ng-template cTemplateId="tableData"
...
// 1. in your component - add props
sortedColumnStyle = {};
sortedColumnKey = '';
// 2. in event handler - store required style and column name
handleSorterValueChange(sorterValue: any) {
console.log('sorterValueChange', sorterValue);
this.sortedColumnStyle = sorterValue?.state ? {backgroundColor: '#badce3'} : {};
this.sortedColumnKey = sorterValue?.column ?? '';
}
<!-- 3. in html - style the td accordingly -->
<c-smart-table
...
(sorterValueChange)="handleSorterValueChange($event)"
>
<ng-template cTemplateId="tableData" let-columnName="columnName" let-item="item" let-tdContent="tdContent">
<td [ngStyle]="sortedColumnKey === columnName ? sortedColumnStyle: {}">
...
{{tdContent}}
...
</td>
</ng-template>
</c-smart-table>
Thanks, should I do the same for the header as well? Should I use th instead of td?
cTemplateId="tableData"
works for <tbody>
only
- assuming you have defined
columns
and every column def has akey
- you should also take care of types
handleSorterValueChange(sorterValue: any) {
this.sortedColumnStyle = sorterValue?.state ? {backgroundColor: '#badce3'} : {};
this.sortedColumnKey = sorterValue?.column ?? '';
this.columns.forEach((column) => {
column._style = column.key === sorterValue?.column
? { ...column._style, backgroundColor: '#badce3' }
: { ...column._style, backgroundColor: null };
});
}
why I am getting this error?
`Type '{ backgroundColor: string; minWidth?: undefined; width?: undefined; }' is not assignable to type '{ minWidth: string; width: string; backgroundColor: null; } | { backgroundColor: null; minWidth?: undefined; width?: undefined; } | { minWidth: string; width: string; backgroundColor: string; } | { ...; }'.
Type '{ backgroundColor: string; minWidth?: undefined; width?: undefined; }' is not assignable to type '{ minWidth: string; width: string; backgroundColor: null; }'.
Types of property 'minWidth' are incompatible.
Type 'undefined' is not assignable to type 'string'.
81 column._style = column.key === sorterValue?.column`
in my column property there is one column that I set like this _style: {backgroundColor: ''}
. If I add another style like width I will get that error
@32x0lf that's why you should also take care of types
a minimal setup could look like:
interface IColumn {
key: string;
label?: string;
filter?: boolean;
sorter?: boolean;
_style?: any;
_props?: any;
_classes?: any;
}
and in your component:
columns: IColumn[] = [
...
Oh. thanks for the info.
Hi @xidedix ,
I noticed this code
this.columns.forEach((column) => {
column._style = column.key === sorterValue?.column
? { ...column._style, backgroundColor: '#badce3' }
: { ...column._style, backgroundColor: null };
});
is no longer working. I don't know maybe after updating to latest version of coreui pro for angular 14 version,
Just noticed this one today.
@32x0lf mutated columns won't trigger change detection,
you can try:
this.columns.forEach((column) => {
column._style = column.key === sorterValue?.column
? { ...column._style, backgroundColor: '#badce3' }
: { ...column._style, backgroundColor: null };
});
this.columns = [...this.columns]
or
this.columns = this.columns.map((column, index) => {
column._style = column.key === sorterValue?.column
? { ...column._style, backgroundColor: '#badce3' }
: { ...column._style, backgroundColor: null };
return column;
})
this.columns.forEach((column) => { column._style = column.key === sorterValue?.column ? { ...column._style, backgroundColor: '#badce3' } : { ...column._style, backgroundColor: null }; }); this.columns = [...this.columns]
Thank you.