$ionicPlatform.ready(function() {
. . .
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS employee (id integer primary key, EmployeeID integer, Name text)"); //Create a table
var query = "SELECT EmployeeID,Name FROM employee";
$cordovaSQLite.execute(db, query).then(function (res) {
if (res.rows.length > 0) {
var gridObj = $(".e-grid").ejGrid("instance"), dataSource = [];// create a Grid instance
for (var i = 0; i < res.rows.length; i++) {
dataSource.push(res.rows[i]);
}
gridObj.dataSource(dataSource); //bound the Grid data source
} else {
console.log("No results found");
}
}, function (err) {
console.error(err);
});
});
|
<body ng-app="starter">
<ion-pane>
<!--<ion-header-bar class="bar-stable">
<h1 class="title">Ionic - Teste DataBase</h1>
</ion-header-bar>-->
<ion-content ng-controller="ExampleController">
<div class="angularbind ioncenter">
<div ej-grid id="Grid" e-width="500px"
e-datasource="data" e-editsettings-allowadding="true" e-actionbegin="actionBegin" e-toolbarclick="toolbarclick" e-editsettings-allowdeleting="true" e-editsettings-allowediting="true" e-toolbarsettings-showtoolbar="true" e-toolbarsettings-toolbaritems="tools">
<div e-columns>
<div e-column e-field="EmployeeID" e-headertext="Employee ID" e-isprimarykey="true" e-textalign="right"></div>
<div e-column e-field="Name" e-headertext="Employee Name" e-textalign="right"></div>
</div>
</div>
</div>
<button class="button" ng-click="select()">Show DB</button>
</ion-content>
<ion-list>
</ion-list>
</ion-pane>
</body>
example.controller("ExampleController", function($scope, $cordovaSQLite) {
$scope.tools = ["add", "edit", "delete", "update", "cancel"];
$scope.actionBegin = function (args) {
if (args.requestType == "save") {
var EmployeeID = +args.data.EmployeeID, Name = args.data.Name; //get the added record datas in action beign event
var query = "INSERT INTO employee (EmployeeID,Name) VALUES (?,?)"; //insert the record in local DB
$cordovaSQLite.execute(db, query, [EmployeeID, Name]).then(function (res) {
alert(res);
}, function () {
});
}
},
$scope.data = [];
}); |