You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Originally posted by giovdi August 7, 2024
Hi everyone!
Today, I was wondering if it's possible to write HTML into a Select2 dropdown with Backpack. Well, the short answer (I think) is no... but...
I tried looking at the code that generates the Select2 Javascript for my specific case, and it is probably pretty easy to do.
In this example, I added the following code blocks to backpack/pro/resources/views/fields/relationship/select.blade.php.
// make sure the $field['value'] takes the proper value
$current_value = old_empty_or_null($field['name'], []) ?? $field['value'] ?? $field['default'] ?? [];
+ if(isset($field['html']) && !is_array($field['html'])) {+ $field['html'] = [true];+ }+ $activeHtml = !empty($field['html']) ? true : false;++ if ($activeHtml) {+ $field['html']['base_element'] = $field['html']['base_element'] ?? 'span';+ }
if (!empty($current_value) || is_int($current_value)) {
switch (gettype($current_value)) {
As you can see from the code blocks, a <span> tag surrounds the HTML option, but you can customize it with the base_element property, as follows: 'html' => ['base_element' => 'div'].
You can't avoid this because if you mix plain text options and HTML ones, the $(elem.text); fails with a JS error, so at least one HTML tag is required.
Please let me know your suggestions... or if I just lost 2 hours of my life for some option that I don't know 😄
Unfortunately, I don't have enough time to create a full PR in a reasonable time, so please, if it's a good idea, take care of it :)
Thanks a lot!
The text was updated successfully, but these errors were encountered:
pxpm
transferred this issue from Laravel-Backpack/community-forum
Aug 16, 2024
I was 1 minute into reading it and was ready to say "NO" but, holy shit... can this be useful! Like for example... when you have a selector for people - to show the avatar too. Or show both the name and their email, but in a nice way, not in the standard John Doe - [email protected].
I like this, I think it's a nice COULD-DO. Buuuut.... I would mean having to do this in all our select2s right 🤦♂️ So it would take quite some time to actually implement it in Backpack PRO 😔
Discussed in Laravel-Backpack/community-forum#1109
Originally posted by giovdi August 7, 2024
Hi everyone!
Today, I was wondering if it's possible to write HTML into a Select2 dropdown with Backpack. Well, the short answer (I think) is no... but...
I tried looking at the code that generates the Select2 Javascript for my specific case, and it is probably pretty easy to do.
In this example, I added the following code blocks to
backpack/pro/resources/views/fields/relationship/select.blade.php
.At this point, I can add the following option to the CRUD:
CRUD::field([ 'name' => 'variants', 'type' => "relationship", 'inline_create' => true, 'init_rows' => 1, 'min_rows' => 1, 'subfields' => [ [ 'name' => 'price', 'label' => 'Price', 'type' => 'number', 'prefix' => "€", 'attributes' => ["step" => "any"], 'wrapper' => [ 'class' => 'form-group col-md-3' ], ], [ 'name' => 'weight_initial', 'label' => 'Weight', 'type' => 'number', 'suffix' => "grams", 'wrapper' => [ 'class' => 'form-group col-md-3' ], ], ], 'pivotSelect' => [ + 'html' => true, 'wrapper' => [ 'class' => 'form-group col-md-6' ], 'options' => (function ($query) { return $query->orderBy('variant_name', 'ASC')->get(); }) ], ]);
...and with the following accessor:
...here is the result!
Not so bad, isn't it?
As you can see from the code blocks, a
<span>
tag surrounds the HTML option, but you can customize it with thebase_element
property, as follows:'html' => ['base_element' => 'div']
.You can't avoid this because if you mix plain text options and HTML ones, the
$(elem.text);
fails with a JS error, so at least one HTML tag is required.Please let me know your suggestions... or if I just lost 2 hours of my life for some option that I don't know 😄
Unfortunately, I don't have enough time to create a full PR in a reasonable time, so please, if it's a good idea, take care of it :)
Thanks a lot!
The text was updated successfully, but these errors were encountered: