Updating a dynamically created ImageView with Appcelerator - appcelerator

I'm trying to do something I think is simple, but just having a few issues on iOS using Titanium 5.2.2 SDK.
So, I have an app that pulls down a list of Employees from a JSON file hosted outside the app, I cache all that data in an onboard DB and create a masked Image for each employee inside my loop.
employeeImage[i] = Titanium.UI.createMaskedImage({
mask : employee_image, // background image
tint: 'black',
mode : Titanium.UI.iOS.BLEND_MODE_COLOR,
width: 210,
height: 210,
touchEnabled: false,
custom_employee_id: employee_id
});
I need to be able to refer to that Image and update the tint from 'black' to something 'else' elsewhere in my app, so I'm not having to do constant look ups and refreshing every single image.
I want to do something like this:
// get the employee status
var db = Ti.Database.open('Enter');
var getEmp = db.execute('SELECT employee_id, employee_status FROM Employee');
while (getEmp.isValidRow())
{
var empID = getEmp.fieldByName('employee_id');
var empStatus = getEmp.fieldByName('employee_status');
Ti.API.info(empID + ' ' + empStatus);
// update the images correctly
if (empStatus == 0) {
// update the image mask to black if they are now out of the building
Ti.API.info('Status is zero');
//employeeImage[i].tint = 'black';
} else {
// update the image mask to transparent if they are now in the building
Ti.API.info('Status is ONE');
//employeeImage[i].tint = 'transparent';
}
getEmp.next();
}
getEmp.close();
db.close();
The problem is, I don't know if I can set those changes to the image view if it has a dynamic name.
If it were just called employeeImage1 this would be easy. However, if I try call employeeImage1 instead of employeeImage[i] I get this error.
"Can't find variable: employeeImage1";
How do I get around this?
Any ideas?

There is no doubt that you can store your objects as array elements. I've reduced your sample to the following testcase:
(function(){
var count=10;
var i=0;
var employeeImage=[];
for (i=0;i<10;i++){
employeeImage[i]=Ti.UI.createButton({
title : "Employee " + i
});
}
for (var i=0;i<10;i++){
console.log(employeeImage[i].title);
}
})()
The output for this is what you'd expect:
[INFO] Employee 0
[INFO] Employee 1
[INFO] Employee 2
[INFO] Employee 3
[INFO] Employee 4
[INFO] Employee 5
[INFO] Employee 6
[INFO] Employee 7
[INFO] Employee 8
[INFO] Employee 9
Now, as array elements, you should call it as employeeImage[n] and not as employeeImageN, because employeeImageN doesn't exists, what exists is a reference to your object stored as array elements.
Does this make sense.
R

Related

jqgrid get rows in grid after move rows

I have a grid with 3 columns that looks like:
col1 col2 col_sortorder
AAAA 1000 1
AAAA 1002 2
AAAA 1003 3
I made it possible that the user can change the sortoder in the grid by using the mouse. For example move the second row on the top, so the grid looks like:
col1 col2 col_sortorder
AAAA 1002 2
AAAA 1000 1
AAAA 1003 3
I achieved this with:
jQuery("#list").jqGrid('sortableRows');
jQuery("#list").bind('sortstop', function() { fun_sort(event) });
Now I want to update my database with a new value for the col_sortorder.
The function fun_sort() is triggered by the sortstop-event correctly.
My intention was just to read all the data from the grid and use the forloop-index as the new value for the col_sortorder, but when I read through my grid using:
var allRowsInGrid = $('#list').jqGrid('getGridParam','data');
for (i = 0 ; i <= allRowsInGrid.length -1; i++){
var col1 = allRowsInGrid[i].col1;
var col2 = allRowsInGrid[i].col1;
var col_sortorder = i+1; //new value for sortorder
// call ajax to update the database
}
The function getGridParam always returns the initial grid order and not the order after I have moved a row inside the grid.
Can somebody tell me how I can to this?
I find your question interesting and thus I created the demo https://jsfiddle.net/OlegKi/xw0gcjez/, which demonstrates how you can solve the problem. I used update callback of sortableRows, which is the same as "sortupdate" event (see the documentation).
$("#list").jqGrid("sortableRows", {
update: function () {
updateColSortorder();
// the data of the column col_sortorder will contain
// now sequensial values 1,2,3...
// even the display values are still old
// reload grid to display updated data
var p = $grid.jqGrid("getGridParam");
// we reset sortname to "col_sortorder" only to reload
// with minimal visual changes for the user
p.sortname = "col_sortorder";
p.sortorder = "asc";
setTimeout(function () {
$grid.trigger("reloadGrid");
}, 0);
}
});
where updateColSortorder is
function updateColSortorder () {
var rows = $grid[0].rows, localRow, i;
for (i = 0; i < rows.length; i++) {
if ($(rows[i]).hasClass("jqgrow")) {
// row is a row with data. row.id is the rowid
localRow = $grid.jqGrid("getLocalRow", rows[i].id);
localRow.col_sortorder = i;
}
}
}
The grid uses HTML table internally. Thus $grid[0] is the DOM of table, which has rows properties. Every row has id property and so on. The order of elements in the rows collection corresponds the order in which the rows are displayed.

Is there a way to mass input data validation in google sheets

I'm trying to create a drop down menu with contents based on a another cell in the same row. For example if A1 = 'yes' then the drop down in B2 gives you the options of 'yes' or 'no'. I can do this I have the list data set up and to code works. The problem is I need to do this 155 times in 4 different sheets. Is there a faster way to do this than right clicking and editing the data validation rules for each cell. Here's a link to the test sheet I'm working on :
https://docs.google.com/spreadsheets/d/1rd_Ig_wpof9R_L0IiA1aZ9syO7BWxb6jvBhPqG8Jmm4/edit?usp=sharing
You can set data validation rules with a script, as documented here. Here's a reference for starting with Apps scripts.
I wrote a function that does approximately what you described. It works with the range B3:B157 of the sheet '9th grade' in the current spreadsheet. For each of them, it sets the validation rule to be: a value in the same row, columns B and C of sheet 'List Data'. The line with
....... = listData.getRange(i+3, 2, 1, 2);
will need to be modified if the source range of validation is to be different. Here, the parameters are: starting row, starting column, number of rows, number of columns. So, 2 columns starting with the second, in row numbered i+3.
function setRules() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var grade = ss.getSheetByName('9th Grade');
var listData = ss.getSheetByName('List Data');
var range = grade.getRange('B3:B157');
var rules = range.getDataValidations();
for (var i = 0; i < rules.length; i++) {
var sourceRange = listData.getRange(i+3, 2, 1, 2);
rules[i][0] = SpreadsheetApp.newDataValidation().requireValueInRange(sourceRange).build();
}
range.setDataValidations(rules);
}
I land in this issue for a diferent reason: "Just mass DataValidation copy (or update) in one column". Thanks, to user3717023 that bring me a light.
I hope that helps someone this simplification.
function setRules() {
//select spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var leads = ss.getSheetByName('Leads');
//Select correct Datavalidation
var rangeNewDataValidation = leads.getRange('M2:M2');
var rule = rangeNewDataValidation.getDataValidations();
//Copy (or Update) Datavalidation in a specific (13 or 'M') column
var newRule = rule[0][0].copy();
Logger.log(leads.getMaxRows())
for( var i=3; i <= leads.getMaxRows(); i++){
var range = leads.getRange(i, 13);
range.setDataValidations([[newRule.build()]]);
}
}

random images in corona sdk

I have 3 images and 1 button -
I want to be able to click my button and have 1 of the 3 images appear. And everytime I click the button i want a new random image to appear in the place of the last image......Pretty simple it would seem, but Im losing hair over this and am about to call it quits......Can anyone help me do this? I want to learn, so please comment the code if you decide to help me....Thanks in advance.
So far, I have:
display.setStatusBar( display.HiddenStatusBar ) -- hide status bar
--insert background
local bgImg = display.newImageRect( "images/myBG.jpg", 625, 450 )
bgImg.x = display.contentCenterX -- center bg on X
bgImg.y = display.contentCenterY -- center bg on Y
-- scripture references
myTable = {
display.newImage("images/btnLogo1.png"),
display.newImage("images/btnLogo2.png"),
display.newImage("images/btnLogo3.png"),
}
randomPicture = myTable[math.random(1,3)]
This should work:
-- scripture references
myTable = {
"images/btnLogo1.png",
"images/btnLogo2.png",
"images/btnLogo3.png",
}
local randomPicture = myTable[math.random(1,3)]
display.newImage(myTable[randomPicture])
I hope you need no explanation about it :)
If your image names are continous, that is like img_1,img_2,img_3. etc... then you can use the following method:
-- Display an image
local myImage = display.newImageRect("images/btnLogo1.png",50,50)
myImage.x = display.contentWidth/2
myImage.y = display.contentHeight/2
-- Call this function on button click
function imageChangeFunction()
-- remove the previous image
if(myImage)then myImage:removeSelf() end
-- creating the sprite with new image
myImage = display.newImageRect("images/btnLogo"..math.random(3)..".png",50,50)
myImage.x = display.contentWidth/2
myImage.y = display.contentHeight/2
print("Image changed...")
end
-- Here I am assigning the listener to Runtime, you can change it for your button
Runtime:addEventListener("tap",imageChangeFunction)
Note:
math.random(3) gives you any random number between 1 and 3.
.. is used for concatenation. So, "images/btnLogo"..math.random(3)..".png" will give you any of the following strings:
images/btnLogo1.png
images/btnLogo2.png
images/btnLogo3.png
For more info, visit: math.random() and Corona String Operations

Find&Replace script in Google Docs SpreadSheets

I have google spreadsheet with direct links to images (jpg and png):
https://docs.google.com/spreadsheet/ccc?key=0AoPGWppcjtzhdDh6MW1QNVJhSHlwVTlfRnRtd0pvNGc&usp=sharing
I want to increase rows heights starting from "2nd row" to 100px and render images there.
It's possible to do via Find&Replace:
Find jpg and Replace to jpg", 1)
Find http://img and Replace to =image("http://img)
Select rows and Scale them
and the same for png image-urls.
Watch this screencast http://www.screenr.com/S0RH
Is it possible to automate it via script? I think - YES! It have to be pretty simple but I googled a lot but haven't found the solution. I can't do it myself as don't know coding. Will anyone help and make this script?
A function to do what you ask is simple, if you have a basic understanding of the language (Javascript), know how to use the development environment, and read the API documentation.
For example, see this script. It's been added to your shared spreadsheet, so you can also view it (and run it) in the script editor there.
/**
* Scan column A, looking for images that have been inserted using
* =image() function. For any row with an image, set the row height
* to 100 pixels.
*/
function resizeImageRows() {
var sheet = SpreadsheetApp.getActiveSheet(); // Get a handle on the sheet
var HEADERS = 1; // Number of header rows at top
var firstRow = HEADERS + 1; // First row with data
var lastRow = sheet.getLastRow(); // Last row with data
var imageRange = sheet.getRange(1, 1, lastRow, 1); // Column A
// Get all formulas from Column A, without Headers
var formulas = imageRange.getFormulas().slice(HEADERS);
// Look for image() formulas, and set the row height.
for (var i = 0; i< formulas.length; i++) {
if (formulas[i][0].indexOf('image') !== -1) {
sheet.setRowHeight(i+firstRow, 100); // Set height to 100 pixels
}
}
}
You can absolutely do this with the find and replace function under the edit menu, just make sure you click "search in formulas" and it will find and replace in the formula.

Greasemonkey script to find rows with certain conditions

I tried some different ways do find rows in a table where a columns contain a particular link.
My goal: replace an icon when a link to xyz is in this same row as the image.
This is my snippet so far:
var rows = document.getElementsByTagName("tr");
for(var i = rows.length - 1; i >= 0; i--) {
var links = rows[i].getElementsByTagName("a");
for(var k = links.length - k; k >= 0; k--) {
if (links[k].href =="http://www.XXXX.net/forum/index.php?showforum=121"){
var images = rows[i].getElementsByTagName("img");
for (var j=0;j<images.length;j++) {
images[j].src = "http://www.XXXX.net/forum/folder_post_icons/icon7.gif";
}
}
}
}
I'm pretty sure this is not really the best concept. But as you might see I try to search links in all rows and once the link to forum "121" is found, I try to replace all images in this particular row.
What I get is every image at the site getting replaced.
Since it's simple enough, here's a complete script that does that.
It uses jQuery and here's a handy jQuery reference. See, especially, the Selectors section (which are almost the same as CSS Selectors).
Re: "What I get is every image at the site getting replaced." ...
This maybe because the search criteria is too broad. If it's a poorly designed (uses table layouts) page, every image may be in a table row with a target link!
When posting Greasemonkey questions, link to the target page, or at the very minimum, post enough of the page's HTML that we can adjust the GM script to match.
Anyway, this will work, possibly pending more information about the target page:
// ==UserScript==
// #name _Replace image on custom-targeted row
// #include http://www.XXXX.net/forum/*
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// ==/UserScript==
//--- This may need tuning based on information not provided!
var targetLinks = $("tr a[href*='showforum=121']");
//--- Loop through the links and rewrite images that are in the same row.
targetLinks.each ( function () {
//--- This next assumes that the link is a direct child of tr > td.
var thisRow = $(this).parent ().parent ();
//--- This may need tuning based on information not provided!
var images = thisRow.find ("td img");
//--- Replace all target images in the current row.
images.each ( function () {
$(this).attr (
'src',
'http://www.XXXX.net/forum/folder_post_icons/icon7.gif'
);
} );
} );

Resources