How poputalte como with json response of server

Hi, the following is to ask how to populate a combo with a response from a server in json format like this:

[{“id”:“1”,“name”:“someValue”},
{“id”:“2”,“name”:“someValue”},
{“id”:“3”,“name”:“someValue”},
{“id”:“4”,“name”:“someValue”},
{“id”:“5”,“name”:“someValue”}]

I see that in other controls like grid you can make a one to one correspondence between a return to a column element specifies just taking care with the same name …

Note: The server is remote, I can not change the format of return.

Thanks …

Did you try load method?
Or do you need replace default option value [value, label, css, img_src] with id and name?

I carry the load combo with the method like this:

myCombo.load("…/server/url.php");

I carry the load combo with the method like this:
but the answer that gives me the server is a json with: id, name and
I think I should change to value, text … how I do this, as I said in controls like the grid simply establish one to one correspondence between the column name and record name …

Combo waits “value, text, css, img_src” or an array of array or an array of objects.

Is there a predefined method to transform between response format and the default for the combo …?

I have created a function that allows you to transform the results of the request to the server to the format required by the combo (value, text) but now must pass as a parameter the resource url from which I extract the resulting json, my question is, how internally the method load…? that function calls to load the resource via ajax?

Thanks…

load
docs.dhtmlx.com/api__link__dhtmlxcombo_load.html

the method load load the resource and tries to render the component, that makes me at the beginning, since the data source returns me a set of values in json format that represent the internal structure of the table, and which must transform to value, text format therefore require:

1 make the request for the resource.
2 capture the resource in the form of a valid json result
3 convert it json(o array) valid for the control.

I’m obviously new to this library, even in javascript, but it is strange me that the grid component has a Setup mode Basic to make possible the loading of external resources (json) names from remote, simply configuring data columns with the names of the fields, and the combo control do not allow… me is hard to believe that you can not with predefined methods…

I hope your help in this problem…

Please attach demo to have example.

If you can be override options default as avalon in previous comment said:

Or do you need replace default option value [value, label, css, img_src] with id and name?

It would be great…

hi to all

[code]// add once
dhtmlXCombo.prototype.customLoad = function(url){
var combo = this;
window.dhx4.ajax.get(“options_url.php”, function®{
var t = r.xmlDoc.responseText; // or responseXML
// parse your opts here from response
// add add them into combo manualy
for (var q=0; q<parsed_options.length; q++) {
combo.addOption(…);
}
combo = null;
})
};

// then use
var myCombo = new dhtmlXCombo(…);
myCombo.customLoad(“some_url.php”);
[/code]

also you can open ticket in a support system or request a modification at sales@dhtmlx.com

Ok.

  • load json form server (use XMLHttpRequest) and return request.responseText

  • add '{"options":' + response + '}'

  • replace “id”: with “value”:
    and “name”: with “text”:

  • parse result string

  • myCombo.load(final_JSON);

It works.)

I was late

Hello, had not seen their responses, however reviewing code and documentation did something as well:

myCombo = new dhtmlXCombo("combo_zone", "combo", 230);
			
   			url="../../resourse.php";	
   			
			 dhx.ajax(url, function(text,salida){

   				respuesta_json=eval("("+text+")");
   				//respuesta_json=JSON.parse(text);
   				myCombo.enableFilteringMode(true);
   				myCombo.load(iterateJson(respuesta_json,"id","name"));
   			
			}); 

and light also I have a function that iterates over the parsed json response
call iterateJson:


function iterateJson(data,field_as_Value,field_as_Text)
	{
		var salida='{options:[';
		for(i=0;i<data.length;i++){

			salida=salida+
			"{value:"+"'"+data[i][field_as_Value]+"'"+","+
			"text:"+"'"+data[i][field_as_Text]+"'}";
			if(i<data.length-1){
				salida=salida+",";
			}else
				salida=salida+"]}";
		}
		return salida;	
	}

that takes three parameters,
data: the parsed response
field_as_value = the field of the table that will be taken as the value of the combo
field_as_text = the field of the table that will be taken as text to show

This last allows flexibility in the event of a change in the name of the resources provided by the server, since the idea is to make a method generic valid for any combo require

Thanks to everyone for their patience and answers…

so using the option given by Andrei, in the end my code is thus:

dhtmlXCombo.prototype.customLoad=function(url,field_as_Value,field_as_Text)
{
	var combo = this;
    window.dhx4.ajax.get(url, function(r){
        var t = r.xmlDoc.responseText; 
        options=eval("("+t+")");
     
        for (var i=0; i<options.length; i++) {
            combo.addOption(options[i][field_as_Value],options[i][field_as_Text]);
            ;
        }
        combo = null;
    });
};

once more thank you for your patience, I’m satisfied…

This clear to my how to add new functionality to the combo, but now as I integrate it to a form, see the combo type is declared just so:


{type:"fieldset", label:"Personal Data", width:600, 
					list:[
					{type:"input", label:"id", width:100},
					{type:"password", label:"password", width:100},
					{type:"newcolumn"},
					{type:"input", label:"last_name", width:250},
					{type:"input", label:"first_name",width:250},
					{type:"combo", label:"City",width:250, 
						connector:"cities.php"}

and is this connector property that now want to feed with the above-described functionality

Hi

here is original code. add the same but with your changes once after dhtmlx.js or form.js loaded.

dhtmlXForm.prototype.items.combo.doLoadOptsConnector = function(item, url) { var that = this; var i = item; item._connector_working = true; item._apiChange = true; item._combo.load(url, function(){ // try to set value if it was called while options loading was in progress i.callEvent("onOptionsLoaded", [i._idd]); i._connector_working = false; if (i._connector_value != null) { that.setValue(i, i._connector_value); i._connector_value = null; } i._apiChange = false; that = i = null; }); }

you need to change item._combo.load to item._combo.customLoad and add 4th param ‘callback’ to your customLoad function, i.e.:

dhtmlXCombo.prototype.customLoad=function(url,field_as_Value,field_as_Text, callback) { var combo = this; window.dhx4.ajax.get(url, function(r){ var t = r.xmlDoc.responseText; options=eval("("+t+")"); for (var i=0; i<options.length; i++) { combo.addOption(options[i][field_as_Value],options[i][field_as_Text]); } combo = null; if (callback) callback(); }); };

only field_as_Value,field_as_Text params left, no info to handle them in sample

I am implementing your solution, however for the custonLoad parameters to the

dhtmlXForm.prototype.items.combo.doLoadOptsConnector = function (item, url, field_as_Value, field_as_Text)

so this in turn pass that was the customLoad, but does not work, internally the connect method transforms all parameters to the url, thus receiving a false url (url + field_as_value + field_as_text) does not work…

well

then please attach your custom combo-xml here and what version of combo?

Hi

dhtmlXCombo.prototype.customLoad=function(url,field_as_Value,field_as_Text,callback)
//dhtmlXCombo.prototype.customLoad=function(url,callback)
{
	var combo = this;
    window.dhx4.ajax.get(url, function(r){
        var t = r.xmlDoc.responseText; 
        options=eval("("+t+")");

     
        for (var i=0; i<options.length; i++) {
            combo.addOption(options[i][field_as_Value],options[i][field_as_Text]);
            ;
        }
        combo = null;
        if (callback) callback();
    });
};


dhtmlXForm.prototype.items.combo.doLoadOptsConnector = function(item, url,field_as_Value,field_as_Text ) 
{
   var that = this;
   var i = item;
   item._connector_working = true;
   item._apiChange = true;
   /*
   field_as_Value="id";
   field_as_Text="nombre";
   */
   item._combo.customLoad(url,field_as_Value,field_as_Text, function(){
      // try to set value if it was called while options loading was in progress
      i.callEvent("onOptionsLoaded", [i._idd]);
      i._connector_working = false;
      if (i._connector_value != null) {
         that.setValue(i, i._connector_value);
         i._connector_value = null;
      }
      i._apiChange = false;
      that = i = null;
   });
};

i have the last version of dhtmlxSuite

thanks …