如何刷新Titanium Studio中的表视图数据

时间:2013-06-16 19:28:09

标签: ios search titanium appcelerator titanium-mobile

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

我使用了以下代码: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});
        }
        });

1 个答案:

答案 0 :(得分:0)

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

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

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