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

How do I add a new method to Encore\Admin\Grid\Column ? #5860

Open
jicksonjohnson opened this issue Jul 9, 2024 · 1 comment
Open

How do I add a new method to Encore\Admin\Grid\Column ? #5860

jicksonjohnson opened this issue Jul 9, 2024 · 1 comment

Comments

@jicksonjohnson
Copy link

  • Laravel Version: 8.12
  • PHP Version: 8.0.28
  • Laravel-admin: 1.8.16

Description:

I am trying to add a new column filter. To achieve this, I need to place a new method similar to "filter" that exists in "Encore\Admin\Grid\Column". Since this is part of a vendor package, I cannot edit it directly. Therefore, my plan is to rewrite this vendor class. I tried using the following code in "AdminServiceProvider". This is a new ServiceProvider I added under config/app.php to make this change.

public function boot() { Admin::booting(function () { $this->app->singleton(\Encore\Admin\Grid\Column::class, \App\Admin\Extensions\Override\Grid\Column::class); }); }

Here my try is to rewrite vendor class method "filter"

`<?php

namespace App\Admin\Extensions\Override\Grid;

use Encore\Admin\Grid\Column as OriginalColumn;

class Column extends OriginalColumn
{
/**
* Set column filter.
*
* @param mixed|null $builder
*
* @return $this
*/
public function filter($builder = null)
{
dd('sss');
return $this->addFilter(...func_get_args());
}
}`

I do not see this change reflected. Could someone help me with rewriting this vendor class?

@jicksonjohnson
Copy link
Author

I need to keep the column filter open, like below

Screenshot 2024-07-09 at 5 51 20 PM

I did this by editing the vendor files.

  1. Edited "Encore\Admin\Grid\Column" class by adding new method called "open_filter"

/** * Set column open filter. * * @param mixed|null $builder * * @return $this */ public function open_filter($builder = null) { return $this->addOpenFilter(...func_get_args()); }

2. Inside the trait "HasHeader", added "addOpenFilter" method

`protected function addOpenFilter($type = null, $formal = null)
{
    if (is_array($type)) {
        return $this->addHeader(new CheckOpenFilter($type));
    }

    if (is_null($type)) {
        $type = 'equal';
    }

    if (in_array($type, ['equal', 'like', 'date', 'time', 'datetime'])) {
        return $this->addHeader(new InputOpenFilter($type));
    }

    if ($type === 'range') {
        if (is_null($formal)) {
            $formal = 'equal';
        }

        return $this->addHeader(new RangeOpenFilter($formal));
    }

    return $this;
}`

3. included CheckOpenFilter, InputOpenFilter and RangeOpenFilter classes to the folder. those actually a copy of InputFilter, CheckFilter and RangeFilter. The only change is below

Updated the inline styling of ul dropdown-menu class element with below 

`style="padding: 10px;box-shadow: 0 2px 3px 0 rgba(0,0,0,.2);left: -5px;border-radius: 0; display: block; position: relative;"` display and position updated.

Now, I achieved this by editing vendor files. However, this approach is not allowed and is generally considered a bad practice to meet this requirement. As I mentioned in my question, I was unable to achieve the same result using the rewrite method.

If anyone knows of a better approach to accomplish this without editing the vendor files, please advise.

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