检索行时出现Titanium错误

时间:2014-11-12 11:30:44

标签: javascript titanium titanium-mobile

使用id从行中检索数据并在下一页的文本字段中显示时出错...而另一个错误是在为特定行传递id时所有行都被删除..

以下是编码:

var data = [];

var db = Titanium.Database.open('trip');

db.execute('CREATE TABLE IF NOT EXISTS newtrip (id INTEGER PRIMARY KEY AUTOINCREMENT, triplabel TEXT,tripname TEXT,destination TEXT,fromdate TEXT,todate TEXT)');
//db.execute('INSERT INTO newtrip(triplabel,tripname,destination,fromdate,todate) VALUES(?,?,?,?,?)',"British museum","mytrip","london","12-10-2014","12-12-2014");
//db.execute('DELETE FROM newtrip');
var resultrows = db.execute('SELECT destination,fromdate,todate FROM newtrip');

while (resultrows.isValidRow()) {

    //var res=
    var row = Ti.UI.createTableViewRow({
        height : Ti.UI.SIZE,
        rightImage : '/images/right1.png',

        layout : 'absolute'
    });

    var tripnamelabel = Ti.UI.createLabel({
        //text : 'Buckingham Palace',
        text : resultrows.fieldByName('destination'),
        color : theme_style,
        font : {
            fontSize : '16dp',
            fontWeight : 'bold'
        },

        top : '10dp',
        left : '10dp',
        //right:'30dp'
    });

    var gettablecount = resultrows.rowCount;
    for (var i = 0; i < gettablecount; i++) {
        var data_edit = [];
        var imgedit = Ti.UI.createButton({

            backgroundImage : '/images/list_edit.png',
            // left:'200dp',
            top : '20dp',
            width : wb,
            height : hb,
            // bottom:10,
            right : '20dp',
            onClick : "edit",
            rowid : resultrows.fieldByName('id')
        });
            if (resultrows.isValidRow()) {
                imgedit.addEventListener('click', function(e) {
                    var db = Titanium.Database.open('trip');

                    if (e.source.onClick == "edit") {
                        var x = db.execute('SELECT * FROM newtrip WHERE id=' + rowid);
                        //alert(x);
                        var createnewWindowback = require('ui/apppage5');
                        //the name of the url you wish to move
                        new createnewWindowback(e.source.rowid).open();
                        win.close();
                    }
                    resultrows.close();
                    db.close();

                });

        }

    }

    for (var i = 0; i < gettablecount; i++) {

        var imgdelete = Ti.UI.createButton({

            backgroundImage : '/images/delete_ic.png',
            // left:'240dp',
            top : '20dp',
            width : wb,
            height : hb,
            //bottom:'20dp',
            right : '60dp',
            onClick : "delete",
            rowid : resultrows.fieldByName('id')
        });

        imgdelete.addEventListener('click', function(e) {
            var db = Titanium.Database.open('trip');

            if (e.source.onClick == "delete") {
                var x = db.execute('DELETE FROM newtrip WHERE id=' + rowid);
                alert("you have just clicked the delete button");
            }
            resultrows.next();
            db.close();

        });
    }
    var fromdate = Ti.UI.createLabel({
        //text : '10.11.2014',
        text : resultrows.fieldByName('fromdate'),
        color : 'Black',
        font : {
            fontSize : '13dp',
            fontWeight : 'bold'
        },
        top : '40dp',
        left : '10dp',
        right : '10dp',
        bottom : '20dp'
    });

    var dash = Ti.UI.createLabel({
        text : '-',
        color : 'Black',
        font : {
            fontSize : '15dp',
            fontWeight : 'bold'
        },
        top : '40dp',
        left : '80dp',
        right : '10dp',
        bottom : '20dp'
    });

    var todate = Ti.UI.createLabel({

        text : resultrows.fieldByName('todate'),
        color : 'Black',
        font : {
            fontSize : '13dp',
            fontWeight : 'bold'
        },
        top : '40dp',
        left : '90dp',
        right : '10dp',
        bottom : '20dp'
    });
    row.add(tripnamelabel);
    row.add(imgedit);
    row.add(imgdelete);
    row.add(fromdate);
    row.add(dash);
    row.add(todate);
    row.className = 'control';
    data.push(row);
    resultrows.next();
}

resultrows.close();
db.close();
triplistview.setData(data);

1 个答案:

答案 0 :(得分:2)

我认为您无法根据 id 字段获取数据,因为您将resultrows变量中的数据提取为:

var resultrows = db.execute('SELECT destination,fromdate,todate FROM newtrip');

因此,您无法获取 id 列。但是你在以下地方使用它:

rowid : resultrows.fieldByName('id') // in : var imgedit

因此rowid的{​​{1}}将为null / undefined。您应该将var imgedit查询修改为:

SELECT

修改:在var resultrows = db.execute('SELECT id,destination,fromdate,todate FROM newtrip'); 的点击监听器下,您有:

imgedit

但是没有定义变量var x = db.execute('SELECT * FROM newtrip WHERE id=' + rowid); ,因此抛出了错误。进行以下更改(rowid已更改为rowid):

e.source.rowid

希望它有所帮助。