开发者

Facebook Page Array to Select Box

I am grabbing the facebook pages a user admins and displaying them in a select box through the Facebook JS SDK.

What's the best way to take the array I get back from facebook response.data and iterate so it goes into the select?

  <div id="fb-root"></div>
  <script src="http://connect.facebook.net/en_US/all.js">
  </script>
  <script>
    FB.init({ 
    appId:'119406238144386', cookie:true, 
    status:true, xfbml:true 
    });
    FB.api('/me/accounts/', function(response) {
      alert(response.data);
    });
   </script>
   <select id="pages"></select>
  <fb:login开发者_StackOverflow-button perms="manage_pages">Connect to Facebook</fb:login-button>


Facebook returns the object in an object array, so just loop through each one and add a new html object item to the select list.

A full working example without jQuery:

<!DOCTYPE html>
<html>
<body>
<div id="fb-root"></div>
<a href="#" onclick="getPages();return false;">Get Pages</a>

<select id="pages" style="display:none;"></select>

<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
  FB.init({ appId: 'your app id', status: true, cookie: true, xfbml : true });

  function getPages() {  
    FB.login(function(response) {
      if (response.session && response.perms) {
        FB.api('/me/accounts/',  function(response) {
            var pages = document.getElementById('pages');
            pages.style.display = 'block';
            for(var i =0; i < response.data.length; i++)
            {
              pages[i] = new Option(response.data[i].name);
            }
          }
        );
      }
    } , {perms:'manage_pages'}); 
}
</script>
</body>
</html>


FB.api('/me/accounts/', function(response) {
  for i in response.data {
    var option = $('<option></option>')
                    .attr("val",response.data[i])
                    .html(reponse.data[i]);
    $('#pages').append(option);
  }
});

i'm just assuming response.data is giving back an array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜