Hello,
i am trying to set some input fields on my screen readonly. I was disappointed that the native HTML attribute readonly="true" did not work, but saw I could use
the native HTML attribute readonly="true" did not work, but saw I could use
$(
".selector"
).igEditor(
"option"
,
"readOnly"
true
);
However, some fields are igTextEditor, some are igMaskEditor, some are igNumerixEditor. etc. These controls are created via my own CSS class, so I don't have 300 separate initializations - ie :
$(".myTextClass').igTextEditor();
$(".myNumClass').igNumericEditor();
However only some of these are read only, so I want to add another class designating that the field is readonly ie :
$(".myReadOnlyClass").igEditor("option", "readOnly", true);
".myReadOnlyClass"
But when I do this I get :
Uncaught Error: cannot call methods on igEditor prior to initialization; attempted to call method 'option'
I presume this is because the fields were created as igTextEditor, igNumericEditor, etc and not igEditor. I thought igEditor would be like a 'superclass' meaning that its methods could be called on any subclass object.
Is there any 'generic' ig control name or function/method I can call on every ig control, no matter what the actual type of control it is?
Thank you
Hello CJ,
In short, it is supposed to use each editor’s instance/options in order to set the readOnly option. It could not be done like using igEditor to access and change all of the instantiated editors options at once. Thus the following will not work in the way you require: $(".myReadOnlyClass").igEditor("option", "readOnly", true);
What is more, there is no ‘generic’ method to achieve this. However, what you require is to use a custom function to set the readOnly option based on the actual instance type. For example, I came with the following function and implemented a sample illustrating a possible approach.
$("#button1").on("click", function () { $(".myEditorsClass").toggleClass("myEditorsReadOnlyClass"); $(".myEditorsReadOnlyClass").each(function () { var input = $(this); var inputData = $(this).data(); var keys = Object.keys(inputData); if (keys.length > 0) { editorType = keys[0]; var alt = "$(this)" + "." + editorType.toString() + "('option', 'readOnly', true)"; eval(alt); } }); });
I initialize all editors with “myEditorsClass” and later on a btn click add “myEditorsReadOnlyClass” to all igEditors as well as making the editors readOnly based on their actual type. I believe this approach is appropriate for you, as you are independent of the actual containers ID’s each editor is instantiated on and use only classes.
I am also attaching a runnable sample.Please let me know how my suggestions work for you.
In reading your answer, my next question was if there was some equivalent to the Java 'instanceof' operator that would tell me what the object was that I had (what 'this' is) so that I could then call the proper method. Then I looked at and debugged your code and that is exactly what you are doing - finding the type of object via the 'keys' of the object. I can use this information to do what I need, but with 2 questions.
1) This feels like a hack in that its not using a documents API, and you (Infragistics) can change the format of the objects you create ( like keys[0] stops being the value 'of the object type). Is this documented somewhere for developers to use safely?
2) I am unsure why you suggested setting my own class name, and then using 'toggleClass' in this code. I used this code without this complication. Was there a reason for it?
$(".myReadonlyClass").each(function () {
var inputData = $(this).data();
var keys = Object.keys(inputData);
if (keys.length > 0) {
editorType = keys[0];
var alt = "$(this)" + "." + editorType.toString() + "('option', 'readOnly', true)";
eval(alt);
}
});
Thanks for your help.