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

Add onclick to button field #12

Merged
merged 1 commit into from
Sep 2, 2019
Merged
Show file tree
Hide file tree
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
36 changes: 28 additions & 8 deletions src/Fields/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,21 @@ class Button extends Field
* @var array
*/
public $target;

/**
* Set the button label
*
* @var string
*/
public $label;


/**
* Set onclick method
*
* @var string
*/
public $onClick;

/**
* Init the fields class
*
Expand All @@ -79,7 +86,7 @@ public static function make(string $column)
$class = new Button;
$class->column = $column;
$class->id = uniqid('button_');

return $class;
}

Expand Down Expand Up @@ -111,10 +118,10 @@ public function route(string $route, $parameters)
foreach($params as $index => $param){

$this->routeReplace["Q{$param}Q"] = $param;

$params[$index] = "Q{$param}Q";
}

$this->route = route($route, $params);

return $this;
Expand Down Expand Up @@ -145,17 +152,30 @@ public function target(string $target)

return $this;
}

/**
* Set the button label
*
*
* @param string $label
* @return $this
*/
public function label(string $label)
{
$this->label = $label;


return $this;
}

/**
* Set the onclick method
*
* @param string $onClick
* @return $this
*/
public function onclick(string $onClick)
{
$this->onClick = $onClick;

return $this;
}
}
6 changes: 3 additions & 3 deletions src/resources/views/fields/button.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
@else
location.href = $(e.currentTarget).data('route');
@endif

});
let template = `
<button title="{{ $class->title['title'] }}" data-toggle="{{ $class->title['toggle'] }}" type="button" ${route} id="${id}" class="{{ $class->class }}">{{ $class->label }} {!! $class->icon !!}</button>
<button title="{{ $class->title['title'] }}" data-toggle="{{ $class->title['toggle'] }}" onclick="{{ $class->onClick }}" type="button" ${route} id="${id}" class="{{ $class->class }}">{{ $class->label }} {!! $class->icon !!}</button>
`;
@elseif(in_array($class->method, ['POST', 'DELETE', 'PUT', 'PATCH']))
let template = `
<button title="{{ $class->title['title'] }}" data-toggle="{{ $class->title['toggle'] }}" onclick="$('#form${id}').submit()" type="button" id="${id}" class="{{ $class->class }}">{{ $class->label }} {!! $class->icon !!}</button>
<button title="{{ $class->title['title'] }}" data-toggle="{{ $class->title['toggle'] }}" onclick="{{ $class->onClick != "" ? $class->onClick : '$("#form${id}").submit();' }}" type="button" id="${id}" class="{{ $class->class }}">{{ $class->label }} {!! $class->icon !!}</button>
wimurk marked this conversation as resolved.
Show resolved Hide resolved
<form class="laravel-datatable-form-{{ strtolower($class->method) }}" style="display:none;" method="post" id="form${id}" action="${url}">@csrf @method($class->method)</form>
`;
@endif
Expand Down