Select2 concatenating various fields
Select2 concatenating various fields
carrarachristophe
Posts: 113Questions: 25Answers: 2
in Plug-ins
Hello,
I don't find any example of a Select2 field listing various concatenated mysql fields, or even including an icon (flag).
Would you have any example to share?
I am currently using the following:
{
label: "Langue:",
name: "utilisateurs.langue",
type: 'select2',
placeholder: '',
placeholderDisabled: false,
placeholderValue: null
},
Thank you
This question has an accepted answers - jump to answer
Answers
Hi,
The display of Select2 is dependent upon what you return from the server. By default it uses
id
andtext
properties in the array of results, although in Editor's plug-in we actually usevalue
/label
to match the other field types in Editor.So your
label
return from the server would include the two (or more) concatenated fields. How are you currently populating the values? I presume you are reading them from an SQL database?Thanks,
Allan
I forgot to reply about the icons, sorry. This Select2 documentation page shows how to use their
templateResult
option to render HTML.Allan
Hi Allan,
Yes I am using ajax to read mysql data
with the following in table.bibliotheque_auteurs.php:
The Select2 dropdown currently only displays the field pays (which apears as label) from the table pays.
I would like to display both pays and pays_code from the same mysql table pays
That will populate the Select list from:
The documentation for the Options class is available here and as shown in one of it's examples you can pass an array of columns into the
label
method - e.g.:You can also specify a
render
function if you want to customise how the fields are joined together (by default they are space separated).Allan
Thank you Allan,
I was able to concatenate both pays and pays_code using the following:
But I fail adding an image in the string. I tried the following:
I am getting the following message:
DataTables warning: table id=bibliotheque_auteurs - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
http://datatables.net/tn/1
Warning</b>: A non-numeric value encountered in <b>C:\xampp\htdocs.....\php\table.bibliotheque_auteurs.php</b> on line <b>7</b>
Any idea of what I am doing wrong?
note: I changed the line number so you it matches with the above code.
The string concatenation uses Javascript style concat for about half of it, and PHP style for the other half
Should do it.
Allan
Hi Allan,
Thanks for your reply, and merry Christmas to you!
I am confused. I should use the code in the php file, correct?
Because I am getting the following value in the dropdown: (example)
<img src="img/flags-iso/flat/16/AL.png"> Albania (AL)
instead of:
theimage Albania (AL)
Merry Christmas to you as well
From the Select2 documentation it looks like you need to specify at
templateResult
function in order to have it display HTML inside the dropdowns.Allan
Thanks Allan,
Looks like this is not something easy to do.
I think this post might also help me.