24 March, 2011

How To Display Data in a Grid using ExtJS

Grids in ExtJs are one of the most widely used components. Grids provide a better way to present your data to the end-users in an easy-to-understand manner. ExtJs Grids are user friendly, and have different customization options and events so that it can be used in your own way.
ExtJs Grids are similar to spreadsheets containing rows and columns. So, in order to display data in grid using ExtJs we need two Ext components.
1) A Grid Panel, for displaying data
2) A Store, which is like database to keep track of the data.

Now we write the javascript code for displaying data in grid

//load the required extjs files and inside script tag add the following
Ext.onReady(function() {
//first we need to add data store which is to be displayed in the grid
//so adding the data store for the grid
var firstStore = new Ext.data.Store({
//now add the relevant data
data: [
"Sudhir Bastakoti",
"John Doe",
"Joane Doe",
], //end of add data
//now add a reader; in this case we are using Array Reader of ExtJs to read the data
reader: new Ext.data.ArrayReader({id: 'id'}, [
{name: 'birth_date', type: 'date', dateFormat: 'Y-m-d'},
}); //end data store

//finally display the Grid Panel
var firstGrid = new Ext.grid.GridPanel({
renderTo: document.body, //grid in body; ID of a div can also be used
frame: true, //just adds a border to Grid Panel, not compulsory though
title: 'User Information', //as the name suggests; title of Grid
height: 200,
width: 250,
store: firstStore, //name of data store that contains data and reader confs
columns: [
{header: "User Name", dataIndex: 'full_name'},
{header: "Birth Date", dataIndex: 'birth_date', renderer: Ext.util.Format.dateRenderer('m/d/Y')},
{header: "Gender", dataIndex: 'gender'},
{header: "User Email", dataIndex: 'email', sortable: true},
{header: "Skype Add", dataIndex: 'skype'},
] //enf of column field

Add above javascript code in your html file and run in browser , you will be
seeing a grid with data added from store.

'renderer' in column field is for data formatting. In our case its used to format date to m-d-Y format. We can use some callback function instead of that. The grid has lots of built-in features which can be checked in ExtJs Manual.

19 March, 2011

Secure a page using php

By default php script runs on port 80 unless changed, but i had to secure a page so that it could run only on secure port i.e. 443. So what i did was used a code to force to run a page on secure port i.e. https which uses port 443, Here's what i did:

$serverPort = $_SERVER['HTTP_PORT'];
//check if its not secure port
if("443" != $serverPort) {
header("Location: https://".$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);

So, the above code ensures that the page runs under https i.e. port 443 and
is securely displayed. Hope it helps someone.