Skip to content

Added a custom button method to datatables #104

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 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions src/Datatables/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

class Button
{
protected $class = '';
protected $color = 'default';
protected $href = '#';
protected $icon = '';
protected $label = '';
protected $attributes = [];
protected string $class = '';
protected string $color = 'default';
protected string $href = '#';
protected string $icon = '';
protected string $label = '';
protected array $attributes = [];
protected string $tooltip = '';

/**
* Instanciate a new button.
Expand All @@ -32,6 +33,23 @@ public static function add(string $label = ''): self
return new static($label);
}

/**
* Returns a custom button.
*
* @param string $route
* @param array|string $args
* @param string $tooltip
* @param string $color
* @param string $icon
* @param array $attributes
* @return string
*/
public static function custom(string $route, array|string $args = [], string $tooltip = '', string $color = 'default', string $icon = '', array $attributes = []): string
{
return self::add()->route($route, $args)->tooltip($tooltip)->color($color)->icon($icon)->attributes($attributes)->make();
}


/**
* Returns an edit button.
*
Expand Down Expand Up @@ -168,7 +186,7 @@ public function link(string $href): self
*/
public function make(): string
{
$str = '<a href="%s" class="btn btn-sm btn-%s ml-1%s" %s>%s%s</a>';
$str = '<a href="%s" title="%s" class="btn btn-sm btn-%s ml-1%s" %s>%s%s</a>';

if (! empty($this->label) && ! empty($this->icon)) {
$this->label = $this->label.' ';
Expand All @@ -182,6 +200,19 @@ public function make(): string
return sprintf('%s="%s"', $k, $this->attributes[$k]);
}, array_keys($this->attributes)));

return sprintf($str, $this->href, $this->color, $this->class, $attributes, $this->label, $this->icon);
return sprintf($str, $this->href, $this->tooltip, $this->color, $this->class, $attributes, $this->label, $this->icon);
}

/**
* Sets tooltip of button.
*
* @param string $tooltip
* @return $this
*/
public function tooltip(string $tooltip): self
{
$this->tooltip = $tooltip;

return $this;
}
}