1

I have a visualization in CartoDB of a set of data points in table pointlayer. I am using cartodb.js and have a nice map for browser consumption.

When a user clicks on a point, I'd like to run a sql query that returns multiple rows relating to the clicked-upon point, and I'd like to put all that into the infowindow.

select column_a column_b, etc
from anothertable
  join pointlayer
  on pointlayer.point_id = anothertable.point_id
where
  pointlayer.point_id = <cartodb_id of clicked on point>

anothertable may have 0 or more rows for any point_id

Is this possible with infowindows? Looks to me like the answer is probably no: they are designed for a single row. I suspect that I need to find a solution other than using CartoDB's infowindow (e.g. using Leaflet's popup), but I thought I would ask.

Getting info from multiple rows in single popup of CartoDB? suggests to "actually query the table and get back all data." It doesn't state that the infowindow itself can be used to display it...though I could be reading it incorrectly.

1

Yes, you can do it with CartoDB.js. This link will help you: http://bl.ocks.org/xavijam/378c6dc0d43654280757

  • I might be missing something here, but it looks like this example is joining two tables with a one-to-one relationship and returning a single row comprised of a field or two from each table. I have a one-to-many relationship and need to retrieve multiple rows (one for each of the "many" table); each row is comprised of a few fields from each table. All rows will be displayed in the infowindow – susie derkins Dec 10 '14 at 16:13
0

You could probably use PostgreSQL aggregate functions to solve this. Assuming your columns are strings, you could just use string_agg(expression, delimiter) on the query in order to retrieve several columns in just one row.

  • I appreciate the suggestion - I'm relatively new to PostgreSQL and hadn't come across string_agg before, and I can see it's potential usefulness in similar situations. However, it's important for me that my returned data actually is multiple rows - I'm using them for various purposes for which it's much simpler if they are separate rows. – susie derkins Dec 10 '14 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.