How to initialize form elements with value in AngularJS

To initialize form elements with value in AngularJS, you can use ng-init:

<!DOCTYPE html>
<html>
<script src=”libs/angular.min.js”></script>
<body>

<div ng-app=”myApp” ng-controller=”myCtrl” ng-init=”fname=’tom';lname=’phil'”>
<p>First Name : <input type=”text” ng-model=”fname”></p>
<p>Last Name : <input type=”text” ng-model=”lname”></p>
<h1>Hello {{fname}} {{lname}}</h1>
</div>

</body>
</html>

How to get product currency short name in WooCommerce

In order to get product currency short name like USD, GBP, etc., you can you the following function: get_option(‘woocommerce_currency’)

In order to print currency symbol, you can you following function:

get_woocommerce_currency_symbol()

Or if you want to print a specific currency symbol, for example, US Dollar, you can use following function:

get_woocommerce_currency_symbol(“USD”)

Angularjs ng-app and ng-controller demo

The following script will demo the use of ng-app and ng-controller in AngularJS script.

<!DOCTYPE html>
<html>
<script src=”libs/angular.min.js”></script>
<body>

<div ng-app=”myApp” ng-controller=”myCtrl”>
<p>First Name : <input type=”text” ng-model=”fname”></p>
<p>Last Name : <input type=”text” ng-model=”lname”></p>
<h1>Hello {{fname}} {{lname}}</h1>
</div>
<script>
var app = angular.module(‘myApp’, []);
app.controller(‘myCtrl’, function($scope) {
$scope.fname = “John”;
$scope.lname = “Adams”;
});
</script>
</body>
</html>

Sample script to print hello world in Node.js

The following script will print hello world in console:

//Open a text editor and add following code and save it. (here I used test.js as filename)

var http = require("http");

http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}).listen(8888);



To run the above code:
node test.js

That's it.

Adding JavaScript confirmation on record deletion from Magento Admin Grid

In order to add JavaScript confirmation on record deletion from Magento Admin Grid, You can add a ‘confirm’ => ‘Are you sure?’ key and value in the $this->addColumn():

$this->addColumn(‘action’, array(

….

array( ‘caption’ => Mage::helper(‘namespance_modulename’)->__(‘Delete’),
‘url’ => array(‘base’=>’*/*/delete’),
‘field’ => ‘id’, ‘confirm’ => ‘Are you sure that you want to delete this record?’ )

….

));

That’s it.

Node.js code to list records from a MySQL database

The following code snippet will list records from a MySQL database using Node.js:

Don’t forget to install MySQL libraries (npm install mysql) and change database name and table name I used in the code,  if you are using a different database name and table name.

Database: test

Table Name: Employee

Table Columns: First_Name and Last_Name

 

var http = require(“http”);
var mysql = require(“mysql”);
var sql_db = ‘test';

var con = mysql.createConnection({
host     : ‘localhost’,
user     : ‘root’,
password : ”,
database : ‘test’
});

http.createServer(function(request, response){
response.writeHead(200, {“Content-Type”:”text/plain”});
response.write(“Mysql DB Example”);

con.query(‘USE ‘+sql_db, function(err) {
if(!err){

con.query(“SELECT * FROM EMPLOYEE”, function(err, rows) {
if(!err) {

for(i=0;i<rows.length;i++){
console.log(“First Name:”+rows[i][‘First_Name’]+”, Last Name:”+rows[i][‘Last_Name’]);
}
}
});
}else{
console.log(“NOT OK”);
}
});
response.end();
}).listen(“8888″);
console.log(“Server Started”);

Node.js mysql database connectivity example

The following code snippet will list records from a MySQL database using node.js:

Before executing the code you have to install mysql library using following command:

npm install mysql

And don’t forget to change database name and table name, here I used ‘test’ as dbname and ‘employee’ as table name.

var http = require(“http”);
var mysql = require(“mysql”);
var sql_db = ‘test';

var con = mysql.createConnection({
host     : ‘localhost’,
user     : ‘root’,
password : ”,
database : ‘test’
});

http.createServer(function(request, response){
response.writeHead(200, {“Content-Type”:”text/plain”});
response.write(“Mysql DB Example”);

con.query(‘USE ‘+sql_db, function(err) {
if(!err){

con.query(“SELECT * FROM EMPLOYEE”, function(err, rows) {
if(!err) {
console.log(JSON.stringify(rows));
}
});
}else{
console.log(“NOT OK”);
}
});
response.end();
}).listen(“8888″);
console.log(“Server Started”);

 

AngularJS example for associative arrays

<html ng-app>
<head>
<script src="libs/angular.js"></script>
<script>
function myController($scope){
$scope.employee=[{"name":"ajith","job":"manager","salary":"$2000"},    {"name":"manoj","job":"clerk","salary":"$1000"}    ]    ;
}
</script>
</head>
<body ng-controller="myController">
<h2>List Example</h2>
<table>
<tr>
<td>Name</td>
<td>Job</td>
<td>Salary</td>
</tr>

<tr>
<td>{{employee[0].name}}</td>
<td>{{employee[0].job}}</td>
<td>{{employee[0].salary}}</td>
</tr>

<tr>
<td>{{employee[1].name}}</td>
<td>{{employee[1].job}}</td>
<td>{{employee[1].salary}}</td>
</tr>


</table>
<p>{{employee.name}}</p>
</body>
</html>

AngularJS example for handling form with elements

<html ng-app>
<head>
<script src="libs/angular.js"></script>
<script>
function formController($scope){
$scope.total=0;
$scope.num1=0;
$scope.num2=0;
$scope.calculate = function(){
$scope.total = parseInt($scope.num1) + parseInt($scope.num2);
}

}
</script>
</head>
<body ng-controller="formController">
<form>
<table>
<tr>
<td>Enter Number1</td><td>:<input ng-model="num1" ng-change="calculate()"/></td>
</tr>
<tr>
<td>Enter Number2</td><td>:<input ng-model="num2" ng-change="calculate()" /></td>
</tr>
</table>
<button
ng-click="calculate()">Calculate</button>
</form>
<p>{{total}}</p>
</body>
</html>