cancel
Showing results for 
Search instead for 
Did you mean: 

how to load data json to combobox

naotoxxx
Participant
0 Kudos

Hi community, i'm triying to load some data from my json to a combobox but nothing happend

error:

jquery.sap.global-dbg.js:1035 2018-05-18 14:37:36.056399 The following problem occurred: parsererror - {
	"V_ADRCTPRT" : [ {
	    "cod_comuna":   "1000005",  "cod_pob": "02101",	"nomb_comuna": "ANTOFAGASTA",	"ciudad": "ANTOFAGASTA",   "region": 02"
	    "cod_comuna":   "1000005",  "cod_pob": "02102",	"nomb_comuna": "MEJILLONES",	"ciudad": "ANTOFAGASTA",   "region": 02"
	    "cod_comuna":   "1000005",  "cod_pob": "02103",	"nomb_comuna": "SIERRA GORDA",	"ciudad": "ANTOFAGASTA",   "region": 02"
	    "cod_comuna":   "1000005",  "cod_pob": "02104",	"nomb_comuna": "TALTAL",		"ciudad": "ANTOFAGASTA",   "region": 02"
	} ]
},200,OK 

my structure just in case

this is my data: (comunas.json)

{ "V_ADRCTPRT" : [ 
	    {"cod_comuna":   "1000005",  "cod_pob": "02101",	"nomb_comuna": "ANTOFAGASTA",	"ciudad": "ANTOFAGASTA",   "region": "02"},
	    {"cod_comuna":   "1000005",  "cod_pob": "02102",	"nomb_comuna": "MEJILLONES",	"ciudad": "ANTOFAGASTA",   "region": "02"},
	    {"cod_comuna":   "1000005",  "cod_pob": "02103",	"nomb_comuna": "SIERRA GORDA",	"ciudad": "ANTOFAGASTA",   "region": "02"},
	    {"cod_comuna":   "1000005",  "cod_pob": "02104",	"nomb_comuna": "TALTAL",		"ciudad": "ANTOFAGASTA",   "region": "02"}
 ]
}

this is my view:

<ComboBox showSecondaryValues= "true"
          items="{path: '/oModelComunas/V_ADRCTPRT'}">
<core:ListItem key="{cod_comuna}-{cod_pob}" text="{nomb_comuna}" additionalText = "{ciudad}"/>
</ComboBox>

and in my controller i don't know how to load the data

var oModelComunas = new JSONModel(jQuery.sap.getModulePath("crm.prospecto", "/model/comunas.json"));
this.getView().setModel(oModelComunas);

Accepted Solutions (1)

Accepted Solutions (1)

Sharathmg
Active Contributor

First, load the json to your local variable using the loadData method of the JSON model.

var localData = new sap.ui.model.json.JSONModel();
localData.loadData("model/comunas.json");
// Before you proceed, test in debugger, if your data from local json is loaded in the local vairable localData.
Then, set the data to view
this.getView().setModel(localData); 
naotoxxx
Participant
0 Kudos

thanks!, but still getting the error, i have a model already assigned to my view is there a problem if i send another? or can i sent the model just to the combobox using an id?

naotoxxx
Participant
0 Kudos

look i send the model to my combobox

<ComboBox 	id="comboComuna"
		showSecondaryValues= "true"
		items="{path: '/localData/V_ADRCTPRT/'}">
<core:ListItem key="{cod_comuna} - {cod_pob}" text="{nomb_comuna}" additionalText = "{ciudad}"/>
</ComboBox>

and my controller:

var localData = new JSONModel();
localData.loadData("model/comunas.json");
this.getView().byId("comboComuna").setModel(localData);

and i'm no getting any error of this and my data is loading god, look:

combo.png

naotoxxx
Participant
0 Kudos

i know what was the problem , if i sent my model i was rewriting the model again in my combo box

items="{path: '/V_ADRCTPRT'}" Ok

items="{path: '/localData/V_ADRCTPRT'}"> Not Ok

and the controller was fine

 var localData = new JSONModel();
         localData.loadData("model/comunas.json");
         this.getView().byId("comboComuna").setModel(localData);

Answers (1)

Answers (1)

former_member251534
Participant
0 Kudos

Hi Naoto

There is issue with your items bindingthere shouldnot be "/" for /oModelComunas/V_ADRCTPRT . I think your model is named model so it should be like oModelComunas>/V_ADRCTPRT.

Another issue is you have not mentioned the row to be binded here . It should be like oModelComunas/0/V_ADRCTPRT. This should solve your issue please check.