Skip to content

Commit

Permalink
Some cleanup, some config options
Browse files Browse the repository at this point in the history
…and an example image for the readme.
  • Loading branch information
gbrock committed Mar 29, 2015
1 parent a3803d9 commit 162b62e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 14 deletions.
3 changes: 3 additions & 0 deletions config/tables.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

return [
'key_field' => 'sort',
'key_direction' => 'direction',
'default_direction' => 'asc',

];
Binary file added examples/images/basic_initialization.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
use Illuminate\Support\Facades\Input;

class Column {
/** @var Model The base model from which we can gather certain options */
protected $model;

/** @var string Applicable database field used in sorting */
protected $field;

Expand Down Expand Up @@ -58,6 +61,22 @@ public static function create()
return $class;
}

/**
* Sets some common-sense options based on the underlying data model.
*
* @param Model $model
*/
public function setOptionsFromModel($model)
{
if(in_array($this->getField(), $model->getSortable()))
{
// The model dictates that this column should be sortable
$this->setSortable(true);
}

$this->model = $model;
}

/**
* Checks if this column is currently being sorted.
*/
Expand All @@ -68,6 +87,12 @@ public function isSorted()
return true;
}

if(!Request::input('sort') && $this->model && $this->model->getSortingField() == $this->getField())
{
// No sorting was requested, but this is the default field.
return true;
}

return false;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Providers/TableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function boot()
$root . 'config/tables.php' => config_path('gbrock-tables.php'),
]);

// Merge user config, passing in our defaults
$this->mergeConfigFrom(
$root . 'config/tables.php', 'gbrock-tables'
);

// Publish assets
// $this->publishes([
// $root . 'build/assets' => public_path('vendor/gbrock/tables'),
Expand Down
16 changes: 9 additions & 7 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct($models = [], $columns = [])
if(!$columns)
{
// Columns were not passed; generate them
$columns = $this->getColumnsFromModels($models);
$columns = $this->getFieldsFromModels($models);
}

$this->setColumns($columns);
Expand All @@ -44,6 +44,7 @@ public function create($rows, $columns = false)
/**
* Add columns based on field names
* @param $columns
* @throws ColumnKeyNotProvidedException
*/
protected function addColumns($columns)
{
Expand Down Expand Up @@ -71,14 +72,11 @@ protected function addColumns($columns)

// Create the complex column
$new_column = Column::create($field, $field_parameters);
}

if(in_array($field, $model->getSortable()))
{
// The model dictates that this column should be sortable
$new_column->setSortable(true);
}

$new_column->setOptionsFromModel($model);

$this->columns[] = $new_column;
}
}
Expand All @@ -96,7 +94,11 @@ protected function getFieldsFromModels($models)
return [];
}

return array_keys($models->first()->toArray());
$fields = array_keys($models->first()->toArray());
$timestamp_fields = ['created_at', 'updated_at', 'deleted_at'];

// only those non-timestamp fields
return array_diff($fields, $timestamp_fields);
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/Traits/Sortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function scopeSorted($query, $field = false, $direction = false)
// If the direction requested isn't correct, assume ascending
if($direction !== 'asc' && $direction !== 'desc')
{
$direction = 'asc';
$direction = config('gbrock-tables.default_direction');
}

// At this point, all should be well, continue.
Expand All @@ -54,12 +54,12 @@ public function getSortable()
*
* @return string
*/
protected function getSortingField()
public function getSortingField()
{
if(Request::input('sort'))
if(Request::input(config('gbrock-tables.key_field')))
{
// User is requesting a specific column
return Request::input('sort');
return Request::input(config('gbrock-tables.key_field'));
}

// Otherwise return the primary key
Expand All @@ -74,14 +74,14 @@ protected function getSortingField()
*/
protected function getSortingDirection()
{
if(Request::input('direction'))
if(Request::input(config('gbrock-tables.key_direction')))
{
// User is requesting a specific column
return Request::input('direction');
return Request::input(config('gbrock-tables.key_direction'));
}

// Otherwise return the primary key
return 'asc';
return config('gbrock-tables.default_direction');
}
}

0 comments on commit 162b62e

Please sign in to comment.