How to specify in the properties of the component that the drop-down list should be with a multi-select?
public function defineProperties()
{
return [
'doctorslistrender' => [
'title' => 'title',
'description' => 'title',
'default' => '',
'multiple'=> true,// not working
'type' => 'dropdown',
],
];
The documentation says:
Currently supported types are string , checkbox , dropdown and set .
I think, you could use a set:
public function defineProperties()
{
return [
'doctorslistrender' => [
'title' => 'Doctors',
'description' => 'Famous TV doctors :)',
'default' => '',
'type' => 'set',
],
];
}
public function getDoctorslistrenderOptions()
{
return [
'mcCoy' => 'Dr. Leonard \'Bones\' McCoy',
'dorian' => 'J.D. Dorian',
'house' => 'Greg House'
];
}
It works. Thank you.
But there is one problem with populating getDoctorslistrenderOptions.
it works:
public function getDoctorslistrenderOptions()
{
$list_doctors = Doctor::get()->sortBy('last_name');
$list_slug_component['none'] = 'No';
foreach ($list_doctors as $value) {
$list_slug_component += ["$value[slug]" => $value['full_name']];
}
return $list_slug_component;
}
‘slug’ field in database:
$table->string(‘slug’)
this does not work:
public function getDoctorslistrenderOptions()
{
$list_doctors = Doctor::get()->sortBy('last_name');
$list_slug_component['none'] = 'No';
foreach ($list_doctors as $value) {
$list_slug_component += ["$value[id]" => $value['full_name']];
}
return $list_slug_component;
}
‘id’ field in databases
$table->increments(‘id’)->unsigned();
i think the problem is in the datatype of the ‘id’ field.
How can I konvert “$value[id]” to a string type?
If guess, that would be following:
foreach ($list_doctors as $key => $value) {
$list_slug_component += [$key => $value['full_name']];
}
Does not work. It seems to me that this cannot work correctly, $key => $value
will write to $key the number after sorting, and I need id
Could you please:
dd($list_doctors);
and show me the contents. Maybe then I’m able to help.
I can’t get dump in getDoctorslistrenderOptions() function,
so i added the code to the onRender() function
and this is what i got
public function onRender(){
$list_doctors = Doctor::active()->get()->sortBy('last_name');
$list_slug_component['none'] = Lang::get('konvertagency.landing::lang.interface.none');
foreach ($list_doctors as $value) {
$list_slug_component += ["$value[id]" => $value['last_name']];
}
dd($list_slug_component, $list_doctors);
}
i tried to simplify the collection
$list_doctors = Doctor::active()->get()->sortBy('last_name')->toArray();
but for getDoctorslistrenderOptions() it doesn’t work either - .
trying to change the syntax didn’t work
$list_slug_component += [$value['id'] => $value['last_name']];
Hey point111,
I just tried it out and it seems that, key and value should be both a string (I don’t know if that is a bug?).
So you may try following:
foreach ($list_doctors as $key => $value) {
$list_slug_component += [$value['slug'] => $value['full_name']];
}
Does this work?
Yes, it works (as amended - foreach ($list_doctors as $value)
), I wrote in post #3. I can’t convert a number to a string in [$value[‘id’].
"$value[id]"
- does not work
(string) $value['id']
- does not work
'id'.$value['id']
- works, but parasitic characters are added to the id, in the future these characters should be removed.
Here is the complete solution:
public function defineProperties()
{
return [
'doctorslistrender' => [
'title' => 'Doctors',
'description' => 'Famous TV doctors :)',
'type' => 'set',
'default' => 'none'
],
];
}
public function getDoctorslistrenderOptions()
{
$list_doctors = Doctor::get()->sortBy('last_name');
$list_slug_component['none'] = 'none';
foreach ($list_doctors as $value) {
$list_slug_component += ['id'.$value['id'] => $value['last_name']];
}
return $list_slug_component;
}
public $listrender;
public function onRender(){
$doctorslistrender = $this->property('doctorslistrender');
$list = [];
foreach ($doctorslistrender as $value){
array_push ($list, str_replace('id','',$value));
}
$this->listrender = Doctor::whereIn('id', $list)->get()->sortBy(fn($item) => array_search($item->getKey('id'), $list))->toArray();//send to twig
}
}
I do not like this solution, adding parasitic characters and then removing (str_replace('id','',$value
) them is extra steps. But it works.