Skip to content

Instantly share code, notes, and snippets.

@stevewithington
Last active November 20, 2020 15:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stevewithington/7947902 to your computer and use it in GitHub Desktop.
Save stevewithington/7947902 to your computer and use it in GitHub Desktop.
Mura: Example of how to populate a Mura Form Builder select menu, radio group, or checkbox options with data from a SQL recordset via Custom Object option
component {
/**
* MURA FORM: `CUSTOM OBJECT` Options Example
*
* Notes:
* + On the form, select `Custom Object` and enter the dotted notation path to where this file resides under `/sites/{SiteID}`
* - For example, if the file is located under `/sites/default/remote/muraFormCustomObjectOptions.cfc`
* - Enter: `remote.muraFormCustomOptions`
* - Keep in mind, Mura will be looking for the `getData()` method! See below.
* + How to use a REMOTE SOURCE for options: <https://gist.github.com/stevewithington/115b5ac681fe35d42505b1cdedad1593>
* + How to create a Mura form: <https://docs.getmura.com/v7-1/content-managers/advanced-content/#section-how-to-createedit-a-form>
*/
function getRS() {
// example recordset : this could come from your own db, of course
var recordsArray = [
{
'datarecordid': '1',
'label': 'California',
'value': 'CA'
},
{
'datarecordid': '2',
'label': 'Ohio',
'value': 'OH'
},
{
'datarecordid': '3',
'label': 'Wisconsin',
'value': 'WI'
},
];
return QueryNew(
'datarecordid,label,value',
'varchar,varchar,varchar',
recordsArray
);
}
/**
* Notes:
* - The method MUST be named `getData`
* - The returned object MUST be wrapped in a `data` attribute
* - The `data` object MUST contain `datarecordorder` and `datarecords` attributes
* - Optionally, include a `defaultid` attribute to select a specific option by default
*/
function getData() {
var dataRecordOrder = [];
var dataRecords = {};
var rs = getRS();
for (var row in rs) {
var id = row.datarecordid;
ArrayAppend(dataRecordOrder, id);
dataRecords[id] = {
'datarecordid': id,
'label': row.label,
'value': row.value
};
}
return {
'defaultid': dataRecords[1]['datarecordid'],
'datarecordorder': dataRecordOrder,
'datarecords': dataRecords
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment