I'm trying to create db in sqlite and then store data dynamically into some table, again I want to fetch multiple data which should be stored in json file and finally from json array this data will be presented in GUI. I tried following way, but there's some error, I need help.
index.html
<body ng-app="starter" ng-init="statusMessage='started';fname='bikash';lname='dan'">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Ionic Blank Starter</h1>
</ion-header-bar>
<ion-content ng-controller="ExampleController">
<input type="text" ng-model="fname" placeholder="Your first name" /><br />
<input type="text" ng-model="lname" placeholder="Your first name" />
<button class="button" ng-click="insert('{{fname}}','{{lname}}')">Insert</button>
<button class="button" ng-click="select('{{lastname}}')">Select</button>
<p> {{statusMessage}} </p>
<p>{{fname}} {{lname}}</p>
<div ng-repeat="d in all_data">
<p>{{p.fname}} <br/> {{p.lname}}</p>
</div>
</ion-content>
</ion-pane>
</body>
app.js
var db = null;
var example = angular.module('starter', ['ionic', 'ngCordova'])
.run(function ($ionicPlatform, $cordovaSQLite) {
$ionicPlatform.ready(function () {
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
StatusBar.styleDefault();
}
db = $cordovaSQLite.openDB("my.db");
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS people (id integer primary key, firstname text, lastname text)");
});
});
example.controller("ExampleController", function ($scope, $cordovaSQLite) {
//var items = [];
$scope.insert = function (firstname, lastname) {
$scope.statusMessage = "Start Inserting: " + $scope.fname +' '+ $scope.lname;
var query = "INSERT INTO people (firstname, lastname) VALUES (?,?)";
$cordovaSQLite.execute(db, query, [$scope.fname, $scope.lname]).then(function (res) {
$scope.statusMessage += "INSERT ID -> " + res.insertId;
}, function (err) {
$scope.statusMessage = err;
});
}
$scope.select = function (lastname) {
$scope.statusMessage = "Start Fetching";
var query = "SELECT firstname, lastname FROM people WHERE lastname = ?";
$cordovaSQLite.execute(db, query, [$scope.lname]).then(function (res) {
if (res.rows.length > 0) {
var data=[];
for (var i = 0 ; i <= res.rows.length; i++) {
var obj={fname:res.rows.item(i).firstname , lname:res.rows.item(i).lastname};
data.push(obj);
//console.log("Result->"+ res.rows.item(i).firstname + " " + res.rows.item(i).lastname);
//$scope.statusMessage = "SELECTED -> " + res.rows.item(i).firstname + " " + res.rows.item(i).lastname;
}
};
$scope.all_data =data;
} else {
$scope.statusMessage = "No results found";
}
}, function (err) {
console.error(err);
});
}
});
Aucun commentaire:
Enregistrer un commentaire