rsanchez / json

Output ExpressionEngine data in JSON format.
http://devot-ee.com/add-ons/json/
101 stars 234 forks source link

Getting data from Playa fields #29

Closed elsmore closed 10 years ago

elsmore commented 10 years ago

Is there some way of getting data from Playa fields instead of the entry_id for the related entry?

Thanks in advance.

rsanchez commented 10 years ago

The only way to do it is in a separate ajax request/template using the entry ids provided in the parent entry.

elsmore commented 10 years ago

Ok, thanks for your quick reply.

elsmore commented 10 years ago

I don't suppose you have an example of how I might implement this? Please excuse my ignorance when it comes to AJAX!

rsanchez commented 10 years ago

I’ll try to show a brief example.

First template (site/main_json):

{exp:json:entries channel=“your_main_channel”}

Second template (site/related_json):

{exp:json:entries channel=“your_related_channel” dynamic_parameters=“entry_id"}

Ajax:

$.ajax({
  url: ‘/site/main_json’,
  dataType: ‘json’,
  success: function(data) {
    if (data.your_playa_field) {
      $.ajax({
        url: ‘/site/related_json’,
        dataType: ‘json’,
        data: {
          entry_id: data.your_playa_field.join(‘|’) // EE requires its parameters as pipe delimited strings
        },
        function(data) {
          // this will be a JSON object of your related entries
        }
      });
    }
  }
});

I hope that helps. That’s about the best I could do!

elsmore commented 10 years ago

Thanks Rob, I appreciate your help!