Enterprise Open source Grafana Cloud

Form elements

The Business Forms plugin provides a wide variety of element types to meet almost any web application requirement.

Note

All form elements (starting from version 4.9.0, including sections) can be created dynamically. For more info, please refer to the Dynamic Forms page of the Business Forms panel documentation.

Layout

You can choose from three layouts: Basic, Buttons Only, and Sections. To switch between them, open the Layout category.

Layout can be Basic, Buttons Only, and Sections.
Form elements layout types.

Basic

In the Basic layout, all form elements are positioned vertically, one following another.

Sections

With the Sections layout, you can create as many sections as needed and place sections either vertically or horizontally using the Orientation option.

When you select the Sections layout, controls for creating sections appear. For each section, you can specify an ID for later reference and a name that displays as the section label.

Note

The Collapsable parameter has been supported starting from version 4.0.0.

Sections can be collapsible. This functionality is available only for sections in the Vertical orientation. You can set any section to Expanded when the form opens or refreshes.

You can also control the Expanded and Collapsed states in Custom code by using the following commands:

js
context.panel.expandSection(id);
context.panel.collapseSection(id);

Note

The full list of the Business Form panel parameters can be found in the Custom Code section of the panel’s documentation.

Buttons Only

The Buttons Only layout displays only standard buttons on your form. The standard buttons include Submit, Reset, and Save default. This layout hides all panel options related to other form elements, which provides a cleaner edit mode.

Add a form element

To add a form element, go to the Form Elements category. Specify the element ID, label, and type. After you click Add Element, you can configure additional element options.

Element types have specific options. For example, the Text area type includes a Rows option that controls the element size on the dashboard.

Form element in Edit mode.

Move a form element

Note

Supported starting from the version 3.0.0.

You can drag-and-drop form elements in the edit mode to change their order on the form.

Drag-and-drop form elements in Edit mode.

Form element types

The following image shows all available element types before version 4.4.0:

Form elements on UI.

Common configuration

There are three form elements with select options.

  • Multi Select with Custom Options
  • Select with Custom Options
  • Radio Group with Custom Options

All three have some options in common.

Icon

Optionally select an icon for this element.

Custom color and background color for elements

Note

Element colors are supported starting from version 4.0.0.

You can customize the following Form Elements colors:

  • Background color
  • Label background
  • Label color
Color options

Select options from a query

Note

This feature is supported starting from version 3.2.1.

You can reference a query from any configured data source to populate form elements dynamically. Specify the Label and Value fields.

Use values from the query for your types with select options.

Select options from GET Options code

Note

This feature is supported starting from version 3.5.0.

Get Options Code lets you hard code options using the code editor. The code must:

  • Return an array with {label,value} objects.
  • Be synchronous.
Use hard-coded values from the Get Options Code for your types with select options.

Set options received asynchronously

Get Options Code doesn’t support asynchronous code. However, you can still work with options received asynchronously.

Complete the following steps:

Step 1: Initial request

Initial request to work with options received asynchronously.

In the following example, the code finds the required element and updates its options. After the data is received, the element is updated using context.panel.onChangeElements().

js
const url = "https://jsonplaceholder.typicode.com/users";
const element = context.panel.elements.find(
  (element) => element.id === "select"
);

async function fetchData() {
  try {
    const response = await fetch(url);
    const body = await response.json();

    const optionsNew = body.map((element) => ({
      label: element.name,
      value: element.username,
    }));

    const newElement = {
      ...element,
      options: optionsNew,
    };
    const newElements = context.panel.elements.map((element) => {
      if (element.id === "select") {
        return newElement;
      }
      return element;
    });
    context.panel.onChangeElements(newElements);
  } catch (error) {
    console.log("Error:", error);
  }
}

return fetchData();

Step 2. Create the GET Options code

Set options for your element from context.panel.elements.

Get Options code for the Options received asynchronously
js
const element = context.panel.elements.find(
  (element) => element.id === "select"
);

if (element?.options) {
  return element?.options;
}

Custom Values

Note

Available starting from version 4.9.0.

If enabled, users can enter custom values in the Select and Multi select form element types.

Conditional visibility

Every form element has a Show If returned value is true parameter where you can enter JavaScript code.

  • If this code returns true, the element is shown on the panel.
  • If this code returns false, the element is hidden from the panel.
Conditional visibility example.

Example code to check the current value of the select element and show the dateTime element if the value equal to max:

js
const select = context.panel.elements.find(
  (element) => element.id === "select"
);

if (select) {
  return select.value === "max";
}

Note

Below is supported starting from the version 3.2.1.

You can use dashboard and global variables in your JavaScript code.

Conditional visibility using a dashboard variable example.

Example code to check the value of the dashboard variable var:

js
const test = context.grafana.replaceVariables("$var");
return test === "test";

Field mapping

Note

This feature is supported starting from version 4.4.0.

To map initial form element values, use the Initial Fields options category.

Data source 4.4.0

Steps to configure the Initial Request for Data Source. New location of `Field Name` fields.

Query 4.4.0

Explicitly specify the field-to-form element mapping for Query using Query Field. New location of Query Field fields since version 4.2.0

Note

This feature is supported in versions 3.2.1 through 4.0.0.

You can specify field-to-form mapping using the Field name parameter for Data Source or the Query Field parameter for Query.

Data Source

Explicitly specify the field-to-form element mapping for Data Source using Field name.

The Field Name option for each Form Element is located under the code editor when the data source option is enabled. Specify a field name for appropriate form element from the data source response.

Query

Explicitly specify the field-to-form element mapping for Query using Query Field.

The Query Field option for each Form Element is located under the code editor when the Query option is enabled. Specify a field name for appropriate form element from the Query response.