tags:

views:

68

answers:

6

got this variable

 var zvmlist = { 
   'Huishoudelijke hulp': 'Huishoudelijke hulp', 
   'Verpleging thuis': 'Verpleging thuis',
   'Verzorging thuis': 'Verzorging thuis',
   '24 uurs zorg': '24 uurs zorg',
   'Ondersteunende begeleiding': 'Ondersteunende begeleiding',
 }; 

this var is used in a function to create a dropdownlist.

    $.each(zvmlist, function(key, value) { 
  var selected='';
  if(key==eventdata.title){var selected='selected' }
  $('<option value="'+key+'" '+selected+'>'+value+'</option>').appendTo($('#calendar_edit_entry_form_title')); 
  });

which works lika a charm. Now i created a function to fetch the list from a mysql table.

 $.get('get_zorgvormen.php', function(data) {

    var zvmlist = '{'+data+'}';
    //alert(zvmlist);

  });

when i enable the alert function it shows me the var(array). But i it wont act as a variable. How can i pass this data to my .each function?

see it in action here: http://www.zorgzuster-zeeland.nl/site/static/calendar_test.php

A: 

Instead of

var zvmlist = '{'+data+'}';

Try

var zvmlist = Eval('{' + data + '}');

Kindness,

Dan

Daniel Elliott
thanks Dan, but its not working
Ritz
I removed the static var and added $.get('get_zorgvormen.php', function(data) { var zvmlist = Eval('{' + data + '}'); //alert(zvmlist); });But still not working
Ritz
A: 

i think you have to evaluate that string so it becomes useful. Try:

var zvmlist = eval('{'+data+'}');
Sam3k
thanks Sam3k, but its not working
Ritz
A: 

You can parse JSON data using the eval() function.

$.get('get_zorgvormen.php', function(data) { 
   return eval('({' + data + '})');
}); 
Anthony Faull
How do pass this to the .each function then?
Ritz
A: 

Without using eval, you could create an object literal first, then pass that to zvmlist.

  $.get('get_zorgvormen.php', function(data) {
      //data = {"key":"value"};
      var obj = {};
      obj[0] = data;
      var zvmlist = obj[0];
      alert(zvmlist);
  });

not tested, but something like this.

danp
it doesnt pass the var zvmlist to the .each function
Ritz
how does the return from the .get look? is it a json object?
danp
A: 
 $.each(zvmlist, function(key, value) { 
        var selected='';
        if(key==eventdata.title){var selected='selected' }
        $('<option value="'+key+'" '+selected+'>'+value+'</option>').appendTo($('#calendar_edit_entry_form_title')); 
        });

when the var zvmlist= currently

var zvmlist = { 
      'Huishoudelijke hulp': 'Huishoudelijke hulp', 
      'Verpleging thuis': 'Verpleging thuis',
      'Verzorging thuis': 'Verzorging thuis',
      '24 uurs zorg': '24 uurs zorg',
      'Ondersteunende begeleiding': 'Ondersteunende begeleiding',
    }; 

but i want to get it populated by:

$.get('get_zorgvormen.php', function(data) {
      //data = {"key":"value"};
      var obj = {};
      obj[0] = data;
      var zvmlist = '{'+obj[0]+'}';
      alert(zvmlist);
    });

When i delete the static var the app breaks and wont populate the dropdown

Ritz
A: 

I combined both functions, it is kind of working except, the dropdownlist is populated with each letter from the var.

like

<option value="1">1</option>
<option value="2">'</option>
<option value="3"> </option>
<option value="4">:</option>
<option value="5"> </option>
<option value="6">'</option>
<option value="7">H</option>
<option value="8">u</option>

and so on

$.get('get_zorgvormen.php', function(data) {
      //data = {"key":"value"};
      var obj = {};
      obj[0] = data;
      var zvmlist = obj[0];  
     $.each(zvmlist, function(key, value) { 
        var selected='';
        if(key==eventdata.title){var selected='selected' }
        $('<option value="'+key+'" '+selected+'>'+value+'</option>').appendTo($('#calendar_edit_entry_form_title')); 
        });
     });

any help will be appriciated

Ritz