I want Hide/show admin form field on toggle value using UiComponent.
when enable the toggle fields should be visible and on disable the toggle fields should not be visible.
Example:
[When enable the toggle][1]
[When Disable the toggle][2]
1 Answer
You can use below code for Hide/show admin form field and change field name accordingly.
app/code/Namespace/ModuleName/view/adminhtml/web/js/form/element/options.js
define([
'underscore',
'uiRegistry',
'Magento_Ui/js/form/element/select',
'Magento_Ui/js/modal/modal'
], function (_, uiRegistry, select, modal) {
'use strict';
return select.extend({
/**
* On value change handler.
*
* @param {String} value
*/
onUpdate: function (value) {
console.log('Selected Value: ' + value);
var field1 = uiRegistry.get('index = test2');
if (field1.visibleValue == value) {
field1.show();
} else {
field1.hide();
}
var field2 = uiRegistry.get('index = test3');
if (field2.visibleValue == value) {
field2.show();
} else {
field2.hide();
}
return this._super();
},
});
});
Thanks!
-
Do you have any idea how to do same with text input, like if we open form in edit mode then only field should be visible? Jan 13, 2020 at 9:24
-
@RonakChauhan I don't have implement for text input. But you can try with get text input value and check, might that things you can help. Jan 21, 2020 at 9:52
-
I have found the solution for text toggle. it can work without js @Kirti Nariya Jan 21, 2020 at 10:05
-
@RonakChauhan It is a nice. can you please share that solution as you get. so also it is helpful in future. Jan 21, 2020 at 10:08
-