1

This is the same question as here: Cartodb - SQL query of public visualization of a private table

We have a private table and would like to use setSQL method to query the sublayer as we did with private data. I have created a basic template per documentation but what's next? Do I have to use this template instead of visualization? I can't get how the concept of the named maps and all that template stuff is related to the actual application code in Javascript. I have seen that you can have placeholder for SQL in the template but how is this placeholder filled from the application side in this case? We want to query by one or multiple fields. I just generated the code dynamically based on the filter object collecting values from UI elements (field1 and field2 are text and field3 is numeric with min and max values):

var filterFunction = function() {

        ////default query which is TRUE for all rows
        newQuery = "SELECT * FROM mytable WHERE 1=1";

        ////collect filter parameters and append them to WHERE clause in SQL
        if (filter.field1!="undefined"){
            newQuery = newQuery + " AND field1='"+filter.field1+"'";
        };
        if (filter.field2!="undefined"){
            newQuery = newQuery + " AND field2='"+filter.field2+"'";
        };
        if (filter.field3.min!="undefined"){
            newQuery = newQuery + " AND field3>="+filter.field3.min;
        };
        if (filter.field3.max!="undefined"){
            newQuery = newQuery + " AND field3<="+filter.field3.max;
        };

        ////send query to the layer
        sublayers[0].setSQL(newQuery);
        return true;
    };

So, how do we convert this to work with named map? I'm lost.

1 Answer 1

1

You can NOT change the query in a named map that access to private tables. The reason is pretty simple: if you allow to change the query any user could get/render all the data just changing the SQL.

What named maps allow to have different queries is templates. You can setup some variables that you can change, so your query could look like:

select * from table where column = <%= variable %>

take a look at the documentation: http://docs.cartodb.com/cartodb-platform/maps-api.html#create

2
  • yes I was looking at this docs but I don't understand what do I have to do with the application code once I create the template... do I pass this template as metadata parameter into cartodb.createLayer function?
    – AlexYes
    Nov 14, 2014 at 10:51
  • take a look at docs.cartodb.com/cartodb-platform/… Nov 15, 2014 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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