GithubHelp home page GithubHelp logo

saade / filament-adjacency-list Goto Github PK

View Code? Open in Web Editor NEW
70.0 4.0 11.0 4.02 MB

A Filament package to manage adjacency lists (aka trees).

Home Page: https://filamentphp.com/plugins/saade-adjacency-list

License: MIT License

JavaScript 7.51% PHP 75.42% CSS 0.05% Blade 17.01%
adjacency-list filament form-builder laravel tree

filament-adjacency-list's Introduction

Filament Adjacency List

Latest Version on Packagist Total Downloads

A Filament package to manage adjacency lists (aka trees).

Banner

Installation

You can install the package via composer:

composer require saade/filament-adjacency-list

Usage

use Saade\FilamentAdjacencyList\Forms\Components\AdjacencyList;

AdjacencyList::make('subjects')
    ->form([
        Forms\Components\TextInput::make('label')
            ->required(),
    ])

Configuration

Customizing the label key used to display the item's label

AdjacencyList::make('subjects')
    ->labelKey('name')          // defaults to 'label'

Customizing the children key used to gather the item's children.

Note: This is only used when not using relationships.

AdjacencyList::make('subjects')
    ->childrenKey('children')   // defaults to 'children'

Customizing the MaxDepth of the tree.

AdjacencyList::make('subjects')
    ->maxDepth(2)               // defaults to -1 (unlimited depth)

Creating items without a modal.

AdjacencyList::make('subjects')
    ->modal(false)      // defaults to true

Disabling creation, edition, deletion, and reordering.

AdjacencyList::make('subjects')
    ->addable(false)
    ->editable(false)
    ->deletable(false)
    ->reorderable(false)

Customizing actions

use Filament\Forms\Actions\Action;

AdjacencyList::make('subjects')
    ->addAction(fn (Action $action): Action => $action->icon('heroicon-o-plus')->color('primary'))
    ->addChildAction(fn (Action $action): Action => $action->button())
    ->editAction(fn (Action $action): Action => $action->icon('heroicon-o-pencil'))
    ->deleteAction(fn (Action $action): Action => $action->requiresConfirmation())
    ->reorderAction(fn (Action $action): Action => $action->icon('heroicon-o-arrow-path-rounded-square'))

Important

Reorder Action

If you want to add ->extraAttributes() to the action, you need to add the 'data-sortable-handle' => 'true' to the array, as the action serves as a handle for SortableJS.

By default, clicking on the action will do anything. If you want to trigger some action on click, you need to chain ->livewireClickHandlerEnabled() on the action.

Relationships

In this example, we'll be creating a Ticketing system, where tickets can be assigned to a department, and departments have subjects.

Building the relationship

// App/Models/Department.php

class Department extends Model
{
    public function subjects(): HasMany
    {
        return $this->hasMany(Subject::class)->whereNull('parent_id')->with('children')->orderBy('sort');
    }
}
// App/Models/Subject.php

class Subject extends Model
{
    protected $fillable ['parent_id', 'name', 'sort']; // or whatever your columns are

    public function children(): HasMany
    {
        return $this->hasMany(Subject::class, 'parent_id')->with('children')->orderBy('sort');
    }
}

Now you've created a nested relationship between departments and subjects.

Using the relationship

// App/Filament/Resources/DepartmentResource.php

AdjacencyList::make('subjects')
    ->relationship('subjects')          // Define the relationship
    ->labelKey('name')                  // Customize the label key to your model's column
    ->childrenKey('children')           // Customize the children key to the relationship's method name
    ->form([                            // Define the form
        Forms\Components\TextInput::make('name')
            ->label(__('Name'))
            ->required(),
    ]);

That's it! Now you're able to manage your adjacency lists using relationships.

Working with Staudenmeir's Laravel Adjacency List

This package also supports Staudenmeir's Laravel Adjacency List package.

First, install the package:

composer require staudenmeir/laravel-adjacency-list:"^1.0"
  1. Use the HasRecursiveRelationships trait in your model, and override the default path separator.
// App/Models/Department.php

class Department extends Model
{
    use \Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;

    public function getPathSeparator()
    {
        return '.children.';
    }
}

If you're already using the HasRecursiveRelationships trait for other parts of your application, it's probably not a good idea to change your model's path separator, since it can break other parts of your application. Instead, you can add as many path separators as you want:

class Department extends Model
{
    use \Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;

    public function getCustomPaths()
    {
        return [
            [
                'name' => 'tree_path',
                'column' => 'id',
                'separator' => '.children.',
            ],
        ];
    }
}
  1. Use the relationship method to define the relationship:
AdjacencyList::make('subdepartments')
    ->relationship('descendants')   // or 'descendantsAndSelf', 'children' ...
    ->customPath('tree_path')       // if you're using custom paths

That's it! Now you're able to manage your adjacency lists using relationships.

Customizing the query

AdjacencyList::make('subdepartments')
    ->relationship('descendants', fn (Builder $query): Builder => $query->where('enabled', 1))

Ordering

If your application needs to order the items in the list, you can use the orderColumn method:

AdjacencyList::make('subdepartments')
    ->orderColumn('sort')   // or any other column

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

Sponsor Saade

filament-adjacency-list's People

Contributors

atmonshi avatar dependabot[bot] avatar github-actions[bot] avatar iotron avatar ribesalexandre avatar saade avatar vanhooff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

filament-adjacency-list's Issues

[Bug]: Error when using form on widget

What happened?

I want to display adjecency list as widget on top of list page but when I try pass data from page table to widget it seems error or not working

How to reproduce the bug

I make widget resource and put it on list page and try to access data from widget with dd it seems appear but when I try to fill the form then this error happend foreach() argument must be of type array object null given

Package Version

3.1

PHP Version

8.1.17

Laravel Version

10.10

Which operating systems does with happen with?

Windows

Notes

No response

[Bug]: When you move items they disappear

What happened?

You want to change the level of an item or a group

How to reproduce the bug

You have a list of items when you edit the resource, you move the item/group to root or another level

11-09-2023_14-47-31.mp4

Package Version

3.x

PHP Version

8.2.0

Laravel Version

10.22.0

Which operating systems does with happen with?

Linux

Notes

No response

[Bug]: Cannot Reorder, Undefined array key "children"

What happened?

I cannot reorder my items in the list, or make an item a subitem. I can create a child perfectly fine though.

The data in the database is this:
{"f692b5e5-2cc9-41ad-9b5a-79ea86d1f0b4":{"label":"lol","children":[],"url":null},"4a463f2d-e261-4fb7-bb1b-f130c9cdea64":{"label":"lol2","children":[],"url":null}}

I am using this form on a settings page (package)

How to reproduce the bug

No idea to be honest. It might be because i am doing it in a settings page.

Package Version

3.1.0

PHP Version

8.1

Laravel Version

10

Which operating systems does with happen with?

Windows

Notes

No response

[Bug]: Overrides standard components in Filament

What happened?

Our ordinary classes like buttons is overrided with white background with this plugin.

How to reproduce the bug

When I install the plugin

Package Version

3.1.0

PHP Version

8.1

Laravel Version

10

Which operating systems does with happen with?

No response

Notes

No response

[Bug]: Throwing errors when not using relationships

What happened?

Upgraded the package from 3.1.2 -> 3.2.0 and now List is throwing errors about releationships and missing traits.

Adding the trait to my model, however causes the form to break, since it is looking for a relationship on the AdjacencyList component.

https://flareapp.io/share/NPLWWAQm

How to reproduce the bug

AdjacencyList::make('items')
      ->columnSpanFull()
      ->form([
          Forms\Components\TextInput::make('label')
              ->required(),
          Forms\Components\TextInput::make('url')
              ->required(),
          Forms\Components\Select::make('target')
              ->options(...),
          Forms\Components\CheckboxList::make('rel')
              ->columnSpan(1)
              ->columns(3)
              ->options(...,
      ]),

Package Version

v3.2.0

PHP Version

8.2

Laravel Version

10.0

Which operating systems does with happen with?

macOS

Notes

No response

[Bug]: Error in create resource

What happened?

Add in form

public static function form(Form $form): Form
    {
        return $form
            ->schema([
                AdjacencyList::make('Subjects')
                    ->form([
                        Forms\Components\TextInput::make('name')
                            ->required(),
                    ])
                ->labelKey('name')
            ]);
    }

image

image

image

Illuminate\Database\Grammar::parameterize(): Argument #1 ($values) must be of type array, string given, called in /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 1040

How to reproduce the bug

Create new resource

Add items

Save resource

Package Version

3.0.3

PHP Version

8.2.9

Laravel Version

10.20.0

Which operating systems does with happen with?

Linux

Notes

No response

[Bug]: Modal disappers when adding child item

What happened?

While adding child items to a primary node, the modal closes unexpectedly but the modal backdrop remains.

How to reproduce the bug

Install the plugin. Add first item with at least one child item. Then, create another top node item and attempt to add a child item to the new node.

adjacency-list-bug.mov

Package Version

3.x

PHP Version

8.1

Laravel Version

10

Which operating systems does with happen with?

macOS

Notes

No response

[Bug]: Actions are not configurable

What happened?

Actions are not configurable

How to reproduce the bug

When trying to configure actions, such as the edit action, it doesn't seem to work.

For example, using:

AdjacencyList::make('descendants')
    ->label('Subcategories')
    ->editAction(fn(Action $action) => $action->slideOver())
    ->labelKey('name')
    ->relationship('descendants')
    ->form([
        Forms\Components\TextInput::make('name')
            ->required(),
    ]),

Doesn't seem to apply the slideOver() method correctly. However, if you manually add slideOver() to the action in the source code, it works, indicating that something is off with how the configurations are passed through. This issue occurs for all actions.

Here's the relevant source code:

public function getEditAction(): Actions\Action
{
    $action = Actions\EditAction::make()->slideOver(); // Testing here works
    if ($this->modifyEditActionUsing) {
        $action = $this->evaluate($this->modifyEditActionUsing, [
            'action' => $action,
        ]) ?? $action;
    }
    return $action;
}

It seems the configuration isn't being applied as expected when using the method to modify the edit action.

Package Version

v1.22

PHP Version

v8.3.4

Laravel Version

v11.9.2

Which operating systems does with happen with?

macOS

Notes

No response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.