Javascript 清除最后一个JSON响应
我正在使用Titanium Studio开发iOS 6.1应用程序,版本:3.1.2.201307091805,我正在iPhone模拟器和iPad设备上进行测试。我有一个从远程服务器获取JSON结果的搜索字段。我遇到问题的屏幕顶部有搜索框,下面有几条消息。当用户在搜索字段中键入并单击return时,消息将隐藏,并放置一个表以准备从数据库接收结果。所有这些都很好。当用户键入数据库中不存在的内容时,我有一条消息显示未找到结果,请重试。我做了一个按钮来清除表或未找到的消息。以下是迄今为止我掌握的按钮代码:Javascript 清除最后一个JSON响应,javascript,ios,json,titanium-mobile,Javascript,Ios,Json,Titanium Mobile,我正在使用Titanium Studio开发iOS 6.1应用程序,版本:3.1.2.201307091805,我正在iPhone模拟器和iPad设备上进行测试。我有一个从远程服务器获取JSON结果的搜索字段。我遇到问题的屏幕顶部有搜索框,下面有几条消息。当用户在搜索字段中键入并单击return时,消息将隐藏,并放置一个表以准备从数据库接收结果。所有这些都很好。当用户键入数据库中不存在的内容时,我有一条消息显示未找到结果,请重试。我做了一个按钮来清除表或未找到的消息。以下是迄今为止我掌握的按钮代
var clear = Ti.UI.createButton({
title: "Clear",
style:Titanium.UI.iPhone.SystemButtonStyle.BORDERED
});
clear.addEventListener("click", function() {
message.hide();
table.setData([]);
});
Ti.UI.currentWindow.setRightNavButton(clear);
此代码确实清除了表中的消息或结果,但当我进行另一次搜索时,即使搜索完全无关,以前的结果也会显示在新结果的上方。这是我的密码
var win = Titanium.UI.currentWindow;
win.backgroundImage='images/background.png';
win.barColor='#28517A';
win.title='Product Search';
var message = Ti.UI.createLabel({
text: 'No results found, please try again',
top:'100dp',
left:'20dp',
right:'20dp'
});
var customSearchBar = Ti.UI.createView({
backgroundColor: '#28517A',
height: 42,
top: '0dp',
width: Ti.UI.FILL
});
var customSearchField = Ti.UI.createTextField({
autocorrect: false,
borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
clearOnEdit: true,
height: 28,
hintText: 'Search For Product or Service',
textAlign: 'center',
width: '90%',
});
customSearchBar.add(customSearchField);
win.add(customSearchBar);
var nolist= Ti.UI.createLabel({
text: 'XXXXXX',
color: '#000',
font: {fontSize:'16dp', fontWeight:'bold'},
top:'50dp',
left:'20dp',
right:'20dp'
});
win.add(nolist);
var businessowner = Ti.UI.createLabel({
text: 'XXXXXX',
color: '#000',
font: {fontSize:'16dp', fontWeight:'bold'},
bottom:'10dp',
left:'20dp',
right:'20dp'
});
win.add(businessowner);
var view = Ti.UI.createView({
backgroundColor: 'transparent',
top: '100dp',
bottom:'60dp'
});
var table = Ti.UI.createTableView({
backgroundColor: 'transparent',
top: '0dp',
height:'auto',
bottom:'0dp'
});
view.add(table);
table.show();
var tableData = [];
function checkInternetConnection(){
return Ti.Network.online ? true : false;
}
customSearchField.addEventListener("return", function(e) {
if(checkInternetConnection()){
nolist.hide();
businessowner.hide();
getdata();
win.add(view);
function getdata(){
var url = "http://mydomain.com/filename.php?title="+e.value;
var xhr = Ti.Network.createHTTPClient({
onload: function() {
Ti.API.debug(this.responseText);
var json = JSON.parse(this.responseText);
if (json.cms_list.length< 1){
win.add(message);
}
for (i = 0; i < json.cms_list.length; i++) {
client = json.cms_list[i];
row = Ti.UI.createTableViewRow({
height:'44dp',
hasChild:true
});
var clientlist = Ti.UI.createLabel({
text:client.clientname,
font:{fontSize:'16dp', fontWeight:'bold'},
height:'auto',
left:'10dp',
color:'#000'
});
row.add(clientlist);
tableData.push(row);
}
table.addEventListener('click',function(e){
var row = e.row;
var clientlist = row.children[0];
var win = Ti.UI.createWindow({
url: 'clientdetail.js',
title: clientlist.text
});
var clientlist = clientlist.text;
win.clientlist = clientlist;
customSearchField.blur();
Titanium.UI.currentTab.open(win,{animated:true});});
table.setData(tableData);
},
onerror: function(e) {
Ti.API.debug("STATUS: " + this.status);
Ti.API.debug("TEXT: " + this.responseText);
Ti.API.debug("ERROR: " + e.error);
alert('There was an error retrieving the remote data. Try again.');
},
timeout:5000
});
xhr.open("GET", url);
xhr.send();
}
}
else{
alert('Your internet connection is not available');
}
});
var clear = Ti.UI.createButton({
title: "Clear",
style:Titanium.UI.iPhone.SystemButtonStyle.BORDERED
});
clear.addEventListener("click", function() {
message.hide();
table.setData([]);
});
Ti.UI.currentWindow.setRightNavButton(clear);
如果我按下后退按钮,然后返回此屏幕,搜索就可以了。如何在不离开屏幕然后返回的情况下完全清除以前的结果?每次单击“返回”时,您都在将表添加到窗口中。通过删除win.addview更改事件侦听器;并将该行替换为table.show;像这样:
customSearchField.addEventListener("return", function(e) {
if(checkInternetConnection()){
nolist.hide();
businessowner.hide();
getdata();
//win.add(view); dont do this!!!!
table.show();
.....
});
clear.addEventListener("click", function() {
message.hide();
tableData = [];
table.setData([]);
});
然后,更改此选项:
var table = Ti.UI.createTableView({
backgroundColor: 'transparent',
top: '0dp',
height:'auto',
bottom:'0dp'
});
view.add(table);
//table.show();
win.add(table);
table.hide();
现在,一个表只有一个实例,每次要更改所有行时,都可以在返回侦听器中使用setData。您没有从var tabledata中清除数据。在设置table.setData[];”时,应将其清除。将表设置为空后,它将推送相同的数据 您的代码应该如下所示:
customSearchField.addEventListener("return", function(e) {
if(checkInternetConnection()){
nolist.hide();
businessowner.hide();
getdata();
//win.add(view); dont do this!!!!
table.show();
.....
});
clear.addEventListener("click", function() {
message.hide();
tableData = [];
table.setData([]);
});
每次都要添加一个新表吗?Josiah,当我查看我的代码时,我想我是通过使用table.setData[]来添加的;后跟table.setDatatableData;以及view.addtable;。第一个请求删除消息并添加表。当按下清除按钮时,我尝试用table.setData[]清除所有数据;除了“table.setData[];”之外还有什么吗这将删除对上一个查询的所有引用?请确保只调用一次view.addtable。否则你会得到你所看到的奇怪的结果。约西亚,非常感谢你的帮助!我注释掉了屏幕上的消息,并将添加视图移到函数之外,以确保我只添加了一次表。我的结果没有变化,所以我把信息放回去了。我编辑了上面的代码,将大部分代码都包含在文件中,希望您能查看一下,看看是否可以识别我的问题。谢谢Josiah,显示和隐藏表比每次创建新表要好得多。但是,我仍然遇到同样的问题。第一个查询工作正常,但即使使用“清除”按钮清除表,以前每个查询的每个查询结果都会按照查询顺序显示在表中。如果我回到这个搜索窗口之前的窗口,所有查询都会被清除,我可以重新开始。你知道我还能试什么吗?