Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I am making a map with Carto.js and I am trying to update a SQL query depending on what the user has selected from a dropdown menu. For example, if the user selects "change to fruit production", then the layer's SQL query should change to:

SELECT * FROM indicators WHERE fruit ILIKE 'Yes'


What I have tried:

Carto has recently updated, so there entire .js API formatting has changed. Using their docs, this is how I have added the SQL layer so far:

JavaScript
//Add SQL layer
		const indicatorsSource = new carto.source.SQL(`
		  SELECT *
		    FROM indicators WHERE grain ILIKE 'Yes'     
		`);		

		const indicatorsStyle = new carto.style.CartoCSS(`
		  #layer {
		    polygon-fill: #50ae31;
    		polygon-opacity: 0.65;
		  }
		`);
		const indicators = new carto.layer.Layer(indicatorsSource, indicatorsStyle);


As you can see, I have no idea how to change the feature variable ("grain" in the example) depending on what the user has been selected.
Posted
Updated 16-Jun-18 5:12am

1 solution

Is it the string delimiter that's the issue here? To embed single quotes in a string, use double quote on the extremes of the string.
const indicatorsSource = new carto.source.SQL("
		  SELECT *
		    FROM indicators WHERE grain ILIKE 'Yes'     
		");

However I don't know of a 'grain' named 'Yes'. Grains are wheat, rye, oat. Oh, and now that I think about it... where's the wildcard that LIKE and ILIKE are supposed to use? Getting grain starting with 'w' would be this

const indicatorsSource = new carto.source.SQL("
		  SELECT *
		    FROM indicators WHERE grain ILIKE 'w%'     
		");

Is that more what you're looking for?

HTH,
Mike
 
Share this answer
 
Comments
[no name] 16-Jun-18 11:33am    
Not sure just speculation: I think he is more looking for how to handle different column name "grain"/"fruits" in the WHERE part of his SQL

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900