2

I'm following the example on this page to setup an Ajax source for JQuery Datatables. http://datatables.net/release-datatables/examples/ajax/objects.html

I can recreate this just fine if I start out with the source file just like their example array, in the following format.

{
  "aaData": [
    {
       "engine": "Trident",
       "browser": "Internet Explorer 4.0",
       "platform": "Win 95+",
       "version": "4",
       "grade": "X"
    },
    {
       "engine": "Trident",
       "browser": "Internet Explorer 5.0",
       "platform": "Win 95+",
       "version": "5",
       "grade": "C"
    }
  ]
 }

However, I'm using ColdFusion and I want my source data to come from a query. So, I followed another example about populating an array from a cfquery. http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec09f0b-7fea.html

That will put my sample data array in the following format.

RecordID: 2, Name: Al
RecordID: 1, Name: Mike
RecordID: 3, Name: Bill 

What can I do to run a cfquery that will populate my source file in the proper format needed for the sAjaxSource attribute?

3 Answers 3

4

I wrote a JSON converter specifically for this:

<cfset rs.dt = new DataTables() />
<cfset rs.dt.setData( yourQuery ) /> <!--- yourQuery = ColdFusion query --->
<cfset rs.dt.setDataFormat( "struct" ) />
<cfreturn rs.dt.$renderData() />

Converts your ColdFusion query into the jQuery DataTables format.

DataTables.cfc requires ArrayCollection.cfc, which is in the same repository.

Sign up to request clarification or add additional context in comments.

2 Comments

The KungFoo sample apps (there are two) are helpful resources to learn from, thanks for sharing.
You even provided an apache-derby db and provide a link to a db viewer! Awesome-ness!
3

Thanks for the replies. I ended up using pieces of a some code I found online to get this to work.

This is my source table (DataSource.cfm)

<cfquery name="Query1" datasource="x">
    Select RecordID, NAME
    From TestTable
</cfquery>

<cfset data = [] />

<cfoutput query="Query1">
    <cfset obj = {
        "RecordID" = RecordID,
        "NAME" = Name
     } />
    <cfset arrayAppend(data, obj) />
</cfoutput>


<cfprocessingdirective suppresswhitespace="Yes">
    <cfoutput>
        {"aaData":
        #serializeJSON(data)#
        }
    </cfoutput>
</cfprocessingdirective>

<cfsetting enablecfoutputonly="No" showdebugoutput="No">

and this is the JQuery

<script>
$(document).ready(function() {
    var oTable = $('#example').dataTable( {
    "bJQueryUI": true,
        "bProcessing": true,
    "bServerSide": false,
    "sAjaxSource": "DataSource.cfm",
    "aoColumns": [
         { "mData": "RecordID" },
         { "mData": "NAME" }
    ]
         });
    });
 </script>

and here's the body of the page

<form>
        <div id ="div1" class="dataTables_wrapper">
            <table id="example">
                <thead>
                    <tr>
                        <th>RecordID</th>
                        <th>NAME</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </form>

1 Comment

nice job detailing this answer, thanks. Works perfectly. Now all I need to do is tweak it to make it an editable table.
2
  1. Create a variable that is a structure (lets call it myVar).
  2. Create a key in that structure named appData and set it to an empty array.
  3. Loop over the query and inside the loop, create a structure (lets call it myStruct)
  4. Still inside the loop, create keys for myStruct named engine, browser, etc. and populate these keys based on the relative data in your query row.
  5. Still inside the loop, append myStruct to the myVar.appData array.
  6. After the loop, myVar should have a structure similar to your example, if it does not, you made an error somewhere.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.