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

AdminTools Reusable data-table #391

Open
goztrk opened this issue Oct 2, 2023 · 0 comments
Open

AdminTools Reusable data-table #391

goztrk opened this issue Oct 2, 2023 · 0 comments

Comments

@goztrk
Copy link
Contributor

goztrk commented Oct 2, 2023

Problem

#389 idea was good but too much to code and determine. We need to go in small steps. Starting with making easy to create data-tables without re-writing same code everywhere.

Solution

I would like to separate this into two:
First phase is backend utility: a function that will generate the response output from model.
Second phase is to create a dynamic React data-table that will generate the list from given information.

First phase: Backend utility

A single function that will return desired result

Function Name

build_table_data()

Function Parameters

request

Request object is needed to determine page, page size, sorting, filtering

model

Model object that needs to be filtered. Can pass initial filtering with like UserMode.objects.filter(is_active=True)

fields

A dict object with following format:

[
    { 'field_name': str, 'label': str, 'sortable': bool, 'filterable': bool, 'transform': Optional[Callable[[any, any], any] },
]

field_name key might have related fields like user.profile.email. It should resolve them.
transform is an optional key. if presented, it must be a function that takes a value and returns the transformed one. First parameter of transform is value itself, second parameter is the current row in case some other value is needed from same row object.

'options'

An options dict map that enables/disables data-table features:

{
    'sorting': bool,
    'filtering': bool,
    'page_size: int, # default to 20,
    'row_actions': Optional[List[
        { 'icon': str, 'link': str, 'title': str, dynamic: List[str] },
    ]],
}

row_actions is optional.
link should be in format of /path/to/somewhere/:some_field/:id.
dynamic will hold the field names used to build URL.

Return

The return must have following format:

{
    'options': {
        'sorting': bool,
        'filtering': bool,
        'columns': [
            { 'field_name': str, 'label': str, 'sortable': bool, 'filterable': bool },
        ],
        'row_actions': []
    },
    'page_size': int,
    'current_page': int,
    'total_pages': int,
    'total_records': int,
    'sort_by: str,
    'sort_direction; 'asc'|'desc'
    'data': []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant