Skip navigation
Currently Being Moderated

jqGrid Server side

Aug 8, 2013 2:01 PM

I use jqGrid for my project using following server side code for ColdFusion in my cfc file.

The jqGrid works only for a few pages.

I can scroll for a couple of page using jqGrid pager.

It seems that the page and records information does not send to jqGrid correctly.

Do I need to particular to pass any row, pages and records information from client to make server response correct pages?


Your help and information is great appreciated,







<cffunction name="ccdClaimsLookup" access="remote" returnformat="json">
    <cfargument name ="StartDate" required="no" default="">
    <cfargument name ="EndDate" required="no" default="">
    <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">
    <cfset var Mydata = ArrayNew(1)>

     <cfset start = ((*arguments.rows)+1>
     <cfset end = (start-1) + arguments.rows>
     <cfset i = 1>

    <cfstoredproc procedure="MySP">
      <cfprocparam value = "#StartDate#" CFSQLTYPE = "cf_sql_date">
      <cfprocparam value = "#EndDate#" CFSQLTYPE = "cf_sql_date">
      <cfprocresult name="spname" resultset="1">
     <cfloop query="spname" startrow="#start#" endrow="#end#">

       <cfset Mydata[i] = [#MyID#,#MyName#,#MyDate#,#MyNote#]>

            <cfset i = i + 1>           

     <cfset totalPages = Ceiling(spname.recordcount/arguments.rows)>
     <cfset stcReturn = {total=#totalPages#,,records=#spname.recordcount #,rows=#Mydata#}>


<cfreturn #serializeJSON(stcReturn, True)#>

  • Currently Being Moderated
    Aug 8, 2013 3:32 PM   in reply to iccsi



    I can't comment on the data you are passing to jqGrid, as I haven't used that jQuery plugin myself.  I did notice one thing that could be a problem.  You are using the SerializeJSON() function on your data, but you are also specifying the ReturnFormat on your function as JSON.  Specifying the ReturnFormat causes the value being returned to automatically be serialized to JSON; thus, you are serializing your data twice (once by calling the SerializeJSON function, then ColdFusion does it again automatically when it returns the data).  This may cause JSON to become malformed. 


    Since you are not returning your query directly from your function (you are looping through it and building an array of arrays), you shouldn't need the SerializeJSON call at all.  You should be able to just return your stcReturn variable.



    Carl V.

    Mark as:
  • Currently Being Moderated
    Aug 10, 2013 7:41 AM   in reply to iccsi

    jqGrid only outputs the data. Data retrieval is done in the CFC. So, check if you CFC does its job properly.

    Mark as:
  • Currently Being Moderated
    Aug 11, 2013 7:11 PM   in reply to iccsi

    "Are there any way to check that the cfc get correct datra to jqGrid?" -- Just call the URL including the parameters. Use Firebugs or Chrome Developer Tools as I mentioned you in your previous discussion about jqGrid.

    Mark as:
  • Currently Being Moderated
    Aug 12, 2013 7:49 AM   in reply to iccsi

    Another thing to do, var scope all variables that are local to the ccdClaimsLookup() function.

    Mark as:
  • Currently Being Moderated
    Aug 12, 2013 7:50 AM   in reply to iccsi

    Don't you need to double quotation marks in your JSON variable names and values?


    Mark as:
  • Currently Being Moderated
    Aug 23, 2013 9:23 AM   in reply to iccsi

    Something to think about, I have found that jsStringFormat misformats strings that contain single quotes and this breaks the json parsing that jquery uses (it's improper json so that is understandable). My trick is that I wrote my own wrapper around jsStringFormat that replaces "\'" back to "'" (backslash-single-quote back to single-quote).


    I don't use remote cfc calls so I'm not sure your issue is the same jsStringFormat issue -- and if it is, I don't know how you would fix it!

    Mark as:
  • Currently Being Moderated
    Aug 23, 2013 9:33 AM   in reply to Steve Sommers

    Oh, I just remembered, jqGrid (maybe jquery) also has issues with embedded nulls within string data. Here is the actual code my jsStringFormat wrapper uses:


    <cfreturn reReplace(replace(jsStringFormat(arguments.str),"\'","'","ALL"),"[^\x 01-\x7F]","?","ALL") />


    (Note: there is no space between the \x and the 01 in the above code -- the forum editor is putting it there???)

    Mark as:
  • Currently Being Moderated
    Sep 5, 2013 9:46 AM   in reply to iccsi

    I write my own raw json output as opposed to using any of the built in cfc json data stuff so I'm not sure how much this will help you. Here is the final code that sends jqGrid data -- the application.utilities.jsonStringFormat(xxx) does the reReplace code I previously mentioned:




    "total": #variables.pages#,

    "records": #qGrid.recordCount#,

    "rows": [

      <cfloop query="qGrid" startrow="#variables.startRow#" endrow="#variables.endRow#">


        "id": #val(documentID)#,

        "cell": [





         "#application.utilities.jsonStringFormat( created))#",



       }<cfif currentRow LT min(variables.endRow,qGrid.recordCount)>,</cfif>




    Mark as:
  • Currently Being Moderated
    Sep 12, 2013 12:59 PM   in reply to iccsi

    Unfortunately, this is a common problem with SerializeJSON().


    I've mostly stopped using it and switched to an alternative JSON serializer.  The one I'm using is called JSONUtil.cfc by Nathan Mische:


    -Carl V.

    Mark as:
  • Currently Being Moderated
    Sep 12, 2013 2:11 PM   in reply to iccsi

    I would consider it a bug. But it's been part of CF for so long that it'll probably break a lot of work arounds if they ever fix it.

    Mark as:
  • Currently Being Moderated
    Sep 12, 2013 2:48 PM   in reply to iccsi

    The main page has a couple of paragraphs of instructions.  Use CreateObject() to instantiate the CFC into a variable (you might even want to instantiate it in your Application.cfm/.cfc and store it in the application scope for easy access anywhere in your application).


    As far as the usage, this paragraph sums it up well:


    This component has two public methods, serializeJSON and deserializeJSON, which have method signatures similar to ColdFuison's built in Serialize/DeserializeJSON functions. The JSONUtil deserializeJSON method takes the same parameters as DeserializeJSON, JSONVar and strictMapping, and should produce the same results as that function. The JSONUtil serializeJSON method takes the var and serializeQueryByColumns parameters, just as ColdFusion's built in SerializeJSON function, however this method also takes an optional third parameter, strictMapping. If the strictMapping parameter is not provided or set to false the serialize method should produce the same results as ColdFusion's built in SerializeJSON function. If the Boolean strictMapping parameter is set to true, then the serialize method attempts to convert the ColdFusion data to a JSON string using underlying Java/SQL data types as opposed to using ColdFusion's implicit type conversion conventions.


    -Carl V.

    Mark as:
  • Currently Being Moderated
    Sep 12, 2013 3:01 PM   in reply to iccsi

    You can instantiate an object for the JSONUtil.cfc anywhere in your code that you want.  I usually instantiate it in my Application.cfc onApplicationStart() method and store it as application.JSONUtil (as a variable in the application scope).  I keep all my utility cfc's in a "cfc" folder off the web root, so to instantiate the CFC in my Application.cfc I have this:

    application.JSONUtil = createObject("component","cfc.JSONUtil");


    Then I can call application.JSONUtil.serializeJSON() or application.JSONUtil.deserializeJSON() wherever I need to in my code.



    -Carl V.

    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points