When you add an event to the TableViewRow, you don't get the source of which tableviewrow was pressed. The customer wants to select/deselect a checkbox in a row, just with clicking on the row.
The event is not getting the source of the event.
1. Create a new mobile Project
2. Paste this:
var fundListSize,fundCheckboxArray=[],fundsToPlotCount,grey=false,lightBlue=false,clickEvent=false,fundsSitemanagerMsg=null,rowCounter=0;
var win = createHistoricalFundGraphFundsWindow();
win.orientationModes = [Titanium.UI.LANDSCAPE_LEFT, Titanium.UI.LANDSCAPE_RIGHT];
win.open()
/*
* Function to display the available funds that can be selected to display on the graph
*/
function createHistoricalFundGraphFundsWindow() {
/* Declaring the variables*/
var iOSTopTitle, planText, navTitleText, menuTitle, winLeftNavButton, winRightNavButton, winBackButtonTitle, winBackButtonTitleImage, winTabBarHidden, subHeader, tableViewDS, genericTableView, win;
fundsSitemanagerMsg = ''//_response.historicalFundGraphFundMessage;
fundsToPlotCount = 3;
/* reset fundlistsize*/
fundListSize = 0;
tableViewDS = createHistoricalFundGraphFundsTableViewDS();
genericTableView = Titanium.UI.createTableView({
separatorColor:'#e7e7e7',
data : tableViewDS,
backgroundColor :'#e7e7e7'
});
genericTableView.top = 0;
genericTableView.backgroundColor = '#e7e7e7';
tableViewDS = null;
var menuWindow = Titanium.UI.createWindow({
barColor : '#929292',
backgroundColor : '#E7E7E7'
});
menuWindow.add(genericTableView);
return menuWindow;
};
/* Event invoked after clicking done button if the fund does not have any data */
Ti.App.addEventListener('resetCheckBox', function(e) {
for(var i=0;i<fundListSize;i++) {
if (fundCheckboxArray[i].fundName === e.fundName && fundCheckboxArray[i].value) {
fundCheckboxArray[i].off();
}
}
});
/*
* Function to create the data source to be added to the tableview that displays the funds
*/
function createHistoricalFundGraphFundsTableViewDS() {
var responseLength=2;
var datasource=[];
var datasourceChecked=[];
var fundRow,boxChecked;
boxChecked=false;
fundCheckboxArray.push(createHFGCheckBox('','xyz',boxChecked));
fundCheckboxArray.push(createHFGCheckBox('','abc',boxChecked));
fundRow=createHFGRowLndScpe({
fFundName: 'xyz',
sSwitch: fundCheckboxArray[0]
});
var fundRow1=createHFGRowLndScpe({
fFundName: 'abc',
sSwitch: fundCheckboxArray[1]
});
datasource.push(fundRow);
datasource.push(fundRow1);
fundListSize = 2;
return datasource;
};
/*
* function to create checkbox ( a button masked as checkbox, not a switch ) for each row
*/
function createHFGCheckBox(_fundNum,_fundName,_checked) {
var checkbox = Ti.UI.createButton({
title:'',
right:'5%',
width:28,
height:28,
font: {
fontSize: 25,
fontWeight: 'bold'
},
value:_checked,
fundNum:_fundNum,
fundName:_fundName,
checkColor:'none'
});
/* attach on/off actions */
checkbox.on = function() {
this.value = true;
};
checkbox.off = function() {
this.value = false;
};
/* fire action to check or uncheck based on _checked bool param */
if (_checked) {
checkbox.on();
} else {
checkbox.off();
}
checkbox.addEventListener('click', function(e) {
/* can always turn checkboxes off */
alert(e)
});
return checkbox;
};
/*
* Function to create row for fund label
*/
function createHFGRowLndScpe(_fundBalData) {
if( typeof _fundBalData !== 'undefined' && _fundBalData !== null) {
var _fundName = _fundBalData.fFundName;
var row = Titanium.UI.createTableViewRow({
backgroundColor : '#FFFFFF',
selectedBackgroundColor : '#FFFFFF',
selectionStyle : 'NONE'
});
var checkBoxView = Ti.UI.createView({
right : 0,
height : 30,
width : '30%',
uniqueRowId : rowCounter
})
row.addEventListener('click', function(e) {
var id = e.source.uniqueRowId;
if( typeof id !== 'undefined' && id !== null) {
fundCheckboxArray[e.source.uniqueRowId].fireEvent('click');
}
});
var fundNameLabel = Titanium.UI.createLabel({
left : '5%',
width : '65%',
top : 15,
bottom : 15,
height : 'auto',
color : '#363636',
text : _fundName,
font : {
fontSize : 15,
fontFamily : 'Helvetica Neue',
fontWeight : 'bold'
},
uniqueRowId : rowCounter
});
rowCounter += 1;
row.add(fundNameLabel);
_fundName = null;
checkBoxView.add(_fundBalData.sSwitch);
row.add(checkBoxView);
return row;
}
};
This used to work properly in 1.7.6, but now in 2.1CI it's not working as before.
The source of the event is always available. Tested with current master and 2_0_X branch. The problem is the way the code is written. The click event handler for the tableView row can have 4 possible sources (button,label,row,checkBoxView). However the uniqueId is associated with only 2 elements(label,checkBoxView). So the if condition fails for 2 possible sources. From the way the code is written, the uniqueId is completely un-required since it simply is the row index which is always available via e.index. Also if you fire a
click
event to a child of the row from within theclick
event handler of the row, there is a very real possibility of causing a stack overflow and a crash. The way that CS has written the code is correct where the update of the button is done in a different method and not click. Attaching modified code that shows the source is always available and the right way to update the checkbox.Closing ticket as invalid.