55namespace FreezyBee \DataGridBundle ;
66
77use FreezyBee \DataGridBundle \Column \ActionColumn ;
8- use FreezyBee \DataGridBundle \Column \Column ;
98use FreezyBee \DataGridBundle \DataSource \DataSourceInterface ;
109use Symfony \Component \HttpFoundation \JsonResponse ;
1110use Symfony \Component \HttpFoundation \Request ;
@@ -22,33 +21,27 @@ class DataGrid
2221 /** @var DataSourceInterface */
2322 private $ dataSource ;
2423
25- /** @var Column[] */
26- private $ columns ;
27-
28- /** @var ActionColumn|null */
29- private $ actionColumn ;
24+ /** @var DataGridConfig */
25+ private $ config ;
3026
3127 /** @var string */
3228 private $ name ;
3329
3430 /**
3531 * @param EngineInterface $engine
3632 * @param DataSourceInterface $dataSource
37- * @param array $columns
38- * @param ActionColumn|null $actionColumn
33+ * @param DataGridConfig $config
3934 * @param string $name
4035 */
4136 public function __construct (
4237 EngineInterface $ engine ,
4338 DataSourceInterface $ dataSource ,
44- array $ columns ,
45- ?ActionColumn $ actionColumn ,
39+ DataGridConfig $ config ,
4640 string $ name
4741 ) {
4842 $ this ->engine = $ engine ;
4943 $ this ->dataSource = $ dataSource ;
50- $ this ->columns = $ columns ;
51- $ this ->actionColumn = $ actionColumn ;
44+ $ this ->config = $ config ;
5245 $ this ->name = $ name ;
5346 }
5447
@@ -65,13 +58,13 @@ public function ajax(Request $request): JsonResponse
6558 // sort
6659 $ orderByIndex = $ query ['order ' ][0 ]['column ' ] ?? null ;
6760 if ($ orderByIndex !== null ) {
68- $ this ->dataSource ->applySort ($ this ->columns [$ orderByIndex ], $ query ['order ' ][0 ]['dir ' ]);
61+ $ this ->dataSource ->applySort ($ this ->config -> getColumns () [$ orderByIndex ], $ query ['order ' ][0 ]['dir ' ]);
6962 }
7063
7164 // filters
7265 foreach ($ query ['columns ' ] as $ index => $ ajaxColumn ) {
7366 $ value = $ ajaxColumn ['search ' ]['value ' ];
74- $ column = $ this ->columns [$ index ] ?? null ;
67+ $ column = $ this ->config -> getColumns () [$ index ] ?? null ;
7568
7669 if ($ value !== '' && $ column !== null && $ column ->isFilterable ()) {
7770 $ this ->dataSource ->applyFilter ($ column , $ value );
@@ -88,17 +81,17 @@ public function ajax(Request $request): JsonResponse
8881 $ data = [];
8982 foreach ($ items as $ item ) {
9083 $ row = [];
91- foreach ($ this ->columns as $ column ) {
84+ foreach ($ this ->config -> getColumns () as $ column ) {
9285 if ($ column instanceof ActionColumn) {
9386 continue ;
9487 }
9588 $ row [] = $ column ->renderContent ($ item , $ this ->engine );
9689 }
9790
98- if ($ this ->actionColumn !== null ) {
91+ if ($ this ->config -> getActionColumn ()-> hasActions () ) {
9992 $ row [] = $ this ->engine ->render ('@FreezyBeeDataGrid/action.html.twig ' , [
10093 'item ' => $ item ,
101- 'actions ' => $ this ->actionColumn ->getActions ()
94+ 'actions ' => $ this ->config -> getActionColumn () ->getActions ()
10295 ]);
10396 }
10497 $ data [] = $ row ;
@@ -117,10 +110,20 @@ public function ajax(Request $request): JsonResponse
117110 */
118111 public function render (): string
119112 {
113+ $ sortIndex = 0 ;
114+ if ($ this ->config ->getDefaultSortColumnName () !== null ) {
115+ $ sortIndex = array_search ($ this ->config ->getDefaultSortColumnName (), $ this ->config ->getColumns (), true );
116+ }
117+
120118 return $ this ->engine ->render ('@FreezyBeeDataGrid/grid.html.twig ' , [
121119 'name ' => $ this ->name ,
122- 'columns ' => $ this ->columns ,
123- 'actionColumn ' => $ this ->actionColumn
120+ 'columns ' => $ this ->config ->getColumns (),
121+ 'actionColumn ' => $ this ->config ->getActionColumn (),
122+ 'default ' => [
123+ 'perPage ' => $ this ->config ->getDefaultPerPage (), $ this ->config ->getColumns (),
124+ 'sortIndex ' => $ sortIndex ,
125+ 'sortDir ' => $ this ->config ->getDefaultSortColumnDirection () ?? 'desc ' ,
126+ ]
124127 ]);
125128 }
126129}
0 commit comments