Skip to content

Commit c635d48

Browse files
committed
feat: add ability to set cusom css classes to columns
1 parent 25ef23a commit c635d48

5 files changed

Lines changed: 42 additions & 4 deletions

File tree

adminforth/documentation/docs/tutorial/03-Customization/13-standardPagesTuning.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ export default {
142142

143143
>⚠️ Please note that sticky columns can only be applied to one column per resource.
144144
145+
### Custom list column class
146+
147+
You can add a custom CSS class to any list column with `listCssClass`. AdminForth applies it to both the header cell and the data cells for that column.
148+
149+
```typescript title="./resources/apartments.ts"
150+
export default {
151+
resourceId: 'aparts',
152+
...
153+
columns: [
154+
{
155+
name: "price",
156+
listCssClass: "text-right font-semibold min-w-10",
157+
},
158+
...
159+
]
160+
}
161+
```
162+
145163
### Conditional display
146164
You can conditionally display columns in forms and views based on the values of other fields in the current record using the `showIf` property. This enables dynamic layouts that automatically adapt to user input, creating more intuitive and context-aware interfaces.
147165

adminforth/spa/src/components/ResourceListTable.vue

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@
3131
</Checkbox>
3232
</td>
3333

34-
<td v-for="c in columnsListed" ref="headerRefs" scope="col" class="list-table-header-cell px-2 md:px-3 lg:px-6 py-3" :class="{'sticky-column bg-lightListTableHeading dark:bg-darkListTableHeading': c.listSticky}">
34+
<td
35+
v-for="c in columnsListed"
36+
ref="headerRefs"
37+
scope="col"
38+
class="list-table-header-cell px-2 md:px-3 lg:px-6 py-3"
39+
:class="[c.listCssClass, {'sticky-column bg-lightListTableHeading dark:bg-darkListTableHeading': c.listSticky}]"
40+
>
3541

3642
<div @click="(evt) => c.sortable && onSortButtonClick(evt, c.name)"
3743
class="flex items-center font-semibold" :class="{'cursor-pointer':c.sortable}">
@@ -122,7 +128,11 @@
122128
</Checkbox>
123129
</td>
124130

125-
<td v-for="c in columnsListed" class="px-2 md:px-3 lg:px-6 py-4" :class="{'sticky-column bg-lightListTable dark:bg-darkListTable': c.listSticky}">
131+
<td
132+
v-for="c in columnsListed"
133+
class="px-2 md:px-3 lg:px-6 py-4"
134+
:class="[c.listCssClass, {'sticky-column bg-lightListTable dark:bg-darkListTable': c.listSticky}]"
135+
>
126136
<!-- if c.name in listComponentsPerColumn, render it. If not, render ValueRenderer -->
127137
<component
128138
:is="c?.components?.list ? getCustomComponent(typeof c.components.list === 'string' ? { file: c.components.list } : c.components.list) : ValueRenderer"
@@ -835,4 +845,4 @@ tr.list-table-body-row:not(:first-child):hover {
835845
}
836846
}
837847
838-
</style>
848+
</style>

adminforth/types/Common.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,11 @@ export interface AdminForthResourceColumnInputCommon {
10041004
*/
10051005
listSticky?: boolean;
10061006

1007+
/**
1008+
* Custom CSS class applied to the column in list view header and cells.
1009+
*/
1010+
listCssClass?: string;
1011+
10071012
/**
10081013
* Show field only if certain conditions are met.
10091014
*/

tests/application/resources/cars_sl_allow_create.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ export default {
1010
listTableClickUrl: async (record: any, _adminUser: any, resource: any) => `/resource/${resource.resourceId}/show/${record.id}`,
1111
},
1212
columns: [
13-
...cars_sl.columns.filter(c => c.name !== "mileage" && c.name !== "photos" && c.name !== "generated_promo_picture"),
13+
...cars_sl.columns
14+
.filter(c => c.name !== "mileage" && c.name !== "photos" && c.name !== "generated_promo_picture")
15+
.map((column) => column.name === 'model'
16+
? { ...column, listCssClass: 'text-lightPrimary dark:text-darkPrimary' }
17+
: column),
1418
{
1519
name: "mileage",
1620
type: "number",

tests/jest_tests/CRUD_sqlite.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,7 @@ describe('POST /get_resource_data', () => {
922922
expect(res.body.resource.table).toBeUndefined();
923923
expect(res.body.resource.dataSource).toBeUndefined();
924924
expect(res.body.resource.dataSourceColumns).toBeUndefined();
925+
expect(res.body.resource.columns.find((column: any) => column.name === 'model').listCssClass).toBe('text-lightPrimary dark:text-darkPrimary');
925926
});
926927
});
927928

0 commit comments

Comments
 (0)