动态生成按钮并使用JSON将变量传递到新窗口
首先,我是一个初学者,这是我的第一个应用程序。我已经在这个应用程序上工作了好几天,我被卡住了。在页面remote_read-org-3.js上,我正在创建一个状态列表按钮。这些都是从mySQL数据库中提取的。这部分工作正常。我需要在单击按钮时将stateabbr传递到下一个窗口。问题是无论我点击哪个按钮,它都会通过列表中的最后一个状态。 这是remote_read-org.js它可能不是最干净的代码,但我仍在处理how-to的代码动态生成按钮并使用JSON将变量传递到新窗口,json,titanium-mobile,Json,Titanium Mobile,首先,我是一个初学者,这是我的第一个应用程序。我已经在这个应用程序上工作了好几天,我被卡住了。在页面remote_read-org-3.js上,我正在创建一个状态列表按钮。这些都是从mySQL数据库中提取的。这部分工作正常。我需要在单击按钮时将stateabbr传递到下一个窗口。问题是无论我点击哪个按钮,它都会通过列表中的最后一个状态。 这是remote_read-org.js它可能不是最干净的代码,但我仍在处理how-to的代码 var currentWin = Ti.UI.currentWi
var currentWin = Ti.UI.currentWindow;
var view02 = Titanium.UI.createView({
top:0,
left:0,
height: '100%',
width: '100%',
backgroundImage: 'images/wcs_background_2.jpg',
})
var label01 = Titanium.UI.createLabel({
text: "US STATES",
top:25,
left:125,
height:'auto',
width:'175',
textAlign: "left",
font:{fontFamily:'Arial',fontWeight:'bold',fontSize:24},
color: "#1c1e3b",
})
var label02 = Titanium.UI.createLabel({
text: "Attachments",
top:50,
left: 125,
height:'24',
width:'150',
textAlign: "left",
font:{fontFamily:'Arial',fontWeight:'bold',fontSize:18},
color: "#1c1e3b",
})
var view01 = Titanium.UI.createView({
top:90,
left:70,
height: 375,
width: Ti.UI.FILL,
})
var currentWin = Ti.UI.currentWindow;
var sendit = Ti.Network.createHTTPClient();
sendit.open('GET', 'http://localhost/test/read.php');
sendit.send();
sendit.onload = function(){
var json = JSON.parse(this.responseText);
var json = json.states;
var dataArray = [];
var scroller = Ti.UI.createScrollView({
height: Ti.UI.FILL,
width: Ti.UI.FILL,
});
var brandView = Ti.UI.createView({ //Primary view for buttons
title: 'Hello',
top:0,
left:0,
height : Ti.UI.FILL,
width : Ti.UI.FILL,
contentHeight : "auto",
backgroundColor : "transparent",
layout : "horizontal",
horizontalBounce :false,
});
scroller.add(brandView);
view01.add(scroller);
var pos;
for( pos=0; pos < json.length; pos++){
dataArray.push({title:'' + json[pos].stateAbbr + ''});
// set the array to the tableView
var btn = Ti.UI.createButton({
title: json[pos].stateAbbr,
width: 60,
height: 70,
top: pos * 0, // space the buttons at 105
left: 2,
backgroundImage: 'images/state_icon.png',
MyID: json[pos].stateAbbr,
});
btn.addEventListener('click', function(e) {
var newWindow = Titanium.UI.createWindow({
url: 'remote_read_acc.js',
MyID: btn.MyID
});
newWindow.open(newWindow);
});
brandView.add(btn);
};
};
var brandView = Ti.UI.createView({
});
view02.add(view01);
view02.add(label01);
view02.add(label02);
currentWin.add(view02);
var currentWin=Ti.UI.currentWindow;
var view02=Titanium.UI.createView({
排名:0,
左:0,,
高度:“100%”,
宽度:“100%”,
背景图片:“images/wcs_background_2.jpg”,
})
var label01=Titanium.UI.createLabel({
案文:“美国国家”,
前25名,
左:125,
高度:'自动',
宽度:'175',
textAlign:“左”,
字体:{fontFamily:'Arial',fontWeight:'bold',fontSize:24},
颜色:“1c1e3b”,
})
var label02=Titanium.UI.createLabel({
正文:“附件”,
前50名,
左:125,
高度:'24',
宽度:'150',
textAlign:“左”,
字体:{fontFamily:'Arial',fontWeight:'bold',fontSize:18},
颜色:“1c1e3b”,
})
var view01=Titanium.UI.createView({
排名:90,
左:70,
身高:375,
宽度:Ti.UI.FILL,
})
var currentWin=Ti.UI.currentWindow;
var sendit=Ti.Network.createHTTPClient();
sendit.open('GET','http://localhost/test/read.php');
sendit.send();
sendit.onload=函数(){
var json=json.parse(this.responseText);
var json=json.states;
var dataArray=[];
var scroller=Ti.UI.createScrollView({
高度:Ti.UI.FILL,
宽度:Ti.UI.FILL,
});
var brandView=Ti.UI.createView({//按钮的主视图
标题:“你好”,
排名:0,
左:0,,
高度:Ti.UI.FILL,
宽度:Ti.UI.FILL,
内容高度:“自动”,
背景色:“透明”,
布局:“水平”,
水平反弹:错误,
});
scroller.add(brandView);
view01.add(滚动条);
var-pos;
for(pos=0;pos
我需要把stateabbr传给这个新窗口
远程读取附件js
var currentWin = Ti.UI.currentWindow;
var view02 = Titanium.UI.createView({
top:0,
left:0,
height: '100%',
width: '100%',
backgroundImage: 'images/wcs_background_2.jpg',
})
var label01 = Titanium.UI.createLabel({
text: "US STATES",
top:25,
left:125,
height:'auto',
width:'175',
textAlign: "left",
font:{fontFamily:'Arial',fontWeight:'bold',fontSize:24},
color: "#1c1e3b",
})
var label02 = Titanium.UI.createLabel({
text: "Attachments",
top:50,
left: 125,
height:'24',
width:'150',
textAlign: "left",
font:{fontFamily:'Arial',fontWeight:'bold',fontSize:18},
color: "#1c1e3b",
})
var view01 = Titanium.UI.createView({
top:90,
left:90,
height: 375,
width: Ti.UI.FILL,
})
var currentWin = Ti.UI.currentWindow;
var sendit = Ti.Network.createHTTPClient();
sendit.open('GET', 'http://localhost/test/attachments.php');
sendit.send();
sendit.onload = function(){
var json = JSON.parse(this.responseText);
var json = json.attachments;
var dataArray = [];
var scroller = Ti.UI.createScrollView({
height: Ti.UI.FILL,
width: Ti.UI.FILL,
});
var brandView = Ti.UI.createView({ //Primary view for buttons
title: 'Hello',
top:0,
left:0,
height : Ti.UI.FILL,
width : Ti.UI.FILL,
contentHeight : "auto",
backgroundColor : "transparent",
layout : "horizontal",
horizontalBounce :false,
});
scroller.add(brandView);
view01.add(scroller);
var pos;
for( pos=0; pos < json.length; pos++){
dataArray.push({title:'' + json[pos].attachmentName + ''});
// set the array to the tableView
var btn = Ti.UI.createButton({
title: json[pos].attachmentName ,
width: 190,
height: 30,
top: pos * 0, // space the buttons at 105
left: 2,
MyID: json[pos].attachmentName,
});
btn.addEventListener('click', function(e) {
var newWindow = Titanium.UI.createWindow({
url: '',
});
newWindow.open(newWindow);
brandView.add(btn);
};
};
var brandView = Ti.UI.createView({
});
view02.add(view01);
view02.add(label01);
view02.add(label02);
currentWin.add(view02);
var currentWin=Ti.UI.currentWindow;
var view02=Titanium.UI.createView({
排名:0,
左:0,,
高度:“100%”,
宽度:“100%”,
背景图片:“images/wcs_background_2.jpg”,
})
var label01=Titanium.UI.createLabel({
案文:“美国国家”,
前25名,
左:125,
高度:'自动',
宽度:'175',
textAlign:“左”,
字体:{fontFamily:'Arial',fontWeight:'bold',fontSize:24},
颜色:“1c1e3b”,
})
var label02=Titanium.UI.createLabel({
正文:“附件”,
前50名,
左:125,
高度:'24',
宽度:'150',
textAlign:“左”,
字体:{fontFamily:'Arial',fontWeight:'bold',fontSize:18},
颜色:“1c1e3b”,
})
var view01=Titanium.UI.createView({
排名:90,
左:90,,
身高:375,
宽度:Ti.UI.FILL,
})
var currentWin=Ti.UI.currentWindow;
var sendit=Ti.Network.createHTTPClient();
sendit.open('GET','http://localhost/test/attachments.php');
sendit.send();
sendit.onload=函数(){
var json=json.parse(this.responseText);
var json=json.attachments;
var dataArray=[];
var scroller=Ti.UI.createScrollView({
高度:Ti.UI.FILL,
宽度:Ti.UI.FILL,
});
var brandView=Ti.UI.createView({//按钮的主视图
标题:“你好”,
排名:0,
左:0,,
高度:Ti.UI.FILL,
宽度:Ti.UI.FILL,
内容高度:“自动”,
背景色:“透明”,
布局:“水平”,
水平反弹:错误,
});
scroller.add(brandView);
view01.add(滚动条);
var-pos;
for(pos=0;pos
然后,我还需要使用我传递的stateabbr来查询dataArray,以便仅从与stateabbr变量匹配的数组中提取值,以便在此页面上显示它们。
任何帮助都将不胜感激它正在传递列表中的最后一个状态,因为您正在for循环中添加事件侦听器,并忽略JavaScript范围规则(当然,它们不是最容易理解的)。因此,
btn
始终等于您创建的最后一个btn
。请尝试使用每个单击事件的source
属性,t
btn.addEventListener('click', function(e) {
var newWindow = Titanium.UI.createWindow({
url: 'remote_read_acc.js',
MyID: e.source.MyID // Get the actual button that was clicked
});
newWindow.open(newWindow);
});