Skip to content

Commit

Permalink
Improve automatic detection of searchable/orderable columns
Browse files Browse the repository at this point in the history
  • Loading branch information
curry684 committed Dec 1, 2017
1 parent 323870f commit ce07086
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/Adapter/Doctrine/ORM/AutomaticQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private function addSelectColumns(AbstractColumn $column, string $field)
$currentPart = array_shift($parts);
$currentAlias = ($previousPart === $this->entityShortName ? '' : $previousPart . '_') . $currentPart;

$this->joins[$previousAlias . '.' . $currentPart] = ['alias' => $currentAlias, 'type' => $column->getJoinType()];
$this->joins[$previousAlias . '.' . $currentPart] = ['alias' => $currentAlias, 'type' => 'join'];

$metadata = $this->setIdentifierFromAssociation($currentAlias, $currentPart, $metadata);
}
Expand Down
33 changes: 11 additions & 22 deletions src/Column/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use Omines\DataTablesBundle\DataTable;
use Omines\DataTablesBundle\Filter\AbstractFilter;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand Down Expand Up @@ -117,12 +116,11 @@ protected function configureOptions(OptionsResolver $resolver)
'field' => null,
'propertyPath' => null,
'visible' => true,
'orderable' => true,
'orderField' => function (Options $options) { return $options['field']; },
'searchable' => true,
'globalSearchable' => true,
'orderable' => null,
'orderField' => null,
'searchable' => null,
'globalSearchable' => null,
'filter' => null,
'joinType' => 'join',
'className' => null,
'render' => null,
])
Expand All @@ -131,12 +129,11 @@ protected function configureOptions(OptionsResolver $resolver)
->setAllowedTypes('field', ['null', 'string'])
->setAllowedTypes('propertyPath', ['null', 'string'])
->setAllowedTypes('visible', 'boolean')
->setAllowedTypes('orderable', 'boolean')
->setAllowedTypes('orderable', ['null', 'boolean'])
->setAllowedTypes('orderField', ['null', 'string'])
->setAllowedTypes('searchable', 'boolean')
->setAllowedTypes('globalSearchable', 'boolean')
->setAllowedTypes('searchable', ['null', 'boolean'])
->setAllowedTypes('globalSearchable', ['null', 'boolean'])
->setAllowedTypes('filter', ['null', 'array'])
->setAllowedTypes('joinType', ['null', 'string'])
->setAllowedTypes('className', ['null', 'string'])
->setAllowedTypes('render', ['null', 'string', 'callable'])
;
Expand Down Expand Up @@ -205,15 +202,15 @@ public function isVisible(): bool
*/
public function isSearchable(): bool
{
return $this->options['searchable'];
return $this->options['searchable'] ?? !empty($this->getField());
}

/**
* @return bool
*/
public function isOrderable(): bool
{
return $this->options['orderable'];
return $this->options['orderable'] ?? !empty($this->getOrderField());
}

/**
Expand All @@ -229,23 +226,15 @@ public function getFilter()
*/
public function getOrderField()
{
return $this->options['orderField'];
}

/**
* @return string|null
*/
public function getJoinType()
{
return $this->options['joinType'];
return $this->options['orderField'] ?? $this->getField();
}

/**
* @return bool
*/
public function isGlobalSearchable(): bool
{
return $this->options['globalSearchable'];
return $this->options['globalSearchable'] ?? $this->isSearchable();
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Column/TextColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ protected function configureOptions(OptionsResolver $resolver)
parent::configureOptions($resolver);

$resolver
->setDefaults([
'orderable' => true,
'raw' => false,
'searchable' => true,
])
->setDefault('raw', false)
->setAllowedTypes('raw', 'bool')
;

Expand Down
1 change: 0 additions & 1 deletion src/DataTableFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
namespace Omines\DataTablesBundle;

use Omines\DataTablesBundle\DependencyInjection\Instantiator;
use Symfony\Component\DependencyInjection\ServiceLocator;

class DataTableFactory
{
Expand Down
6 changes: 3 additions & 3 deletions src/Resources/assets/js/datatables.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@

root.html(data.template);
fulfill(dt = $('table', root).DataTable(dtOpts));
}).fail(function(err) {
console.error(err);
reject(err);
}).fail(function(xhr, cause, msg) {
console.error('DataTables request failed: ' + msg);
reject(cause);
});
});
};
Expand Down

0 comments on commit ce07086

Please sign in to comment.