Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/fix 1032: Allow custom mapping of template variable value -> display text #19972

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const InputWithAutoFocus = () => {
return (
<SegmentFrame>
{inputComponents.map((InputComponent: any) => (
<InputComponent intitialValue="test"></InputComponent>
<InputComponent intitialValue="test" />
))}
<a
className="gf-form-label query-part"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const Options: React.FC<Props> = ({ options, value, onSelect, timeZone }) => {
/>
))}
</div>
<div className={styles.grow}></div>
<div className={styles.grow} />
</>
);
};
Expand Down
21 changes: 16 additions & 5 deletions public/app/features/templating/custom_variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import {
} from './variable';
import { VariableSrv } from './variable_srv';

export interface OptionMapping {
__text: string;
__value: string;
}

export class CustomVariable implements CustomVariableModel, VariableActions {
type: VariableType;
name: string;
Expand Down Expand Up @@ -52,11 +57,17 @@ export class CustomVariable implements CustomVariableModel, VariableActions {
}

updateOptions() {
// extract options in comma separated string (use backslash to escape wanted commas)
this.options = _.map(this.query.match(/(?:\\,|[^,])+/g), text => {
text = text.replace(/\\,/g, ',');
return { text: text.trim(), value: text.trim(), selected: false };
});
try {
// content is a json ? read text and value
const mappings: OptionMapping[] = JSON.parse(this.query);
this.options = mappings.map(mapping => ({ text: mapping.__text, value: mapping.__value, selected: false }));
} catch (e) {
// extract options in comma separated string (use backslash to escape wanted commas)
this.options = _.map(this.query.match(/(?:\\,|[^,])+/g), text => {
text = text.replace(/\\,/g, ',');
return { text: text.trim(), value: text.trim(), selected: false };
});
}

if (this.includeAll) {
this.addAllOption();
Expand Down
16 changes: 15 additions & 1 deletion public/app/features/templating/partials/editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,21 @@ <h5 class="section-heading">Interval Options</h5>
<div ng-if="current.type === 'custom'" class="gf-form-group">
<h5 class="section-heading">Custom Options</h5>
<div class="gf-form">
<span class="gf-form-label width-14">Values separated by comma</span>
<span class="gf-form-label width-15">
Values separated by comma
<info-popover mode="right-normal">
You can also use a JSON-Array to define a displayed text for your value.
<br /><br />
<pre>
<code>
[{
"__text": "Friendly Value",
"__value": "myvalue"
},...]
</code>
</pre>
</info-popover>
</span>
<input
type="text"
class="gf-form-input"
Expand Down