Commit 56a2cec3 authored by Sylvain's avatar Sylvain

Select - Chargement dynamiquement via call Controller

parent bc32edc8
......@@ -145,7 +145,11 @@ Laravel 5.8 uses Package Auto-Discovery, so doesn't require you to manually add
'value' => 2,
'placeholder' => "Choose...",
'options' => [
'source' => $collection,
'source' => [
'call' => 'Modules\City\Http\Controllers\Api\CityController@index',
'params' => ['perpage' => 999],
'target' => 'data'
],
'default' => ['value' => '', 'label' => 'Choose a city...'],
'value' => ':id',
'label' => ['template' => [
......
......@@ -2,8 +2,10 @@
namespace Goldenscarab\Modulus\Service\Field\Compose;
use GuzzleHttp\Client;
use Illuminate\Support\Str;
use \Illuminate\Support\HtmlString;
use Illuminate\Support\Facades\App;
use Goldenscarab\Modulus\Helpers\Dynamik;
......@@ -508,7 +510,7 @@ class Field2Bt4
private function renderDynamicOptions($template, $sub_template, $attr_selected): string
{
$render = '';
$datas = data_get($this->options, 'source');
$source = data_get($this->options, 'source');
$value = data_get($this->options, 'value');
$label = data_get($this->options, 'label');
$attrs = data_get($this->options, 'attributes', []);
......@@ -516,13 +518,15 @@ class Field2Bt4
// Ajout de la valeur par défaut
$render .= $this->renderDefaultOption();
$datas = $this->getSourceData($source);
// Ajout des options
foreach ($datas as $data) {
$dynamik = new Dynamik($data);
// Préparation de l'option
$option_value = $dynamik->getValue($value);
$option_label = $dynamik->getValue($label);
$option_value = (string) $dynamik->getValue($value);
$option_label = (string) $dynamik->getValue($label);
// Préparation des attributs
$attributes = [];
......@@ -545,6 +549,23 @@ class Field2Bt4
return $render;
}
private function getSourceData(iterable $source): Iterable
{
$data = $source;
if (is_array($source) && array_key_exists('call', $source)) {
$call = data_get($source, 'call');
$params = data_get($source, 'params', []);
$target = data_get($source, 'target');
$request = new \Illuminate\Http\Request($params);
$response = App::call($call, ['request' => $request])->getData();
$data = data_get($response, $target);
}
return $data;
}
private function renderStaticOptions($template, $sub_template, $attr_selected): string
{
$render = '';
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment