Sunday, 16 November 2014

Web SQL DB create, read, update and delete operations example

Here is the simplified example for web sql db create, read, update and delete operations:

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var db;
function createDatabase(dbName){
db = openDatabase(dbName, '1.0', 'Test DB', 2 * 1024 * 1024);
}

function createTable(tableName, arrFields){
db.transaction(function (tx) {
var fields = "";
for(f=0;f<arrFields.length;f++){
fields += arrFields[f] + ',';
}

fields = fields.substring(0, fields.length-1);

tx.executeSql('CREATE TABLE IF NOT EXISTS '+ tableName + ' (' + fields + ')');
});
}

function insertRecord(query){
db.transaction(function (tx) {
tx.executeSql(query);
});
}

function selectRecord(query, callBack){
var empArr = new Array();
db.transaction(function (tx) {
tx.executeSql(query, [], function (tx, results) {
var len = results.rows.length;

// Create the object of emp
for(i=0;i<len;i++){
var emp = {
id:results.rows.item(i).id,
name:results.rows.item(i).name,
salary:results.rows.item(i).salary
};

// Add objects to array
empArr[i] = emp;
}
callBack(empArr); // This is the callback
});
});
}

createDatabase('db_emp');
createTable('emp', ["id", "name", "salary"]);
insertRecord('INSERT INTO emp (id, name, salary) VALUES (1, "Rahul", 5000)');

// We are using callback in this method
selectRecord('select * from emp', function (results){
console.log(results);
});
</script>
</head>
<body>
</body>
</html>

Friday, 14 November 2014

Order by filter in js controller

Below code will sort the list listLeaves in the descending order of date field .

$scope.listLeaves = $filter('orderBy')($scope.listLeaves, "-date")

Friday, 10 October 2014

Simple Coding Rules

1. Code should be proper indented.
2. Meaning full name of variables and constants.
3. Meaning full name of methods and classes.
4. No duplicate code.
5. No inline css.
6. No inline java script.
7. Html, css and java script code also be indented.
8. No html code should come with java services.
9. If you find a code two times in a class than make a separate method using that code.
10. If you find same code in two or more classes than make a separate class using that code and use       that class.

Friday, 1 August 2014

AngularJs: order by filter

Use order by like this:

<tr ng-repeat="emp in empList | orderBy:joiningDate">
</tr>

Above code will show you emp list in ascending order of joining date.

<tr ng-repeat="emp in empList | orderBy:-joiningDate">
</tr>

You can see I used - (hyphen) in above code. Due to this hyphen employee list will show in descending order of joining date.