Reputation: 1353
My javascript code;
function callDist(sel)
{
$.getJSON('Dist.json', function(data){
var $container = $('#parameters').empty();
$.each(data.distributions, function(i, distribution){
$.each(distribution, function(key,value)
{
if(distribution.type==sel.value) {
alert(distribution.name);
$.each(distribution.parameters, function(key, value) {
$container.append(key + ':' + value + '<br />');
$container.append('<hr />');
});
}
});
}
);
});
}
My Json code;
{
"distributions":[
{
"name":"Uniform",
"type":1,
"parameters":[{ "minValue":2 , "maxValue":4 }]
},
{ "name":"Normal",
"type":2,
"parameters":[{ "mean":5 , "standartDeviation":3 }]
},
{
"name":"Exponential",
"type":3,
"parameters":[{"lamda":2}]
},
{
"name":"Geometric",
"type":4,
"parameters":[{"probability":0.2}]
}
]
}
Html code;
<select name="selectDistribution" class="span12" id="Options"
onchange="callDist(this);" >
<option value="0">Choose one distribution</option>
<option value="1">Uniform</option>
<option value="2">Normal</option>
<option value="3">Exponential</option>
<option value="4" >Geometric</option>
</select>
In Json there is an array that created by some distributions.when the user selects some of them from select option I am triggering callDist(sel) function. When the passed value sel is equal to distribution.type , I want to write the parameters of that distribution to a div.How can I do that?Thanks.
Upvotes: 2
Views: 258
Reputation: 27247
function callDist(sel) {
$.getJSON('Dist.json', function(json){
var $container = $('#parameters').empty();
$.each(json.distributions, function(i, distribution) {
if(distribution.type==sel.value) {
$.each(distribution.parameters, function(j, parameters) {
$.each(parameters, function(parameter_key, parameter_value) {
$container.append(parameter_key + ':' + parameter_value + '<br />');
$container.append('<hr />');
});
});
}
});
});
}
Upvotes: 2
Reputation: 1513
I made a fiddle around it here . I am able to see the alert with the correct value. I used
$('#Options').change()
to trigger the function and a few other modifications to work on fiddle.
Upvotes: 0
Reputation: 7687
With distributions
set up as an array, you'll have to loop through distributions and check to see if the type
value of each element matches sel
, and then append the parameters of that object to the div in question, unless position always translates to a certain type. If that's the case, something like
var distribution = data[(sel-1)]
can be used to set the distribution.
What might make more sense, since there may be cases where position in the array does not correspond to type, is to restructure your JSON so that you have an object instead of an array, like the following:
{"distributions":{
"Uniform":{
"name":"Uniform",
"type":1,
"parameters":[{ "minValue":2 , "maxValue":4 }]
},
"Normal":{
"name":"Normal",
"type":2,
"parameters":[{ "mean":5 , "standartDeviation":3 }]
}
}
}
In this case, I've used the name
field as the attribute names of the distributions
object, and can get my parameters for any given distrubution by typing data[selName].parameters
, in which selName
is the name corresponding to the selection, but you could just as easily use the type as the attribute name.
Both of these methods would remove the need to loop through the data.
Upvotes: 1
Reputation: 1513
Remove the 2nd comma in
$.each(data.distributions, function(i, distribution,) {
Upvotes: -1