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.
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 …
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?
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…
[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
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…
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…
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;
});
};