I'm so hard trying to make a autocomplete widget with an API.
I tested connectors, CAPI and them are ok!
So now I'm trying to make the code for the WI in JS. I'm based in [Tech'Tuesday] Configuration of multi-selection & autocomplete widget video.
My CAPI returns like this example in a test:
{"nome_gera":"cia","gera_result":{"created":1413502503662,"value":[{"RG":null,"Cliente":true,"Bloqueado":false,"Pais":"Brasil","Tecnico":false,"Vendedor":false,"Salt":null,"CodigoUF":"33","LogradouroNumero":null,"Celular":null,"DataNascimento":"0001-01-01T00:00:00-02:00","Complemento":null,"Logradouro":"","NomeMae":null,"NomePai":null,"Naturalidade":null,"ID":"53908a6d7fc272077403c29e","Telefone":"(24) 3353-7300","Fonecedor":true,"VendedorPadrao":null,"IE":null,"Site":"www.cocacolabrasil.com.br","NomeFantasia":"CIA FLU - COCA COLA","Representada":false,"ValorMinimoCompra":0.0,"UF":"RJ","RazaoSocial":"COMPANHIA FLUMINENSE DE REFRIGERANTES LTDA","CNPJ_CPF":"31.456.338/0001-86","Senha":null,"Transportadora":false,"Cidade":"Porto Real","Email":null,"EmailLoginEcommerce":null,"CodigoMunicipio":"3304110","CEP":"27570000","PessoaFisica":false,"CodigoPais":"1058","Bairro":"","Ramo":"Industria"}],"size":1}}
So, I tryied to take for my case the code in that Tech Tuesday. The code is:
var report_to_timer;
$(#"id_gerador").select2({
placeholder : "Entre com o nome do gerador",
allowClear : true,
minimumInputLenght : 1,
query : function (query) {
var data = {
results : []
};
clearTimeout(report_to_timer)
report_to_timer = setTimeout(function () {
id_consulta_geradores_reee.trigger ({
"gera_result" : gera_result.values
}, {},
function(result) {
for (i = 0; i < Math.min(10, result.gera_result.lenght); i++) {
data.results.push({
nome : result.gera_result[i].NomeFantasia,
razao_social : result.gera_result[i].RazaoSocial
id : result.gera_result[i].ID,
cnpj_cpf : result.gera_result[i].CNPJ_CPF,
logradouro : result.gera_result[i].Logradouro,
num : result.gera_result[i].LogradouroNumero,
cidade : result.gera_result[i].Cidade,
uf : result.gera_result[i].CodigoUF,
complemento : result.gera_result[i].Complemento,
telefone : result.gera_result[i].Telefone,
IE : result.gera_result[i].IE
});
}
gera_result.callback(data);
},
function () {
alert("Erro buscando geradores de REEE");
}}
}, 200};
},
formatSelection : formatSelection,
initSelection : function (element, callback) {
var data = {};
if ("${id?default("")}" =!"") {
data = {
text : "",
razao_social : "${RazaoSocial?default("")}",
id : "${id?default("")}",
cnpj_cpf : "${cnpj_cpf?default("")}",
logradouro : "${logradouro?default("")}",
num : "${numero?default("")}",
cidade : "${cidade?default("")}",
uf : "${uf?default("")}",
complemento : "${complemento?default("")}",
telefone : "${telefone_principal?default("")}",
IE : "${ie?default("")}"
};
callback(data);
}
}
});
${"id_gerador"}.on("change", function (e) {
if (e.added != undefined) {
RMPApplication.set("selected_gerador_reee_id", e.added.id);
RMPApplication.setVariable("RazaoSocial", e.added.NomeFantasia);
RMPApplication.setVariable("cnpj_cpf", e.added.cnpj_cpf);
RMPApplication.setVariable("logradouro", e.added.logradouro);
RMPApplication.setVariable("numero", e.added.num);
RMPApplication.setVariable("cidade", e.added.cidade);
RMPApplication.setVariable("uf", e.added.uf);
RMPApplication.setVariable("complemento", e.added.complemento);
RMPApplication.setVariable("telefone_principal", e.added.telefone);
RMPApplication.setVariable("ie", e.added.ie);
} else {
RMPApplication.set("id", "");
RMPApplication.setVariable("RazaoSocial", "");
RMPApplication.setVariable("cnpj_cpf", "");
RMPApplication.setVariable("logradouro", "");
RMPApplication.setVariable("numero", "");
RMPApplication.setVariable("cidade", "");
RMPApplication.setVariable("uf", "");
RMPApplication.setVariable("complemento", "");
RMPApplication.setVariable("telefone_principal", "");
RMPApplication.setVariable("ie", "");
}
});
function formatSelection(result)
{
return result.name;
}
When I launch in test mode the WI, The box for search have the value X and when I click te JS Widget cams this message:
com.google.gwt.core.client.JavaScriptException: (SyntaxError) : Unexpected token ILLEGAL
The authentication method of my webservice is by a fixed token, that is informed in the Heather of my connector.
Can you help me?
Best regards
Hi,I have checked your code and corrected it.I am sending you the modified code assuming that : id_gerador is the identifier of yourautocomplete field on WI, ID is the name of the field as you have saved in Collection, id is the value variable of the field, id_consulta_geradores_reee is the identifier of your CAPI.I want you to add one more code in the input variables of your CAPI instead of using connector for that and don't write anything in output variable.Take a variable: gera_result: <#assign values = []><#list query?split(" ") as value><#assign values = values +[{"ID": {"$regex" : "^${value}" , "$options" : "i" }}]></#list><#assign pattern = {}><#assign pattern = P_json_put(pattern,"$and",values)><#assign result = inject_objects(list_objects(pattern,"Your Collection Name"))>${result}The output of the CAPI would be all the values from collection related to your ID.After that plugin the CAPI to WI and use the code am sending you as attachment assuming that ID is the autocomplete field in your WI.Also you can use one CAPI for autocompleting only one field at a time.If you want to do the same for other fields, follow the same.Create a separate CAPI & use the same code,just replace the variable names.Hope it helps and for further posts,please post on Support Forum.Thanks & RegardsBidisha
Hello,
I send the file in by email.