Populating JQGRID with options from a select.

Posted on 2014-01-29
Last Modified: 2014-02-04
I have two drop downs named: SendtoOrg and SendtoRole. They are auto-populated based on my user's organization and role as follows:

SendtoOrg = #session.currentTierID#
SendtoRole = '1'

On the same page I have a jqgrid that I want to populate with those values on page load and change onChange.

Here's the .cfm for the grid:

<div class="row-fluid">
	<div class="span8 text-center"
        <div id="GridContainer" class="GridCont">
            <table id="list" class="scroll" cellpadding="0" cellspacing="0"></table>
                <div id="pager" class="scroll" style="text-align:center;"></div> 
                <div id="mysearch"></div>

Open in new window

Here is my jqgrid:

Description: This is the jgrid for the apps/admin/email functionality - select recipients 

$(document).ready(function($) {

	// This binds the resizeGrid method to a change of the window size
        resizeTimer = setTimeout("resizeGrid();", 100);



function buildjqGrid() { 

	var grid = $("#list")
		, gCfg = {	
				page: 'pageIndex', 
				sort: 'sortCol', 
				order: 'sortDir', 
				rows: 'pageSize'
			postData: {
				method: "getTestUsers",
				// Pass other params here as needed for the query method
				DSN: cfrequest.dsn,
				InstanceId: cfrequest.instanceId,
				bMySearches: isMySearch,
				bShowActive: true,
				bShowInactive: false,
				xJLLISUserId: cfrequest.xJLLISUserId
			datatype: function (postdata) {
				var $that = $(this); 
					url: "/cfc/Services/EmailRemote.cfc",
					data: postdata,
					type: 'POST',
					dataType: 'json',
					dataFilter: transformData,
					success: function(d, r, o){ 
						if (d.success) 
							bootbox.alert("There was an error in retrieving this data<br>" + d.message);
					error: function () {
						bootbox.alert("There was an error in making your search.");
			{name: 'checkbox', label: 'Checkbox', width: 75, fixed: false, classes: "actionTD"},
			{name:'id', label: 'User ID', index:'id', width:55},
			{name:'firstname', label: 'First Name', index:'firstname', width:200},
			{name:'lastname', label: 'Last Name', index:'lastname', width:250},
			jsonReader : {
				 repeatitems: true,
				 root: "DATA",
				 page: "PAGEINDEX",
				 total: "PAGECOUNT",
				 records: "RECORDCOUNT",
				 id: "id"
			pager: $("#gridPager"),
			pgtext : "Page {0} of {1}",
		sortname: 'id',
		sortorder: "asc",
		viewrecords: true,
			/* <!--- Use if you need row checkboxes to work --->
			,multiselect: true
			,onSelectRow: rowSelectionHandler
			,onSelectAll: selectAllHandler
			,gridComplete: recheckSelections
	//end jqGrid

function transformData (data) {
	return data.replace(/[^\x20-\x7E\xA1-\xFF]+/g,"");

Open in new window

and here's my .cfc:

<cfcomponent displayname="UserMgr" output="false"  extends="cfc/services/DataAccess"> 
<cffunction name="init" output="no" returntype="struct">

	<cfreturn this>


<cffunction name="getTestUsers" access="remote" output="false" returnformat="json">
    <cfargument name="page" required="no" default="1">
    <cfargument name="rows" required="no" default="10">
    <cfargument name="sidx" required="no" default="">
    <cfargument name="sord" required="no" default="ASC">
	<cfargument name="SelectList" default="ID,Firstname,Lastname">

    <cfset var q = "">
	<cfset var returnData = {"success": true, "message": "", "statuscode": 200}>

    <cfquery datasource="#session.currentdsn#" name="LOCAL.q">
		SELECT <!--- TOP #local.nPageSize# -- Need this if the results are paginated --->
			1 AS PageCount,
			1 AS PageIndex,
			10 AS TotalRecordCount,
		id, firstname, lastname
	    FROM f_users
	    <cfif len(arguments.sidx)>
	    ORDER BY #arguments.sidx# #arguments.sord#
	<cfset = LOCAL.q>
	<cfset LOCAL.returnData.pageIndex = ( ) ? : 0>
	<cfset LOCAL.returnData.pageCount = ( ) ? : 0>
	<cfset LOCAL.returnData.recordCount = ( ) ? : 0>
	<cfset = SUPER.getQueryOfQueryBySelectList(, ARGUMENTS.selectList)>

    <cfreturn LOCAL.returnData />

  <cffunction name="queryConvertForJQGRID" access="package" returnformat="json" output="no"> 
    <cfargument name="q" type="query" required="yes"> 
    <cfargument name="page" type="numeric" required="no" default="1"> 
    <cfargument name="rows" type="numeric" required="no" default="10"> 
    <cfset var result = structnew()> 
    <cfset var rowStruct = structnew()> 
    <cfset var col = ""> 
    <cfset => 
    <cfset = ceiling(arguments.q.recordcount/arguments.rows)> 
    <cfset result.records = arguments.q.recordcount> 
    <cfset result.rows = arraynew(1)> 
    <cfoutput query="arguments.q" startrow="#(*arguments.rows+1#" maxrows="#arguments.rows#"> 
      <cfset rowStruct = structnew()> 
      <cfset rowStruct['id'] = id>
	  <cfset rowStruct['cell'] = arraynew(1)>
      <cfset rowStruct['cell'][1] = arguments.q['id'][currentrow]> 
      <cfset rowStruct['cell'][2] = arguments.q['firstname'][currentrow]> 
      <cfset rowStruct['cell'][3] = arguments.q['lastname'][currentrow]> 
      <cfset arrayappend(result.rows, rowStruct)> 
	<cfset output = SerializeJSON(result)>
	<cfset output = replace(output,'.0','','all')>
	<cfset output = replace(output,'ROWS','rows','all')>
	<cfset output = replace(output,'PAGE','page','all')>
	<cfset output = replace(output,'TOTAL','total','all')>
	<cfset output = replace(output,'RECORDS','records','all')>
	<cfset output = replace(output,'CELL','cell','all')>
    <cfreturn output /> 


Open in new window

How would I:

a) Load the jqgrid with the initial values
b) Change the contents of the jqgrid when the select values change

I also need to add a checkbox functionality in one of the columns and I almost have that function written to run by you all here.

Question by:traport
  • 2

Author Comment

Comment Utility


Accepted Solution

Rodrigo Munera earned 500 total points
Comment Utility
As far as loading the data into the grid, the documentation says that you should pass the JSON data to the grid via the addJSONData method (, as long as you're sending the data with in the same format as it's laid out in the grid, you should be fine.

As far as editing the data in the cells it seems to depend on how you want to do it, since there's the option of editing in-line, in-cell, remotely or locally. So that really depends on the workflow of your application. Doing some reading on the documentation of jqgrid (, I was able to find that there's an event that will handle the saves by setting your cellsubmit value to 'remote' and creating a function in your cfc to save the data based on what information is passed from the grid.

Author Closing Comment

Comment Utility
Thanks the documentation was very helpful.

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now