Hi Jeff,
Note - I've used example 53, Validation, when answering this question.
This is a great question because it neatly encapsulates the desire to control something in the browser, something not directly supported by the templates, but which is something one might want to do.
You're right in one sense - the key is in figuring out how the JavaScript and the Clarion code interact. There are a couple of slight differences in the approach one can take in NT6 and NT7 though - I'll try and cover both.
NT7 has a method that NT6 does not have - which is the ability to enable, and disable, the Save button. This is used internally by the FileUpload script, so that the Save button is only enabled when all the files have been uploaded. Assuming you don't have a file upload on the form you could hook into this.
In _JavaScript_ the way to enable, or disable the button wold look like this;
$("#MailboxesFormControl_frm").ntform("disableSave");It's worth breaking this down a bit;
$ is shorthand for jQuery
"#MailboxesFormControl" is the element on the page that the ntform is attached to. I know this 'cause I looked at the source of the form and saw;
<script defer="defer">
jQuery(function() {jQuery("#MailboxesFormControl_frm").ntform({procedure: "mailboxesformcontrol",tabType:3, action: "index.htm",actionCancel: "index.htm",actionCancelTarget: "",actionTarget: "_self",popup:0});});
</script>ntform is the name of the widget. (This is some NT JavaScript code that makes a form into, well, a form.)
disableSave is the name of the method to call. (remember JavaScript is case sensitive.)
You can test JavaScript by entering it into the Console window in firebug. (see attached pic A1.Png).
Once you have the JavaScript, you can then morph it a bit so it can be embedded in a clarion embed point. Fortunately NT comes with a method that make this easy. In the validation example I've added the following to the
Validate::MAI:Speed routine, after the call to
do ValidateValue::MAI:Speed
if loc:Invalid
p_web.Script('$("#MailboxesFormControl_frm").ntform("disableSave");')
else
p_web.Script('$("#MailboxesFormControl_frm").ntform("enableSave");')
end
A slightly better form, because it is more generic would be;
if loc:Invalid
p_web.Script('$("#'& clip(loc:FormName)&'").ntform("disableSave");')
else
p_web.Script('$("#'& clip(loc:FormName)&'").ntform("enableSave");')
end
This doesn't hard-code the procedure name.
So great, in NT7 there is a disableSave method. But what if you are using NT6 or you want to interact with some other control?
The first problem is in identifying the control. The save button does not have a fixed ID (because multiple save buttons are allowed on a page) so we need to be more creative in selecting it.
(A full discourse on jQuery selectors is outside the scope of this reply, but suffice to say they are extremely powerful once you get the hang of them. A good place to start is here;
http://api.jquery.com/category/selectors/)
NetTalk follows a pattern when it comes to identifying things. Instead of locating things by id, it's easier to locate them by function. And the function of many things is set in the data-do attribute. Take a closer look at the Save button;
<button data-do="save" id="save_btnLfse" name="save_btn">Save</button>I've radically shortened this, but you can see the data-do attribute. In the earlier script code we used # and the ID of the form to locate it. Now we use a slightly different syntax to locate the item based on the data-do.
$('[data-do="save"]')note that this time there's a mix of single quotes, and double quotes. Also note that the attribute test is placed in square brackets, indicating that it is an attribute we are testing.
so let's do something simple, like hide the button - the easiest way to hide something is to add the nt-hidden class to it;
$('[data-do="save"]').addClass("nt-hidden")note that this will hide _all_ the save buttons that are visible, which is good because it's probably what you want at this point.
In embed code this would be;
p_web.Script('$(''[data-do="save"]'').addClass("nt-hidden")')naturally the single quotes become two single quotes - don't be confused by that.
Disabling the button would be slightly trickier because the button is (at least in that example) a jQuery widget button. It has a disable method, but to access it we need the id, or, if there are more than one, a list of id's. Also the button itself is a bit different in mobile mode or desktop mode. So if disabling is really the key here then I recommend using NT7 for that.
So your question covers jQuery selectors, JavaScript, and embedding the JavaScript in NT. Great question - thanks.
cheers
Bruce
[attachment deleted by admin]