I'm having a lot of difficulty in checking the state of my checkbox with the android switchbox. When i click on the checkbox it tells me it cannot find the source i don't know why. This is the code that i have:
var merken = [ { title: "Almere", color: "black", height: 75,}, { title: "Amsterdam", color: "black", height: 75,}, { title: "Roberto botticelli", color: "black", height: 75, },]; Ti.include('data.js'); // this sets the background color of the master UIView (when there are no windows/tab groups on it) Titanium.UI.setBackgroundColor('#000'); // create tab group var tabGroup = Titanium.UI.createTabGroup(); // create base UI tab and root window var win1 = Titanium.UI.createWindow({ title:'Tab 1', backgroundColor:'#fff' }); var tab1 = Titanium.UI.createTab({ icon:'KS_nav_views.png', title:'Deals', window:win1 }); var label1 = Titanium.UI.createLabel({ color:'#999', text:'I am Window 1', font:{fontSize:20,fontFamily:'Helvetica Neue'}, textAlign:'center', width:'auto' }); win1.add(label1); // create controls tab and root window var win2 = Titanium.UI.createWindow({ title:'Tab 2', backgroundColor:'#fff', layout:"vertical" }); var tab2 = Titanium.UI.createTab({ icon:'KS_nav_ui.png', title:'Instellingen', window:win2 }); var tableData = []; for(var i=0,ilen=merken.length; i<ilen; i++){ var thisObj = merken[i]; var thisLabelMerken = Ti.UI.createLabel({ color:"black", font:{ fontSize: 16, fontWeight:"bold" }, text:thisObj.title, width:'auto', textAlign:'left', top:10, left:10, height:20, }); var thisCheckbox = Titanium.UI.createSwitch({ style:Titanium.UI.Android.SWITCH_STYLE_CHECKBOX, value:true, left:400 }); var thisRow = Ti.UI.createTableViewRow({ layout:"vertical", selectedColor:"black", height:75, }); thisRow.add(thisLabelMerken); thisRow.add(thisCheckbox); tableData.push(thisRow); } var headerLabel = Ti.UI.createLabel({ backgroundColor:'#035385', color:"white", font:{ fontSize: 30, fontWeight:"bold" }, text:"Favoriete Merken", textAlign:"center", height:45, width:'500' }); var table = Ti.UI.createTableView({ backgroundColor:"white", data:tableData, headerView:headerLabel, separatorColor:"black", top:0, width:'auto' }); table.addEventListener('click', function(e) { //var value = checkb[i].value; for (var i=0,ilen=merken.length; i<ilen; i++) { if (table.tableData[i].thisRow.thisCheckbox.value) {alert("true");} else{alert("false");}; }; }); win2.add(table); // add tabs tabGroup.addTab(tab1); tabGroup.addTab(tab2); // open tab group tabGroup.open();
1 Answer
Accepted Answer
Hi.
First, add checkbox reference to table view row after it is created:
thisRow.thisCheckbox = thisCheckbox;Then in table view click event handler simply do this:
table.addEventListener('click', function(e) { Ti.alert(e.row.thisCheckbox.value); });Basically, in table view click event handler you have reference to clicked row and you just need to get checkbox easily and that's why I have added it's reference to the table view row.
Your Answer
Think you can help? Login to answer this question!