Sto usando typstead Bootstrap con una funzione ajax e voglio sapere qual è il formato corretto del risultato Json, per restituire un ID e una descrizione. Ho bisogno dell'ID per associare l'elemento selezionato typeahead con un modello mvc3.Bootstrap typeahead ajax result format - Esempio
Questo è il codice:
[Html]
<input id="myTypeahead" class='ajax-typeahead' type="text" data-link="myUrl" data-provide="typeahead" />
[Javascript]
$('#myTypeahead').typeahead({
source: function (query, process) {
return $.ajax({
url: $('#myTypeahead').data('link'),
type: 'post',
data: { query: query },
dataType: 'json',
success: function (jsonResult) {
return typeof jsonResult == 'undefined' ? false : process(jsonResult);
}
});
}
});
This works properly when I return a simple list of strings, for example:
{item1, item2, item3}
But I want to return a list with Id, for example:
{
{Id: 1, value: item1},
{Id: 2, value: item2},
{Id: 3, value: item3}
}
Come elaborare questo risultato nella Ajax "successo: function()"?
Questo è molto facile con jquery Completamento automatico, perché posso restituire un elenco di oggetti JSON.
[jquery Autocomplete process data example]
...
success: function (data) {
response($.map(data, function (item) {
return { label: item.Id, value: item.Value, id: item.Id, data: item };
})
...
Ma questo non funziona con boostrap Typeahead.
Qualcuno può aiutarmi?
Grazie.
Avete provato a convertire i valori di ritorno come un 'array? 'var list = [{Id: 1, value: item1}, {Id: 2, value: item2}, {Id: 3, value: item3}];' –
sì, non ho alcun problema. Il mio problema è come associare l'Id con il controllo html. La funzione di processo (dati) accetta solo un array di stringhe, non un array di oggetti – Gonzalo
Si sta utilizzando l'ultima versione di TypeAhead? –