Page 1 of 1

disabled fields

Posted: 2016-07-18 21:36
by grimblefritz
It seems that if you use a hook to mark a field disabled, in addition to readonly, any data written to those fields is not saved.
EDIT:
I should clarify. The fields ARE saved, but with null data. So not only are any changes lost that might have been made programmatically, but any pre-existing data is overwritten with null data as well. Not good.
If the form is simply readonly, then field data is saved.

The problem is, the cursor will stop at a readonly field. It will skip over disabled fields. Obviously, during user input, disabled is better.

I tried using the click() method on the insert/update buttons to re-enabled my disabled fields, but data is still not written.

Do any of you know how to get this to work? Preferably disable then re-enable before write. However, it would also be acceptable if there was some javascript/jquery way to have the cursor simply autotab out if it enters a readonly field.

Thoughts anyone?

Re: disabled fields

Posted: 2016-07-20 12:45
by grimblefritz
I found the solution (it's HTML5 only, due to the tabindex, but that's okay for my needs):

Code: Select all

<input id="myfield"  ....  readonly tabindex="-1" onmousedown="return false;" style="cursor: not-allowed; background-color: #eee;">
That will make a field behave as if "disabled", without the deleterious​ side effects of the actual disabled attribute.

Using jquery, I can add/remove the readonly attribute with no problem.

How do I add/remove the onmousedown and style? I've tried several things, but can't seem to get it right.

Thanks

Re: disabled fields

Posted: 2016-07-20 13:51
by grimblefritz
After consulting The Almighty Oracle (aka, google), I have it:

Code: Select all

$j('#fieldname').prop('readonly', true);
$j('#fieldname').prop('tabIndex', -1);
$j('#fieldname').prop('style', 'cursor: not-allowed; background-color: #EEE;');
$j('#fieldname').mousedown(function(){return false;});
Now #fieldname will look and function just like a field that's been disabled.

In my app, I don't need to toggle this, but if needed it should be a matter of simply removing the added properties and mousedown handler.

Re: disabled fields

Posted: 2016-07-21 15:40
by grimblefritz
The above works for text fields, which was my primary concern. Now, however, I'd like to apply it to checkboxes. It doesn't work at all, so another method is needed.

I will, of course, go ask TAO about it, but perhaps someone here has some insight on the oddity that is checkbox fields?

Re: disabled fields

Posted: 2016-07-21 15:46
by grimblefritz
It works also on lookup fields, as well as date fields if the -mm and -dd segments are also specified.

It does not work for rich text fields.

I haven't tested with any others.

Re: disabled fields

Posted: 2016-07-23 12:12
by grimblefritz
Correction, it works for date fields.

It does not work for lookup fields.

Still looking for a solution, then, for lookup, text and checkbox fields.

Re: disabled fields

Posted: 2016-07-23 14:07
by grimblefritz
Found a workable solution for rich text fields:

Code: Select all

$j(document).on('show.bs.tab', 'a[data-toggle="tab"]', function(){
    $j('.nicEdit-main').css('cursor','not-allowed');
    $j('.nicEdit-main').attr('contenteditable','false');
    $j('.nicEdit-panel').hide();
});
This works when the text field is on a secondary tab - that is, not the initially active tab.

Which does not mean it won't work on the initial tab - only that I've not tested that scenario.

Re: disabled fields

Posted: 2016-11-01 17:19
by TheSpooki
This will make s2 lookups readonly

Code: Select all

$j('#yourfield-container').select2('enable', false);
just in case someone was looking for a solution.