Skip to content

[POC] datatable as a component for multiple cruds #5688

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

Open
wants to merge 24 commits into
base: next
Choose a base branch
from

Conversation

pxpm
Copy link
Contributor

@pxpm pxpm commented Oct 11, 2024

This is a very early POC about something me and @tabacitu are exploring.

What we are trying to achieve here, can be summed up in the following way:

  • re-usable Datatable
  • multiple CrudPanel instances in the same request.

They way Backpack was built from start, although simple, comes with some limitations. Now that we want to introduce more advanced features we'd need to do major refactors of the software, that would take months for us to develop, and a lot of time for our users to upgrade.

We are always exploring the possibility of changing something internal that would allow us to do more advanced things, without the cost for the developers that use our software.

That said, one of the most requested features and needs we had, was to have a "relation table", without having to develop a new table from scratch, given how much effort we already spent on the Datatable/columns, and the huge amount of features we have on both of them.

Without much to add for the moment, we will need to decide on the direction we would like to take, here is a screenshot of somewhat we want to achieve:

image

All of that, with the simplicity we aim for our software:

// in MonsterCrudController.php
$this->crud->addColumn([
            'name'        => 'crudColumn',
            'type'        => 'crud_column',
            'controller'  => IconCrudController::class,
        ]);

// in IconCrudController.php
protected function setupListOperation()
    {
        $this->crud->addColumns(['name', 'icon']);
        $this->crud->addFilter([
            'type'  => 'text',
            'name'  => 'name',
            'label' => 'Name',
        ]);
    }

At the moment there are 0 breaking changes, but also stuff that does no properly work. This is very very wip, we may find something that would require a major breaking change that we are not willing to do for the next version .. but it's a start. 👍

@pxpm pxpm added the v7 label Oct 21, 2024
@pxpm pxpm force-pushed the datatable-single-component branch from d84d59a to 65aa57b Compare March 12, 2025 10:01
@pxpm pxpm changed the base branch from main to next March 12, 2025 10:01
@pxpm pxpm force-pushed the datatable-single-component branch from 39a4912 to d8b8d16 Compare March 24, 2025 15:17
Copy link
Member

@tabacitu tabacitu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's

  1. Polish with the comments here.
  2. Set up a demo
  • used a column in ShowOperation
  • used as a widget in ShowOperation
  • used in a dashboard as an x-component

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call this a datatable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->crud->addColumn([
            'name'        => 'crudColumn',
            'type'        => 'crud_column',
            'controller'  => IconCrudController::class,
            // OPTIONAL:
            // 'afterSetup' => function($crud) {
                $crud->addClause();
            }
        ]);

<div id="datatable_button_stack" class="float-right float-end text-right hidden-xs"></div>
</div>
@endif
<x-datatable :crud="$crud" />

</div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Todo: let's write in the upgrade guide that the list.blade.php and datatables_logic.blade.php files have been turned into a datatable component - if they've made any changes to them, they can delete them from their project and re-do the changes in the component view (after publishing).

}
@endif

window.crud = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to test two datatables on the same page.

@@ -0,0 +1,520 @@
@php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok let's do it. Let's clean up this file a bit - let's take it step-by-step. First: let's remove the PHP from this file.


$crud = $this->crud($controller);

$crud->setOperation('list');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚩 why hard-coded for the list operatin? In the future, we should be able to use BackpackManager to load a Form component, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe....

getOperationFromController(string $controller, string $operation)

Or maybe we move this line to a separate method?! 🤷‍♂️

return isset($this->cruds[$controller]);
}

public function getControllerCrud(string $controller): CrudPanel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's think more about the names 🤷‍♂️

@tabacitu tabacitu moved this to In Review in Backpack v7.0 Apr 3, 2025
@tabacitu tabacitu moved this from In Review to In Progress in Backpack v7.0 Apr 3, 2025
pxpm and others added 4 commits April 7, 2025 16:20
@@ -62,16 +62,19 @@ function updatePageUrl(filterName, filterValue, currentUrl = null) {
}

if(typeof updateDatatablesOnFilterChange !== 'function') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Todo: after the review, we should probably move this JS logic from the FILTERS blade file... to the DATATABLES blade file. Right? I mean... filters are a general-purpose feature... that is supported by whatever operation wants to support them.

DataTables - as a component and as an operation... chooses to support the filters. So the logic that says "when filter does X datatables does Y" should probably live in the DATATABLE, not in the FILTERS.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But right now it's ok here, just to keep the diff minimal. We can create a different PR that moves code around. Like a cleanup PR.

src/Backpack.php Outdated
@@ -0,0 +1,16 @@
<?php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Help me understand... what will this facade facilitate - what kind of calls? I'm wondering whether we need a Facade at all... or whether we should change its name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it to do stuff like Backpack::getActiveController and Backpack::getCrud()? If so... I think we're better of doing CrudManager::getActiveController() and CrudManager::getCrud().

I wouldn't expand this feature so much as to identify it with the Backpack name. It would prevent us from using the Backpack class for anything else in the future.

Backpack::setControllerCrud(get_class($this), $crudPanel);
}

private function triggerControllerHooks()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bad name for this function. Sure, it triggers the controller hooks... but that's just the SMALL part of what it does. What it does MORE IMPORTANTLY is to... setup the crud controller? O what is it? Cause I see it's calling the general-purpose setup methods like setupDefaults() and setup() but it's also calling setupConfigurationForCurrentOperation() which is an operation-thing.

Perhaps we call this method:

Suggested change
private function triggerControllerHooks()
private function setupCrudController()

If you don't have a better name for it.

LifecycleHook::trigger('crud:before_setup', [$this]);
$this->setup();
LifecycleHook::trigger('crud:after_setup', [$this]);
$this->initialized = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something's fishy here. I see that we have a function called initializeCrud() but we are also doing $this->initialized=true outside of it. That is fishy. How can we refactor this bit of code so it's cleaner?

Copy link
Member

@tabacitu tabacitu Apr 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a feeling we could/should move ALL of the logic in the constructor to the initializeCrud() method.

And maybe we call it initialize() - with an optional parameter. Since it sets the initialized property on the CrudController... it isn't initializing the CRUD... but the CrudController, right?

pxpm and others added 5 commits May 2, 2025 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

3 participants