在 nativescript 应用程序中显示 sqlite 数据库数据时出现问题
Having issues displaying sqlite db data in nativescript application
我的应用程序有一个记分牌页面,它应该从 sqlite 数据库中获取数据并将其显示在 ListView 的 NavigatingTo 页面上,但它没有按预期执行。
显示结果的xml页面:
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded" navigatingTo="onNavigatingTo">
<ActionBar title="Scoreboard">
<NavigationButton text="Back" android.systemIcon="ic_menu_back" tap="homeTap"/>
</ActionBar>
<StackLayout orientation="vertical">
<Label text="Your Performance Sheet"></Label>
<ListView items= "{{results}}" >
<ListView.itemTemplate>
<Label text="{{testname}}"/>
<Label text="{{score}}"/>
<Label text="{{percent}}"/>
</ListView.itemTemplate>
</ListView>
</StackLayout>
</Page>
计分板视图模型:
var Observable = require("data/observable").Observable;
var ObservableArray = require("data/observable-array").ObservableArray;
var Sqlite = require("nativescript-sqlite");
function scoreViewModel (database) {
var viewModel = new Observable();
viewModel.results = new ObservableArray([]);
viewModel.select = function () {
this.results = new ObservableArray([]);
database.all("SELECT * FROM scores").then(rows => {
for (var row in rows) {
this.results.push(rows[row]);
}
}, error => {
console.log("SELECT ERROR", error);
})
}
viewModel.select();
return viewModel;
}
exports.scoreViewModel = scoreViewModel;
我正在从分数 table 中选择所有数据并推送到已经绑定到视图的 viewModel.results 数组。
scoreboard.js :
var observable = require("data/observable");
var scoreViewModel = require("./scoreboard-view-model").scoreViewModel;
var page;
var Sqlite = require("nativescript-sqlite");
exports.onNavigatingTo = function (args) {
page = args.object;
(new Sqlite("scoreboard.db")).then(db => {
db.execSQL("CREATE TABLE IF NOT EXISTS scores (id INTEGER PRIMARY KEY AUTOINCREMENT, testname TEXT, score TEXT, percent TEXT)")
.then(id => {
page.bindingContext = scoreViewModel(db);
}, error => {
console.log(error)
});
}, error => {
console.log(error);
});
}
谁能帮我把数据显示在列表视图中。
我通过编辑 scoreboard-view-model.js 文件使它工作,正好在 viewModel.Select 函数中:
viewModel.select = function () {
//the line below was changed to this...
//testname, score, and percent are column names in the scores table
database.all("SELECT testname, score, percent FROM scores").then(rows => {
for (var row in rows) {
//the line below was changed too...
viewModel.results.push({testname: rows[row][0], score: rows[row][1], percent: rows[row][2]});
}, error => {
console.log("SELECT ERROR", error);
})
}
完成。它现在按预期工作。感谢 TJ Vantoll 提供的帮助。非常感谢。
我的应用程序有一个记分牌页面,它应该从 sqlite 数据库中获取数据并将其显示在 ListView 的 NavigatingTo 页面上,但它没有按预期执行。
显示结果的xml页面:
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded" navigatingTo="onNavigatingTo">
<ActionBar title="Scoreboard">
<NavigationButton text="Back" android.systemIcon="ic_menu_back" tap="homeTap"/>
</ActionBar>
<StackLayout orientation="vertical">
<Label text="Your Performance Sheet"></Label>
<ListView items= "{{results}}" >
<ListView.itemTemplate>
<Label text="{{testname}}"/>
<Label text="{{score}}"/>
<Label text="{{percent}}"/>
</ListView.itemTemplate>
</ListView>
</StackLayout>
</Page>
计分板视图模型:
var Observable = require("data/observable").Observable;
var ObservableArray = require("data/observable-array").ObservableArray;
var Sqlite = require("nativescript-sqlite");
function scoreViewModel (database) {
var viewModel = new Observable();
viewModel.results = new ObservableArray([]);
viewModel.select = function () {
this.results = new ObservableArray([]);
database.all("SELECT * FROM scores").then(rows => {
for (var row in rows) {
this.results.push(rows[row]);
}
}, error => {
console.log("SELECT ERROR", error);
})
}
viewModel.select();
return viewModel;
}
exports.scoreViewModel = scoreViewModel;
我正在从分数 table 中选择所有数据并推送到已经绑定到视图的 viewModel.results 数组。
scoreboard.js :
var observable = require("data/observable");
var scoreViewModel = require("./scoreboard-view-model").scoreViewModel;
var page;
var Sqlite = require("nativescript-sqlite");
exports.onNavigatingTo = function (args) {
page = args.object;
(new Sqlite("scoreboard.db")).then(db => {
db.execSQL("CREATE TABLE IF NOT EXISTS scores (id INTEGER PRIMARY KEY AUTOINCREMENT, testname TEXT, score TEXT, percent TEXT)")
.then(id => {
page.bindingContext = scoreViewModel(db);
}, error => {
console.log(error)
});
}, error => {
console.log(error);
});
}
谁能帮我把数据显示在列表视图中。
我通过编辑 scoreboard-view-model.js 文件使它工作,正好在 viewModel.Select 函数中:
viewModel.select = function () {
//the line below was changed to this...
//testname, score, and percent are column names in the scores table
database.all("SELECT testname, score, percent FROM scores").then(rows => {
for (var row in rows) {
//the line below was changed too...
viewModel.results.push({testname: rows[row][0], score: rows[row][1], percent: rows[row][2]});
}, error => {
console.log("SELECT ERROR", error);
})
}
完成。它现在按预期工作。感谢 TJ Vantoll 提供的帮助。非常感谢。