[cfdb-json]

December 15th, 2016

This documentation applies to version 2.6 and later

Summary of Shortcode Options

Option Example
Description
form [cfdb-json form="myform"] Required. Designates the form to display. All rows and columns are displayed by default
role [cfdb-datatable form="myform" role="Author"] Make short code only display output for user’s with minimum-required role. Choices are

  • Administrator
  • Editor
  • Author
  • Contributor
  • Subscriber
  • Anyone

WARNING: this setting is overridden by plugin setting in WP Admin page Contact -> Database Options -> Can See Submission when using shortcodes
(since 2.4.1)

show [cfdb-json form="myform" show="col1,col2"] Limits columns to be shown to be those designated (comma-delimited list)
hide [cfdb-json form="myform" hide="col1,col2"] Does not show listed columns (comma-delimited list)
limit [cfdb-json form="myform" limit="10"] One number: limit=”10″ means show first 10 rows. Is the same as limit=”0,10″
Two Numbers: limit=”100,10″ means show 10 rows starting at row #100)
random [cfdb-table form="myform" random="2"] Display 2 random rows from those that the short code finds. (Since 2.4)
orderby [cfdb-json form="myform" orderby="last-name,first-name"] Sort rows by designated columns. Use “column-name desc” to sort in descending (reverse) order
search [cfdb-json form="myform" search="Simpson"] Select rows to display that have any cell with the search text in it (case insensitive). Intended to mimic behavior of the “Search” field in the DataTable
filter [cfdb-json form="myform" filter="col1=Simpson"] Select which rows to display based on filter expression. More powerful than “search”, it can filter on multiple columns with boolean and regular expressions. If both “search” and “filter” are specified, “search” is ignored.
Options specific to this shortcode
var [cfdb-json form="myform" var="myJsVar"] Sets the Javascript variable name
format [cfdb-json form="myform" format="array"] Can be “map“, “array” or “arraynoheader“. Default is “map”

 

Use [cfdb-json form="your-form"] with optional show, hide and filter just like[cfdb-table].

The [cfdb-json] works much the same as the [cfdb-table] tag but it outputs a <script> tag in the HTML in which it set a Javascript variable equal to a JSON representation of the data.

Use var to indicate the name of the javascript variable you would like. For example:
[cfdb-json var="mystuff"]

would result in an HTML script tag:

<script type="text/javascript" language="JavaScript">
var mystuff = [the json data];
</script>

Use show, hide, filter options work just as they do for all shortcodes.

JSON Formats

The “format” option provides these options:

map” is an array of maps (associative arrays). Each row is a map of column-name -> value. Each “cell” is referenced byjsonVar[row-integer][col-name-string]. For example, using [cfdb-json var="mystuff"] you would be able to reference an example value in Javascript using: mystuff[0]["Submitted"] to refer to the first row, “Submitted” column.

Example:
[
{"Submitted":"December 10, 2010 10:30 am","your-name":"Name1","checkbox-311":"first","text-940":"Field2"},
{"Submitted":"December 10, 2010 9:54 am","your-name":"Alphred","checkbox-311":"third","text-940":"xxxx"},
{"Submitted":"November 16, 2010 12:54 pm","your-name":"joe","checkbox-311":"second","text-940":"goodbye"},
{"Submitted":"November 16, 2010 12:54 pm","your-name":"mike","checkbox-311":"first","text-940":"hello"}
]

array” is a 2D array of the data, with the first row being the column headers.

Example:
[
["Submitted","your-name","checkbox-311","text-940"],
["December 10, 2010 10:30 am","Name1","first","Field2"],
["December 10, 2010 9:54 am","Alphred","third","xxxx"],
["November 16, 2010 12:54 pm","joe","second","goodbye"],
["November 16, 2010 12:54 pm","mike","first","hello"]
]

arraynoheader” is the same as “array” but without the first header row. Is is a 2D array of just the data.

 

Example:
[
["December 10, 2010 10:30 am","Name1","first","Field2"],
["December 10, 2010 9:54 am","Alphred","third","xxxx"],
["November 16, 2010 12:54 pm","joe","second","goodbye"],
["November 16, 2010 12:54 pm","mike","first","hello"]
]

How to make an AJAX call to get JSON

NOTE: the URLs below changed in version 1.8. The 1.8+ URLs are now shown below.

The quick way to see what URL you need to make an AJAX, go to the Database admin page and export to type JSON. Then look at the URL in the browser. It will be of the following form (in this example our form name is “Form Name” so we have to use “Form+Name” in the URL:

http://mywordpress.com/wp-admin/admin-ajax.php?action=cfdb-export&form=Form+Name&enc=JSON&format=map

Issues: you will run into problems if you not logged in when making this AJAX call because WordPress will redirect you to the login form page. To get past that, you need to use the URL for the login page with a redirect_to parameter that gives the URL that you would have wanted above. The problem is that you have to URL-encode the parameters in that URL.

In other words, Example: (You will have to substitute EncodedFormName below for the URLEncoded name of your form)

http://mywordpress.com/wp-admin/admin-ajax.php?action=cfdb-export&form%3DEncodedFormName%26enc=JSON%26format%3DFarray

NOTE: You can also append show, hide, search and filter to the URL

Comments are closed.  Go To Support Forum