Sunday, January 22, 2012

Starting with CFGRID( part - 2 )(Auto Refreshing CFGRID)

How to Add Auto Refreshing In cfgrid

NOTE: All the JS Code is valid for ColdFusion 9(may not work in other version)

The concept auto refreshing is to add some javascript code which will call the grid refresh event after some specified interval of time, So our grid will be refreshed after the interval of time.

First, follow the fist post( ) of cfgrid to create a dynamic grid. Then include the following javascript code in the header section of the code.

NOTE: Always include the JS code for cfgrid in header section. Sometimes, if we will include the JS in body part some features stopped working. So better to include the JS at header.

//Function to refresh the grid
function refreshGrid()
ColdFusion.Grid.refresh('courseGrid', false);

//Set time interval to refresh the grid. We have set time interval 10000ms. The grid will refresh in each 10 sec
setInterval(function() {
}, 10000);

How to Add Custom Form Elements in cfgrid toolbar

In this section we will create two form elements. A button to stop the autorefreshing and a combo box for selecting department ID for which you want to show the courses.

  1. Add Custom buttom in cfgrid to stop auto refreshing

The code for cfgrid.cfm
<script type="text/javascript" src="cfgrid.js"></script>
<!--- Default page size of the grid--->
<cfparam name="url.pageSize" default="10">
<cfform name="logForm">
<cfgrid format="html" name="courseGrid" pagesize="#url.pageSize#" selectmode="row" height="200" bind="cfc:TestCode.cfgrid.getCourses({cfgridpagesize},{cfgridpage},{cfgridsortcolumn},{cfgridsortdirection})">
<cfgridcolumn name="Course_ID" width="100" header="Course ID" headerbold="true" />
<cfgridcolumn name="Dept_ID" width="180" header="Department ID" headerbold="true" />
<cfgridcolumn name="CorName" width="180" header="Course Name" headerbold="true" />
<cfgridcolumn name="CorLevel" width="180" header="Course Level" headerbold="true" />
<!--- Calls the function init when the grid loads --->
<cfset ajaxOnLoad("init") />

The code for cfgrid.js

//Intially the value is true. So when first time the grid loads during page load the auto
//refresh functionality is enables but when we will click the button "OFF Auto Refresh" then
//this functionality will be disabled. Again clciking this button it will enable the autorefresh
var isAutoRefresh = true;

//This section executed when the grid loads
var init = function(){
//get the grid Object
var grid = ColdFusion.Grid.getGridObject('courseGrid');
//Get Top toolbar Object
var gridFootToolBar = grid.getBottomToolbar();
//Add the button for stop auto refresh
gridFootToolBar.add('-', {
pressed: false,
text: '<b style="color:red;">OFF Auto Refresh</b>',
cls: 'x-btn-text-icon',//The ext class that will display the button with text/icon properly
handler:toggleRefresh//Our javascript handler function that toggle the auto refresh
//Reconfigure the layout of the toolbar again, this causes the button to show on the toolbar

function toggleRefresh()
//If auto refresh is set to true then make it false and vice versa
isAutoRefresh = false;
isAutoRefresh = true;

//Function to refresh the grid
function refreshGrid()
//If auto refresh is true then only refresh the grid
ColdFusion.Grid.refresh('courseGrid', false);

//Set time interval to refresh the grid. We have set time interval 10000ms. The grid will refresh in each 10sec
setInterval(function() {
}, 10000);

No comments:

Post a Comment