Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mura CMS : Example of how to populate a Mura CMS Form Builder select menu, radio group, or checkbox options with data from a SQL recordset.
<cfscript>
component output=false {
// On the form, select 'Custom Object' then for the 'Source' enter the dotted notation path to wherever you placed this file.
// For example 'includes.myCustomValues' if your file was called 'myCustomValues.cfc' and placed under the 'includes' directory.
// Mura will automatically invoke the getData() method below
// See http://www.getmura.com/blog/populating-form-builder-dropdowns-via-remote-objects/ for more information
private any function getRS() {
// example recordset : this could come from your own db, of course
var rs = QueryNew('SomeIDColumn, SomeLabelColumn, SomeValueColumn', 'VarChar, VarChar, VarChar');
QueryAddRow(rs, 3);
QuerySetCell(rs, 'SomeIDColumn', '111', 1);
QuerySetCell(rs, 'SomeLabelColumn', 'No', 1);
QuerySetCell(rs, 'SomeValueColumn', 0, 1);
QuerySetCell(rs, 'SomeIDColumn', '222', 2);
QuerySetCell(rs, 'SomeLabelColumn', 'Yes', 2);
QuerySetCell(rs, 'SomeValueColumn', 1, 2);
QuerySetCell(rs, 'SomeIDColumn', '333', 3);
QuerySetCell(rs, 'SomeLabelColumn', 'Maybe', 3);
QuerySetCell(rs, 'SomeValueColumn', 2, 3);
// END example recordset
return rs;
}
public any function getData() {
var hash = '';
var row = '';
var dataRecordOrder = []; // array of keys in the desired order
var dataRecords = {}; // struct of structs
var dataSet = {}; // the return value
// your rs must have an IDCol, ValueCol, LabelCol
var rs = getRS(); // this is YOUR query! from YOUR DB!
// Static Example : how to add a static option
hash = Hash('SomeStaticOption');
ArrayAppend(dataRecordOrder, hash);
dataRecords[hash]['dataRecordID'] = hash;
dataRecords[hash]['label'] = 'Some Label Goes Here';
dataRecords[hash]['value'] = 'Some Value Goes Here';
// Query Example : how to add records from a database as options
for ( row in rs ) {
hash = Hash(row.SomeIDColumn);
ArrayAppend(dataRecordOrder, hash);
dataRecords[hash]['dataRecordID'] = hash;
dataRecords[hash]['label'] = row.SomeLabelColumn;
dataRecords[hash]['value'] = row.SomeValueColumn;
}
// build the final dataSet
dataSet.dataRecordOrder = dataRecordOrder;
dataSet.dataRecords = dataRecords;
WriteDump(var=dataset, abort=1);
// return the dataSet
return dataSet;
}
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.