Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimonka2 committed Jan 31, 2020
2 parents 5cd904f + 4a6c7c9 commit 2225703
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 37 deletions.
49 changes: 41 additions & 8 deletions src/Form/Components/Datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ class Datatable extends ElementContainer
public $options;
public $js_variable;
public $ajax_data_function;
public $details;
public $ajax_details_url;
public $ajax_details_function;
public $details_format;
protected $details;
protected $colDefinition; // collection of DTColumn objects
protected $null_last;
protected $formatFunction;
Expand All @@ -31,6 +28,10 @@ protected function read(array $element)
{
$columns = self::readSingleSetting($element, 'columns');
$this->createColumns($columns ?? []);

$details = self::readSingleSetting($element, 'details');
if(is_array($details)) $this->createDetails($details);

$this->readSettings($element, [
'ajax_url',
'ajax_dataType',
Expand All @@ -41,10 +42,6 @@ protected function read(array $element)
'ajax_data_function',
'null_last',
'formatFunction',
'details',
'ajax_details_url',
'ajax_details_function',
'details_format',
]);
parent::read($element);
$this->requireID();
Expand All @@ -57,6 +54,10 @@ protected function createColumns(array $columns)
$this->addColumn($column);
}
}
protected function createDetails(array $details)
{
$this->details = new DatatableDetails($details, $this->context);
}

protected function render()
{
Expand Down Expand Up @@ -180,4 +181,36 @@ public function processAJAX(Request $request, $query)
return DatatableAjax::process($request, $this, $query);
}


/**
* Get the value of details
*/
public function getDetails()
{
return $this->details;
}

public function hasDetails()
{
return is_object($this->details);
}

/**
* Get the value of ajax_method
*/
public function getAjaxMethod()
{
return $this->ajax_method;
}

/**
* Set the value of ajax_url
*
* @return self
*/
public function setAjaxUrl($ajax_url)
{
$this->ajax_url = $ajax_url;
return $this;
}
}
41 changes: 41 additions & 0 deletions src/Form/Components/DatatableDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace dimonka2\flatform\Form\Components;

use dimonka2\flatform\Form\Contracts\IContext;
use dimonka2\flatform\Form\Element;

class DatatableDetails extends Element
{
public const default_class = "dt-details";
public const default_data_id = "id";
public $url;
public $data_definition;
public $format_function;
public $column_data;
public $data_id;
protected $ajax_method;

protected function read(array $element)
{
$this->readSettings($element, [
'url',
'data_definition', // 'article_id: rowData.id,'
'format_function', // myFormat(rowData);
'ajax_method',
'column_data',
'data_id',
]);
parent::read($element);
if(!$this->class ) $this->class = self::default_class;
if(!$this->data_id ) $this->data_id = self::default_data_id;
}

/**
* Get the value of ajax_method
*/
public function getAjaxMethod()
{
return $this->ajax_method ? $this->ajax_method : $this->parent->getAjaxMethod();
}
}
4 changes: 3 additions & 1 deletion src/Helpers/DatatableAjax.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function process(Request $request, Datatable $table, $query)


if ($request->has('order.0.column')) {
$orderColumn = $fields[$request->input('order.0.column') - ($table->details ? 1 : 0)];
$orderColumn = $fields[$request->input('order.0.column') - ($table->hasDetails() ? 1 : 0)];
// protect from errors
if($orderColumn->getSort() !== false && !$orderColumn->getSystem()) {
$orderDir = $request->input('order.0.dir');
Expand Down Expand Up @@ -90,6 +90,8 @@ public static function process(Request $request, Datatable $table, $query)
$nestedData[ $field ] = $table->format($value, $item, $column);
} else $nestedData[ $field ] = $value;
}
if($table->hasDetails()) $nestedData['DT_RowId'] =
$table->id . '-' . $nestedData[$table->getDetails()->data_id];
$data[] = $nestedData;
}
// $this->formatJSON($items, $table->getColDefinition());
Expand Down
56 changes: 28 additions & 28 deletions views/datatable.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,53 @@

<script type="text/javascript">
@if($element->details)
@if($element->hasDetails())
@php($details = $element->getDetails())
function format_{{$element->id}}( rowData ) {
@if($element->details_format)
{!! $element->details_format !!}
@if($details->format_function)
{!! $details->format_function !!}
@endif
@if($element->ajax_details_url)
var div = $('<div/>')
.addClass( 'loading' )
.text( 'Loading...' );
@if($details->url)
var div = $('<div/>').addClass( 'loading' ).text( 'Loading...' );
$.ajax( {
url: '{{ $element->ajax_details_url }}',
data: function ( d ) {
d._token = "{{csrf_token()}}";
{{$element->ajax_details_function ?? ''}}
url: '{{ $details->url }}',
data: {
'_token': "{{csrf_token()}}",
'{{$details->data_id}}' : rowData.{{$details->data_id}},
{{$details->data_definition ?? ''}}
},
"dataType": "json",
"type": "{{ $element->ajax_method ?? 'GET' }}",
"method": "{{ $details->getAjaxMethod() }}",
success: function ( json ) {
div
.html( json.html )
.removeClass( 'loading' );
div.html( json.html ).removeClass( 'loading' );
}
} );
@endif
return div;
}
var detailRows = [];
var detailRows_{{Str::camel($element->id)}} = [];
function bindDetails() {
$('#{{$element->id}} tbody tr').on( 'click', 'td.{{$element->details}}', function () {
$('#{{$element->id}} tbody tr').on( 'click', 'td.{{trim($details->class)}}', function () {
var dt = $('#{{$element->id}}').DataTable();
var tr = $(this).closest('tr');
var row = dt.row( tr );
var idx = $.inArray( tr.attr('id'), detailRows );
var idx = $.inArray( tr.attr('id'), detailRows_{{Str::camel($element->id)}} );
if ( row.child.isShown() ) {
tr.removeClass( 'details' );
row.child.hide();
// Remove from the 'open' array
detailRows.splice( idx, 1 );
detailRows_{{Str::camel($element->id)}}.splice( idx, 1 );
}
else {
tr.addClass( 'details' );
row.child( format_{{$element->id}}( row.data() ) ).show();
// Add to the 'open' array
if ( idx === -1 ) {
detailRows.push( tr.attr('id') );
detailRows_{{Str::camel($element->id)}}.push( tr.attr('id') );
}
}
} );
Expand Down Expand Up @@ -84,7 +82,7 @@ function bindDetails() {
columnDefs: [
@foreach ($element->getColDefinition() as $column)
{targets: [ {{$loop->index + ($element->details ? 1 : 0) }} ]
{targets: [ {{$loop->index + (isset($details) ? 1 : 0) }} ]
{!! $column->formatColumnDefs() !!} },
@endforeach
],
Expand All @@ -98,12 +96,14 @@ function bindDetails() {
}
},
"columns": [
@if($element->details)
@if($element->hasDetails())
{
className: '{{$element->details}}',
className: '{{trim($details->class)}}',
orderable: false,
data: null,
defaultContent: ''
data: '',
defaultContent: {!! $details->column_data ?
$details->column_data :
'"<button class=\'btn btn-sm btn-clean btn-icon btn-icon-md p-1\'><i class=\'fa fa-caret-down\'></i></button>"' !!}
},
@endif
@foreach ($element->getColDefinition() as $column)
Expand All @@ -113,12 +113,12 @@ className: '{{$element->details}}',
});
@if($element->details)
@if($element->hasDetails() )
var dt = $('#{{$element->id}}').DataTable();
dt.on('draw', function () {
bindDetails();
$.each( detailRows, function ( i, id ) {
$('#'+id+' td.details-control').trigger( 'click' );
$.each( detailRows_{{Str::camel($element->id)}}, function ( i, id ) {
$('#'+id+' td.{{trim($details->class)}}').trigger( 'click' );
} );
});
@endif
Expand Down

0 comments on commit 2225703

Please sign in to comment.