0

我已经使用 tabBar 创建了一个应用程序。我创建了一个单独的搜索窗口,其中包含一个 searchBar 和一个 TableView 来显示最近的搜索项。每当触发返回事件时,就会打开一个名为 searchresult.js 的新窗口,显示搜索到的数据。当我单击后退按钮时,它来自 searchresult.js-->searchpage.js,但问题是最近数据的表在数据库中得到了更新,但它没有显示在表上,我必须去主页并再次打开 searchpage.js 以查看正确的数据...请帮助...提前谢谢

我使用了以下代码:searchpage.js

    //*** Search Field ***
            var search = Titanium.UI.createSearchBar({
                barColor:'#000', 
                showCancel:true,
                height:43,
                hintText:'Name of the part you want to search',
                autocorrect:true,
                top:0,
            });

            content.add(search);


            //*** Table For Recent Search ***

            var db = Titanium.Database.install('car.db','dbversion1');

            var sql = db.execute ('SELECT * FROM search_history ORDER BY id DESC LIMIT 0, 10');


            var data = [];
            while (sql.isValidRow()){

                var searchQuery = sql.fieldByName('search_query');
                var selectedCategory = sql.fieldByName('selected_category');
                var searchID = sql.fieldByName ('id');

                data.push({title: searchQuery});
                sql.next();
            }   
            var searchTable = Titanium.UI.createTableView({
                headerTitle:'RECENT SEARCH',
                data: data,

            });
            Ti.API.info(searchTable.title);
            content.add(searchTable);


            //Search Action

                search.addEventListener('blur', function(e) {
                    Titanium.API.info('search bar:blur received');
                });

                search.addEventListener('cancel', function(e) {
                    Titanium.API.info('search bar:cancel received');
                    search.blur();
                });

            search.addEventListener('return', function(e){
            var insertSql = db.execute('INSERT INTO search_history (search_query, selected_category) VALUES ("' + search.value + '", 1)');   
                var win = Titanium.UI.createWindow({
                    backgroundColor:'#ffffff',
                    url:'searchresult.js',
                    title: 'Search Result',
                    searchValue: search.value
                });
                Ti.API.info(search.value);

                search.blur();
                Titanium.UI.currentTab.open(win, {animation:true});
            });

    // Back Button Action
    bckButton.addEventListener('click', function(e){
            if (Ti.Android){
                        win.close();
                    }else{
                        win.close({animated:true});
        }
        });

和 searchresult.js

    // *** Content ***
            var content = Titanium.UI.createView({
                backgroundColor:'#fff',
                height:'100%',
                width:'100%',
                layout:'vertical'
            });
            wrapper.add(content);

            var searchQuery = win.searchValue;

            var db = Titanium.Database.install('car.db', 'dbversion1');

            var sql = db.execute ("SELECT * FROM part_category WHERE part_name LIKE \'%"+ searchQuery +"%\'");

            var data = [];

            while(sql.isValidRow()){
                var partName = sql.fieldByName ('part_name');
                var partID = sql.fieldByName ('id');

                data.push({title: partName, hasChild:true, id:partID, url:'partsubcategory.js'});
                sql.next()
            };

            var resultTable = Titanium.UI.createTableView({
                data : data,
            });

            content.add(resultTable);


    // Back Button Action
        bckButton.addEventListener('click', function(e){

            if (Ti.Android){
                        win.close();
                    }else{
                        win.close({animated:true});
        }
        });
4

1 回答 1

0

您应该将搜索结果保存在数据结构中,并在搜索完成后触发事件。您要更新的任何其他表都应该监听该事件并在收到事件时更新。

我相信在 tiBountyHunter 的培训文档中有一个这样的例子

http://docs.appcelerator.com/titanium/latest/#!/guide/Event_Handling

于 2013-06-16T23:51:21.150 回答