I have an each statement like this:
$.each(data, function(i, value) {
sublayers.push({
sql: "SELECT " + firstSel2 + ", cartodb_id, the_geom_webmercator FROM full_data_for_testing_deid_2 where " + firstSel2 + "='" + value.attri_1 + "'",
cartocss: "#full_data_for_testing_deid_2 {marker-fill:"+color_here+";marker-width:5;marker-line-width: 0.2;marker-line-color:#fff;marker-allow-overlap: true;}"
});
});
In the cartocss portion, there's a variable color_here. This is what I need to replace. This needs to be a hex value from an object like this:
var myColors = ["#364C57",
"#666DD6",
"#867EAD",
"#1A3D76",
"#7F787F",
"#35304C",
"#1D5772",
"#15446B",
"#7382C0",
"#484A48",
"#454252",
"#333C6F"];
What I need is for the each statement to pull out one of these colors (starting from the top) for each value that it loops through without repeating. Assume that there will never be more loops than there are colors in my object above. Is there a way to do this that ensures the colors don't repeat for a given each statement? I can come up with something that randomly picks from the list, but it would result in the possibility of a duplicate.
Seems that you need just to select the color from the array according to the current index:
var color_here = myColors[i];
doesn't it? Place it before sublayers.push().
Maybe like this what you want:
var color_curr;
var myColors = ["#364C57",
"#666DD6",
"#867EAD",
"#1A3D76",
"#7F787F",
"#35304C",
"#1D5772",
"#15446B",
"#7382C0",
"#484A48",
"#454252",
"#333C6F"];
$.each(data, function(i, value) {
var color = myColors[i];
if(color!=color_curr){
sublayers.push({
sql: "SELECT " + firstSel2 + ", cartodb_id, the_geom_webmercator FROM full_data_for_testing_deid_2 where " + firstSel2 + "='" + value.attri_1 + "'",
cartocss: "#full_data_for_testing_deid_2 {marker-fill:"+color_here+";marker-width:5;marker-line-width: 0.2;marker-line-color:#fff;marker-allow-overlap: true;}"
});
}
color_curr=color;
});
Related
We are working on an ubuntu server, where we have installed node-red. What we want is to take data from one table on our MySQL Database, and then move it to another table.
Our flow looks like this:
Our 'Select Tolerance' node contains:
msg.topic = "SELECT * FROM Tolerance";
return msg;
Simple code, which selects data from our database. If we connect a debug node like this:
We then see an output looking like this:
We want to pick all data, so we need to go through all objects in the array and make sure to take all values and send them over to our new database table. We're using the 'New Tolerance' node to do so, and 'New Tolerance' contains:
var i;
var secured = 1;
for (i = 0; i < msg.payload.length; i++) {
var time_start = msg.payload[i].time_start
var time_end = msg.payload[i].time_end
var temperatur_lpn = msg.payload[i].temperatur_lpn
var temperatur_rising = msg.payload[i].temperatur_rising
var temperatur_weather = msg.payload[i].temperatur_weather
var temp_compare_WL = msg.payload[i].temp_compare_WL
var temp_compare_WR = msg.payload[i].temp_compare_WR
var out = "INSERT INTO Sunrise_Tolerance (time_start, time_end, temperatur_lpn, temperatur_rising, temperatur_weather, temp_compare_WL, temp_compare_WR, secured)"
out = out + " VALUES ('" + time_start + "','" + time_end + "','" + temperatur_lpn + "','" + temperatur_rising + "','" + temperatur_weather + "','" + temp_compare_WL + "','" + temp_compare_WR + "','" + secured + "');"
msg.topic = out;
return msg;
}
The problem is that we only receive the first row of data (first object in the array) and not the rest. Can anyone figure out why we dont receive all data, but only the first?
The problem comes the fact you have a return statement inside the for loop.
That will exit the function the first time it reaches that point - so your loop never loops.
If you want to send multiple messages you should use node.send(msg); in your for loop. The only thing to watch out for is if you call node.send with the same object multiple times, as the message is passed by reference, you would get some odd side-effects. As you only care about msg.topic in this instance, you can afford to create a new message object each time.
So, instead of the return msg statement, you could do:
for (i ... ) {
var out = "INSERT ...";
...
node.send({topic: out});
}
// Return without any arguments so no further messages are sent.
return;
I am trying to do nested queries in order to insert into a table based apon an earlier select statement. However I am running into trouble because my first select statement selects and AVG() of a row. I have not been able to find a way to get the result row object that I have to select the property 'AVG(row)' instead of the trying to call .AVG() on something. The code is below and any help would be appreciated.
var sql1 = 'SELECT tropename, AVG(criticScore) FROM tropesWithScore where tropeName = '+ '\'' + trope + '\'';
//console.log(sql1)
con.query(sql1, function (err1, result1) {
if (err1) throw err1;
Object.keys(result1).forEach(function(key) {
var row2 = result1[key];
var trope2 = row.tropeName;
console.log(trope2)
var avgScore = row.AVG(criticScore)
console.log(avgScore)
sql = 'INSERT INTO TropesWithAverageScores (tropeName, criticScore) VALUES (' + trope2 + ',' + '\'' + avgScore + ')';
///console.log(sql)
con.query(sql, function (err, result) {
if (err) {}
});
});
});
Fixed it myself,
first, I was getting the attribute on row, not row2 object.
Secondly I simplified it by aliasing my first select state, so it now reads
SELECT tropename, AVG(criticScore) as avgCS FROM tropesWithScore where tropeName = '+ '\'' + trope + '\'';
Hope this helps someone else!
I have a function that I need to use for filtering table rows:
setFilterString("Filter");
But I have a problem. I can set it to
setFilterString("OrderID = 5");
and it will filter out row where OrderID is equal to 5 but if i try using a variable that has a value taken before like this
setFilterString("OrderID = vOrderID");
I get error "Invalid column name 'vOrderID'." (as vOrderID is variable and not a column, I guess)
I have seen somewhere in filter section inputting something like this ("OrderID = '" & vOrderID & "'") but it doesn't have any result at all for me. Doesn't even throw any error in the console.
JavaScript assumes you are just passing a string to the function. If you want to use the variable, you should try this:
setFilterString("OrderID = '" + vOrderID + "'"); // Results in OrderID = '5'
or
setFilterString("OrderID = " + vOrderID); // Results in OrderID = 5
depending on the body of your function.
Use + instead of &: setFilterString("OrderID = " + vOrderID) should work.
Use "+" for merge strings:
setFilterString("OrderID = " + vOrderID)
You can also try to use ${idvOrderID} inside string:
setFilterString("OrderID = ${vOrderID}")
Or:
setFilterString(sprintf("OrderID = %s", vOrderID))
Remember about difference between ' and "
I am trying to eventually have my json data displayed in a label. However, when I console.log the json data, only the last two objects are displayed. When I pass the data to a label, only the last object is displayed. Thanks in advance for any help!
Here is my code:
var json =
{
"Question:": " What is my name? ",
"Answer:": " James ",
"Question:": " What is my age? ",
"Answer:": " 31 "
};
for (var key in json)
{
if (json.hasOwnProperty(key))
{
console.log(key + " = " + json[key]);
}
}
var label = Ti.UI.createLabel({
text: key + json[key]
});
win3.add(label);
Your issue has nothing to do with Titanium. In JavaScript dictionary you can't have two same keys with different values. To demonstrate where you made mistake, I'll rewrite your first lines:
var json = {};
json["Question:"] = " What is my name? ";
json["Answer:"] = " James ";
// We are fine untill now.
json["Question:"] = " What is my age? ";
json["Answer:"] = " 31 ";
// Here you overwrote values for keys "Question:" and "Answer:" which were set above.
To fix your problem, I'd change your json dictionary into array of dictionaries:
var i, key, label;
var json = [
{
"Question:": " What is my name? ",
"Answer:": " James ",
},
{
"Question:": " What is my age? ",
"Answer:": " 31 "
}
];
for (i in json) {
for (key in json[i]) {
label = Ti.UI.createLabel({
text: key + json[i][key]
});
win3.add(label);
}
}
your json object key is duplicated, javascript does not complain from this, it will just overwrite the first key value with the second value
I am writing a menu system and I need to call a function when the user clicks on a specific row. I am using new function to pass the div that called the function as well as the row the user clicked on. All well and good until I try to pass a local array to the function. If I do the following:
for (i=0;i<tmpdropnumber;i++){
var dv=document.getElementById(id+i);
dv.style.cursor="pointer";
dv.onmouseover = new Function('dropover'+"('" + id + "','" + i + "')");
dv.onmouseout = new Function('dropout'+"('" + id + "','" + i + "')");
dv.onclick = new Function('dropclick'+"('" + id + "','" + i + "','"+tmparray1+"','"+tmparray2+"')");
}
As you'd expect the arrays are passed as strings. I could rebuild the arrays in the function but that seems inelegant.
if I try the following:
for (i=0;i<tmpdropnumber;i++){
var dv=document.getElementById(id+i);
dv.style.cursor="pointer";
dv.onmouseover = new Function('dropover'+"('" + id + "','" + i + "')");
dv.onmouseout = new Function('dropout'+"('" + id + "','" + i + "')");
dv.onclick = new Function('dropclick'+"('" + id + "','" + i + "',"+tmparray1+","+tmparray2+")");
}
Trying to pass the arrays it crashes. Any ideas on how I can achieve this? I am using jquery in my code so wither a javascript or jquery solution would be fine.
Do not use new Function.
You are having problems because when the string is being built, the array is being turned into a string.
basic idea to get around the toString()
for (i=0;i<tmpdropnumber;i++){
var dv=document.getElementById(id+i);
dv.style.cursor="pointer";
(function(id, i){
dv.onmouseover = function(){ dropover(id,i); };
dv.onmouseout = function(){ dropout(id,i); };
dv.onclick = function(){ dropclick(id,i, tmparray1, tmparray2); };
})(id,i);
}
But in reality there is no need to pass in an object, id.
You can always use this to get the current row and there is rowIndex on the table. Heck you can have one event handler on the table and use handlers on the table/tbody to capture the bubbling and use target/srcElement.
Instead of
dv.onclick = new Function('dropclick'+"('" + id + "','" + i + "','"+tmparray1+"','"+tmparray2+"')");
try
dv.onclick = function() {
dropclick(id, i, tmparray1, tmparray2);
};
Edit
Wrap your declaration in a anonymous function:
(function(id, i) {
dv.onclick = function() {
dropclick(id, i, tmparray1, tmparray2);
};
} (id, i));