12
I’m making a research form (action=GET
) where a field uses Selectize.js:
$("#selectize").each(function () {
$(this).selectize({
plugins: ['remove_button'],
valueField: 'AdvogadoId',
labelField: 'Descricao',
searchField: ['AdvogadoId', 'Descricao'],
create: false,
persist: false,
preload: true,
initUrl: "/Advogados/PesquisarJson/",
initData: true,
load: function (query, callback) {
$.ajax({
url: "/Advogados/PesquisarJson/",
type: 'GET',
error: function () {
callback();
},
success: function (res) {
callback(res);
}
});
}
});
});
With the empty form, I enter the lawyer’s name and acronym. Everything is filled in correctly. When submitting the research, the values go to the back-end correctly, separated by commas.
When the form is reloaded with the values of the GET
, what happens is that each tag appears only with the lawyer’s name filled, without the name. When checking the options, the options chosen appear only as the acronym, and the correct one would be "Acronym - Name".
I tried some solutions like this (does not work, besides being complex for what I need) and this (no use data-data
: use only form values).
Is there any way (preferably performative) to load these values correctly on each tag selected on screen?
Code in the Back-end:
public JsonResult PesquisarJson(String termo = "")
{
// Aqui não é EF. É Dapper em cima de Oracle.
using (var repositorio = new AdvogadoRepositorio())
{
var registros = repositorio.Condicao(a => a.DataSaida == null).OrdenarPor(a => a.AdvogadoId).Selecionar();
return Json(registros.Select(a => new { AdvogadoId = a.AdvogadoId, Descricao = a.AdvogadoId + " - " + a.Nome }).ToList(),
JsonRequestBehavior.AllowGet);
}
}
HTML:
<div class="selectize-control form-control selectize multi plugin-remove_button">
<div class="selectize-input items not-full">
<input type="text" autocomplete="off" tabindex="" style="width: 4px; opacity: 1; position: relative; left: 0px;">
</div>
<div class="selectize-dropdown multi form-control selectize plugin-remove_button" style="display: none;">
<div class="selectize-dropdown-content">
</div>
</div>
</div>
Rest result:
[
{
"AdvogadoId": "A1",
"Descricao": "A1 - Fulano de Araujo"
},
{
"AdvogadoId": "A2",
"Descricao": "A2 - Beltrano de Lima"
},
{
"AdvogadoId": "A3",
"Descricao": "A3 - Ciclano da Silva"
},
{
"AdvogadoId": "A4",
"Descricao": "A4 - Herculano Junior"
},
...
]
But how do you want to get "Acronym - Name" if the
valueField
only allows a single attribute? whereas the names of their respective acronyms are in the database and have aID
, the solution would be to take theID
and get the name and acronym of the selected records via backend.– Felipe Assunção
You could also customize the selectize source to receive a Fields value array and concatenate the respective ones into the desired format
– Felipe Assunção
So the challenge is part of the question. I would like these ideas to become an answer.
– Leonel Sanches da Silva
Please edit the question with your controller code, so the answer is not generic
– Felipe Assunção
@Felipeasunción Feito.
– Leonel Sanches da Silva
@Felipeasuncion I put more information. See if it helps.
– Leonel Sanches da Silva