Skip to content

Commit fc9a27d

Browse files
authored
Merge pull request #14 from alejandrodiazpinilla/main
[1.x] new funtion fromData
2 parents 9f606fc + 9016b78 commit fc9a27d

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,42 @@ In the HTML, you should have a structure similar to the one below. Make sure tha
395395
</thead>
396396
</table>
397397
```
398-
398+
## With Data Preparated
399+
You can send data from a array or a collection to the front, for example:
400+
```php
401+
Array:1 [
402+
0 => array:41 [
403+
"status" => "A"
404+
"name" => "JOHN ALEJANDRO DIAZ PINILLA"
405+
"born_day" => "1993-11-30"
406+
"scheduler" => "Sheduler 235 Hour"
407+
]
408+
]
409+
410+
Collection {
411+
0 => array:41 [
412+
"status" => "A"
413+
"name" => "CARLOS GIOVANNY RODRIGUEZ TRIVIÑO"
414+
"born_day" => "1992-10-19"
415+
"scheduler" => "Scheduler 235 Hour"
416+
]
417+
}
418+
```
419+
For the send to the front, you must use the method fromData(), and put the array or the collection in the parameters.
420+
```php
421+
$dataTable = new EasyDataTable();
422+
/* The array or collection of data must be sent to the method. */
423+
$dataTable->fromData($plantaActiva); /* Mandatory / Required */
424+
$dataTable->map(function ($row) {
425+
return [
426+
"status" => $row->status,
427+
"name" => $row->name,
428+
"born_day" => $row->born_day,
429+
"scheduler" => $row->scheduler,
430+
];
431+
});
432+
```
433+
this method work like the clientSide method, in this way the javascript is the same than the clientSide.
399434
## Creator
400435
- 🇨🇴 Raúl Mauricio Uñate Castro
401436

README_SPANISH.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,42 @@ En el HTML deberás contar con una estructura similar a la siguiente. Asegúrate
395395
</thead>
396396
</table>
397397
```
398-
398+
## Data Preparada
399+
Puedes enviar la data ya preparada como un arreglo o como una colección de datos, como los ejemplos a continuación:
400+
```php
401+
Array:1 [
402+
0 => array:41 [
403+
"estado" => "A"
404+
"nombre" => "JOHN ALEJANDRO DIAZ PINILLA"
405+
"fecha_nacimiento" => "1993-11-30"
406+
"jornada" => "JORNADA 235 HORAS"
407+
]
408+
]
409+
410+
Collection {
411+
0 => array:41 [
412+
"estado" => "A"
413+
"nombre" => "CARLOS GIOVANNY RODRIGUEZ TRIVIÑO"
414+
"fecha_nacimiento" => "1992-10-19"
415+
"jornada" => "JORNADA 235 HORAS"
416+
]
417+
}
418+
```
419+
Para el envió al front podrás utilizarlo de la siguiente manera en el controlador:
420+
```php
421+
$dataTable = new EasyDataTable();
422+
/* Al metodo se debe enviar el arreglo o la coleccion de datos */
423+
$dataTable->fromData($plantaActiva); /* Obligatorio / Requerido */
424+
$dataTable->map(function ($row) {
425+
return [
426+
"estado" => $row->estado,
427+
"nombre" => $row->nombre,
428+
"fecha_nacimiento" => $row->fecha_nacimiento,
429+
"jornada" => $row->jornada,
430+
];
431+
});
432+
```
433+
Este método funcionar como un clientSide, por este motivo la forma de implementarlo en el javascript será la misma.
399434
## Creador
400435
- 🇨🇴 Raúl Mauricio Uñate Castro
401436
- Correo electrónico: [email protected]

src/EasyDataTable.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class EasyDataTable extends EasyDataTableBase
2222
private $search;
2323
private $serverSide;
2424
private $clientSide;
25+
private $data;
2526

2627
/**
2728
* Create a new EasyDataTable instance.
@@ -88,6 +89,21 @@ public function query(Builder $query)
8889
return $this;
8990
}
9091

92+
/**
93+
* Set the array or a collection for the EasyDataTable.
94+
*
95+
* @param iterable $data
96+
*
97+
* @return $this
98+
*/
99+
public function fromData(iterable $data)
100+
{
101+
$this->clientSide();
102+
$this->data = $data;
103+
104+
return $this;
105+
}
106+
91107
/**
92108
* Set the map closure for the EasyDataTable.
93109
*
@@ -147,11 +163,11 @@ public function response()
147163
*/
148164
private function dataClientSide()
149165
{
150-
$rows = $this->query->get();
166+
$rows = is_iterable($this->data) ? $this->data : $this->query->get();
151167

152168
$data = [];
153169
foreach ($rows as $r) {
154-
$item = !empty($this->map) ? ($this->map)($r) : $r;
170+
$item = !empty($this->map) ? ($this->map)((object) $r) : $r;
155171
$data[] = $item;
156172
}
157173

0 commit comments

Comments
 (0)