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

feature: CRUD example table #923

Open
markdoerr opened this issue Jul 4, 2023 · 1 comment
Open

feature: CRUD example table #923

markdoerr opened this issue Jul 4, 2023 · 1 comment

Comments

@markdoerr
Copy link

Hi,

how would I do some edit/ updates / delete on a column with nice bootstrap icons in one columns ?
Could you please add an example, where it is shown how to use the new linkify feature to do update / edit / delete operations on a certain column of a table - including the corresponding view ?

Thanks a lot for you help !

@Alexandre-petitjean
Copy link

A quick solution (not perfect at all) is to make a custom Column. The column get the data for the actions from a method in the object class.

First the table definition :

class ConsumableTable(tables.Table):
    actions = ActionsColumn(accessor="get_actions", verbose_name="Actions")

Then the Column definition

class ActionsColumn(tables.Column):

    def __init__(self, *args, **kargs):
        super().__init__(*args, **kargs)
        self.orderable = False

    def render(self, value):
        result = '<div class="d-flex flex-row justify-content-evenly">'
        for k, v in value.items():
            match k:
                case "detail":
                    result += f'<a href="{v}"><i class="bi bi-eye"></i></a>'
                case "edit":
                    result += f'<a href="{v}"><i class="bi bi-pencil"></i></a>'
                case "delete":
                    # delete is the only action to have a list in value.
                    result += f'<a href="#" class="btn-delete-{v[0]}" data-form-url="{v[1].get_delete_url()}" data-id="{v[1].id}" data-extra="{v[1]}"><i class="bi bi-trash"></i></a>'
        return format_html(result + "</div>")

And the data provider for the column

    def get_actions(self):
        return {
            "detail": self.get_absolute_url(),
            "edit": self.get_edit_url(),
            "delete": ["consumable", self]
        }

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

2 participants