Phone API Reference - UI Classes (Widgets) - List - cellFunc
Phone API Reference - UI Classes (Widgets) - List - cellFunc
Â
cellFunc
Description
Function that takes row and column numbers as parameters and returns the text for a cell. When this property is set, the function is immediately executed.
Basic Example:Â
list.cellFunc = function(r, c) {return ""; }Â |
Use the cellFunc property for functions that take a row and column as parameters and return the string or image to be displayed at that position. This is more efficient because the list doesn't need to build a large data structure. The cellfunc will be called only for items that are on the screen.
The cellFunc option is recommended for large lists.
Examples
List.cellFunc to create a list of employee data
//clear the window and add a header
window.clear();
window.add(new Text(0, 0, 100, 100, "Employees"));
 Â
//add the list object
var list = new List(0, 20, window.w, window.h-20);
window.add(list);
 Â
//column widths will be 70, 70, and rest of the visible object
list.setColumnWidths(70, 70, 0); //last column will take the up as much space as possible
 Â
/* This data structure was created to take advantage of the cellFunc function and make filling out our List very easy. Rows will go in order, and are the index of the 'content' array. Each item in the content array is then an array. Each node of the inner array is a set of values which will correspond to the columns. This combination will be used to fill out all of our cells. */
var content = [
   ['--Emp.--', '--ID--', '--Country--'],
   ['Tom', '1231451', 'USA'],
   ['Ralph', '7567567', 'China'],
   ['Sam', '890294', 'Canada'],
];
 Â
//set cols and rows (rows equal to length of array
list.rows = content.length;
list.cols = 3;
 Â
// run over the cellFunc function accessing our content array each row and column at a time.
list.cellFunc = function (r, c) { return content[r][c]; };
 Â
//focus and select the first item in the list (not the header)
list.takeFocus();
list.select(1); |
Â
List.cellFunc to create a multiplication table
 //Create and add our List object
var listObj = new List(0, 20, window.w, (window.h - 20));
window.add(listObj);
 Â
// we will use this variable in a couple places. It represents how large our grid will be.
var num = 10;
 Â
//programmatically create a widths array which will be passed to setColumnWidths();
var widths = [];
while (widths.length < num) {
   //all the columns are even and will fill the whole screen
   widths.push(window.w/num);
}
 Â
//set rows and columns
listObj.rows = num;
listObj.columns = num;
 Â
//setColumnWidths takes a variable amount of arguments, that is why apply is used here.
listObj.setColumnWidths.apply(listObj, widths);
 Â
//set the cellFunc to return the row multiplied by the column (e.g. row 3, column 4 = 12)
listObj.cellFunc = function(r, c) { return r*c; };
 Â
//focus on the list so we can scroll
listObj.takeFocus(); |
Â