Skip to content

Commit ce07086

Browse files
committed
Improve automatic detection of searchable/orderable columns
1 parent 323870f commit ce07086

File tree

5 files changed

+16
-32
lines changed

5 files changed

+16
-32
lines changed

src/Adapter/Doctrine/ORM/AutomaticQueryBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private function addSelectColumns(AbstractColumn $column, string $field)
103103
$currentPart = array_shift($parts);
104104
$currentAlias = ($previousPart === $this->entityShortName ? '' : $previousPart . '_') . $currentPart;
105105

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

108108
$metadata = $this->setIdentifierFromAssociation($currentAlias, $currentPart, $metadata);
109109
}

src/Column/AbstractColumn.php

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
use Omines\DataTablesBundle\DataTable;
1616
use Omines\DataTablesBundle\Filter\AbstractFilter;
17-
use Symfony\Component\OptionsResolver\Options;
1817
use Symfony\Component\OptionsResolver\OptionsResolver;
1918

2019
/**
@@ -117,12 +116,11 @@ protected function configureOptions(OptionsResolver $resolver)
117116
'field' => null,
118117
'propertyPath' => null,
119118
'visible' => true,
120-
'orderable' => true,
121-
'orderField' => function (Options $options) { return $options['field']; },
122-
'searchable' => true,
123-
'globalSearchable' => true,
119+
'orderable' => null,
120+
'orderField' => null,
121+
'searchable' => null,
122+
'globalSearchable' => null,
124123
'filter' => null,
125-
'joinType' => 'join',
126124
'className' => null,
127125
'render' => null,
128126
])
@@ -131,12 +129,11 @@ protected function configureOptions(OptionsResolver $resolver)
131129
->setAllowedTypes('field', ['null', 'string'])
132130
->setAllowedTypes('propertyPath', ['null', 'string'])
133131
->setAllowedTypes('visible', 'boolean')
134-
->setAllowedTypes('orderable', 'boolean')
132+
->setAllowedTypes('orderable', ['null', 'boolean'])
135133
->setAllowedTypes('orderField', ['null', 'string'])
136-
->setAllowedTypes('searchable', 'boolean')
137-
->setAllowedTypes('globalSearchable', 'boolean')
134+
->setAllowedTypes('searchable', ['null', 'boolean'])
135+
->setAllowedTypes('globalSearchable', ['null', 'boolean'])
138136
->setAllowedTypes('filter', ['null', 'array'])
139-
->setAllowedTypes('joinType', ['null', 'string'])
140137
->setAllowedTypes('className', ['null', 'string'])
141138
->setAllowedTypes('render', ['null', 'string', 'callable'])
142139
;
@@ -205,15 +202,15 @@ public function isVisible(): bool
205202
*/
206203
public function isSearchable(): bool
207204
{
208-
return $this->options['searchable'];
205+
return $this->options['searchable'] ?? !empty($this->getField());
209206
}
210207

211208
/**
212209
* @return bool
213210
*/
214211
public function isOrderable(): bool
215212
{
216-
return $this->options['orderable'];
213+
return $this->options['orderable'] ?? !empty($this->getOrderField());
217214
}
218215

219216
/**
@@ -229,23 +226,15 @@ public function getFilter()
229226
*/
230227
public function getOrderField()
231228
{
232-
return $this->options['orderField'];
233-
}
234-
235-
/**
236-
* @return string|null
237-
*/
238-
public function getJoinType()
239-
{
240-
return $this->options['joinType'];
229+
return $this->options['orderField'] ?? $this->getField();
241230
}
242231

243232
/**
244233
* @return bool
245234
*/
246235
public function isGlobalSearchable(): bool
247236
{
248-
return $this->options['globalSearchable'];
237+
return $this->options['globalSearchable'] ?? $this->isSearchable();
249238
}
250239

251240
/**

src/Column/TextColumn.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ protected function configureOptions(OptionsResolver $resolver)
3939
parent::configureOptions($resolver);
4040

4141
$resolver
42-
->setDefaults([
43-
'orderable' => true,
44-
'raw' => false,
45-
'searchable' => true,
46-
])
42+
->setDefault('raw', false)
4743
->setAllowedTypes('raw', 'bool')
4844
;
4945

src/DataTableFactory.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
namespace Omines\DataTablesBundle;
1414

1515
use Omines\DataTablesBundle\DependencyInjection\Instantiator;
16-
use Symfony\Component\DependencyInjection\ServiceLocator;
1716

1817
class DataTableFactory
1918
{

src/Resources/assets/js/datatables.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@
6060

6161
root.html(data.template);
6262
fulfill(dt = $('table', root).DataTable(dtOpts));
63-
}).fail(function(err) {
64-
console.error(err);
65-
reject(err);
63+
}).fail(function(xhr, cause, msg) {
64+
console.error('DataTables request failed: ' + msg);
65+
reject(cause);
6666
});
6767
});
6868
};

0 commit comments

Comments
 (0)