Skip to content

Commit

Permalink
v0.9.006
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwijaya-dev committed Jul 11, 2021
1 parent ab29e4a commit a151393
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 43 deletions.
10 changes: 10 additions & 0 deletions src/Http/Controllers/ActionableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Andiwijaya\WebApp\Exceptions\KnownException;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;
use function foo\func;

Expand Down Expand Up @@ -81,4 +82,13 @@ public function alertRequest(Request $request){

return htmlresponse()->alert(json_encode($request->all(), JSON_PRETTY_PRINT));
}

public function validate(Request $request, array $rules, array $messages = [], array $customAttributes = [])
{
$validator = Validator::make($request->all(), $rules, $messages, $customAttributes);

if($validator->fails()){
exc(implode("<br />\n", $validator->errors()->all()));
}
}
}
117 changes: 79 additions & 38 deletions src/Http/Controllers/TableView1Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TableView1Controller extends ActionableController
protected $extends;
protected $title;
protected $model;
protected $id;
protected $columns = [
[ 'text'=>'', 'name'=>'options', 'width'=>50 ],
[ 'text'=>'Name', 'name'=>'name', 'width'=>260, 'filterable'=>[ 'type'=>'string' ], 'sortable'=>true ],
Expand All @@ -35,6 +36,7 @@ public function view(Request $request)
View::share([
'extends'=>$this->extends,
'title'=>$this->title,
'id'=>$this->id,
'column_html'=>$this->renderHeader()
]);

Expand All @@ -47,51 +49,18 @@ public function load(Request $request)

$html = [];
foreach($data as $obj){

$id = $obj['id'] ?? '';

$tag = "<tr data-id=\"{$id}\">";
foreach($this->columns as $column){

$name = $column['name'] ?? '';
$text = $obj[$name] ?? '';
$datatype = $column['datatype'] ?? 'text';
$align = $column['align'] ?? '';
$class = $column['class'] ?? '';

switch($datatype){
case 'number':
$text = number_format(doubleval($text));
if(!$align) $align = 'align-right';
break;

case 'datetime':
$text = date('j M Y H:i', strtotime($text));
break;
}

$tag .= "<td class='{$align}'>";
if(method_exists($this, ($method = 'column' . ucwords(Str::camel($name))))){
$tag .= $this->$method($obj, $column);
}
else{
$tag .= "<label class=\"ellipsis {$class}\">{$text}</label>";
}
$tag .= "</td>";
}
$tag .= "</tr>";

$html[] = $tag;

$html[] = $this->renderItem($obj);
}
$html = implode('', $html);

$response = htmlresponse();

if($page <= 1)
$response->value('#tableview1', $html, [ 'next_page'=>$next ])
->html('#tableview1 .table-foot', $this->renderFooter($builder));
$response->value("#{$this->id}", $html, [ 'next_page'=>$next ])
->html("#{$this->id} .table-foot", $this->renderFooter($builder));
else
$response->append('#tableview1', $html, [ 'next_page'=>$next ]);
$response->append("#{$this->id}", $html, [ 'next_page'=>$next ]);

return $response;
}
Expand Down Expand Up @@ -235,6 +204,12 @@ protected function renderHeader()
$sortable = $column['sortable'] ?? false;

switch($datatype){

case 'bool':
case 'boolean':
if(!$align) $align = 'align-center';
break;

case 'number':
if(!$align) $align = 'align-right';
break;
Expand All @@ -251,6 +226,65 @@ protected function renderHeader()

return implode('', $html);
}

protected function renderItem($obj){

$id = $obj['id'] ?? '';
$tag = "<tr data-id=\"{$id}\">";
foreach($this->columns as $column){

$name = $column['name'] ?? '';
$text = $obj[$name] ?? '';
$datatype = $column['datatype'] ?? 'text';
$align = $column['align'] ?? '';
$class = $column['class'] ?? '';

switch($datatype){

case 'bool':
case 'boolean':
if(!$align) $align = 'align-center';
break;

case 'number':
$text = number_format(doubleval($text));
if(!$align) $align = 'align-right';
break;

case 'date':
$text = date('j M Y', strtotime($text));
break;

case 'datetime':
$text = date('j M Y H:i', strtotime($text));
break;
}

$tag .= "<td class='{$align}'>";
switch($datatype){

case 'bool':
case 'boolean':
if($text)
$tag .= "<label class='ellipsis'><span class='fa fa-check-circle cl-green'></span></label>";
else
$tag .= "<label class='ellipsis'><span class='fa fa-minus-circle cl-gray-500'></span></label>";
break;

default:
if(method_exists($this, ($method = 'column' . ucwords(Str::camel($name))))){
$tag .= $this->$method($obj, $column);
}
else{
$tag .= "<label class=\"ellipsis {$class}\">{$text}</label>";
}
}
$tag .= "</td>";
}
$tag .= "</tr>";

return $tag;
}

protected function renderFooter($builder)
{
Expand Down Expand Up @@ -363,4 +397,11 @@ protected function addFilter(Request $request)
// { name: "or|contains|123", "|between|2012-04-01,2021-04-22" }}

}

public function __construct()
{
if(!$this->id) $this->id = 'tableview1';

parent::__construct();
}
}
10 changes: 8 additions & 2 deletions src/Responses/HTMLResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public function prepend($target, $html, $options = []){
return $this;
}

public function close($target, array $params = []){

$this->data[] = [ '_type'=>'method', 'method'=>'close', 'target'=>$target, 'params'=>$params ];
return $this;
}

public function html($target, $html, $options = []){

$this->data[] = [ '_type'=>'html', 'html'=>$html, 'target'=>$target, 'options'=>$options ];
Expand All @@ -44,13 +50,13 @@ public function replace($target, $html){
return $this;
}

public function replaceOrAppend($target, $html, $options)
public function replaceOrAppend($target, $html, $options = [])
{
$this->data[] = [ '_type'=>'html', 'html'=>$html, 'mode'=>'replace-or-append', 'target'=>$target, 'options'=>$options ];
return $this;
}

public function replaceOrPrepend($target, $html, $options)
public function replaceOrPrepend($target, $html, $options = [])
{
$this->data[] = [ '_type'=>'html', 'html'=>$html, 'mode'=>'replace-or-prepend', 'target'=>$target, 'options'=>$options ];
return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ function in_array_all($needles, $haystack) {
function save_image($image, $disk = 'images', $dir = '', $urlprefix = ''){

$file_md5 = '';

if(filter_var($image, FILTER_VALIDATE_URL)){

$image_params = getimagesize($image);
Expand Down
4 changes: 2 additions & 2 deletions src/views/tableview1.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
</div>

<div class="my-2">
<div data-type="table" id="tableview1" data-event data-init-click="[value=load]">
<div data-type="table" id="{{ $id }}" data-event data-init-click="[value=load]">
<div class="table-head">
<table>
<thead>
Expand All @@ -61,7 +61,7 @@
</thead>
</table>
</div>
<div class="table-body v-scrollable h-60h"></div>
<div class="table-body v-scrollable h-60h" data-event data-scroll-check-appear></div>
<div class="table-foot"></div>
</div>
</div>
Expand Down

0 comments on commit a151393

Please sign in to comment.