Date Posted:
Product: TIBCO Spotfire®
Product: TIBCO Spotfire®
Problem:
How to automatically set a property value to the current date/time with a Text Area JavaScript.
Solution:
It is useful to have a property (document, data table, column) that is updated with the current date/time. One way this can be accomplished is by using JavaScript in a Text Area to update the property when the Text Area is referenced.
Create the Text Area:
- Select "Insert > Text Area" from the main menu.
Create the Input Field Property Control based on the Property:
- Hover the cursor over the upper right-hand corner of the new Text Area, to display its four toolbar icons.
- Hover the cursor over the pencil icon to display its tooltip ("Edit Text Area").
- Left-click on the pencil icon to open the "Edit Text Area" window.
- In the "Edit Text Area" dialog's toolbar, hover the cursor over the second icon from the right to display its tooltip ("Insert Property Control").
- In the "Edit Text Area" dialog, left-click this "Insert Property Control" button, then select "Input Field" from the drop-down menu to open the "Property Control" dialog.
- On the "Property Control" dialog's "Document Properties" tab, left-click the "New" button. This will open the "New Property" dialog.
- In the "New Property" dialog's "Property name" field, type (or paste) a name for the property (for example, "CurrentDateTimeProperty").
- In the "New Property" dialog's "Data type" drop-down list, ensure that the "String" data type has been selected.
- In the "New Property" dialog, left-click "OK". This will close the "New Property" dialog.
- On the "Property Control" dialog's "Document Properties" tab, note that the new property has been added to the list of properties.
- In the "Property Control" dialog', left-click "OK" to complete the insertion of the Input Field Property Control. This will also close the "Property Control" dialog.
- In the upper left corner of the "Edit Text Area" dialog, note that an empty property control has been added.
- At the left-hand end of the "Edit Text Area" dialog's toolbar, hover the cursor over the diskette icon to display its tooltip ("Save")
- Left-click on the "Edit Text Area" dialog's "Save" button.
- Note that the new property appears in the upper left corner of the Text Area.
- In the upper right corner of the "Edit Text Area" window, press the white-on-red [X] button to close the window.
Wrap Input Field Property control in a div:
- Right click on the Text Area and select "Edit HTML"
- Find the Input Field property control:
<SpotfireControl id="558f713f917a42c3b4bced0ef2950af0" />
- Wrap the control in a div by inserting <div id="myInputField"> before, and </div> after the previous line (preferably on separate lines). This id will be the unique identifier used by the JavaScript to access the element. Like:
<div id="myInputField"> <SpotfireControl id="558f713f917a42c3b4bced0ef2950af0" /> </div>
Create the JavaScript in the Text Area which references the Input Field Property Control:
- Right-click in the Text Area and select "Edit HTML" from the context-sensitive menu to open the "Edit HTML" dialog.
- On the "Edit HTML" dialog's toolbar, hover the cursor over the icon at the right-hand end of the toolbar to display its tooltip ("Insert JavaScript").
- In the "Edit HTML" dialog, left-click the "Insert JavaScript" button to open the "Insert JavaScript" dialog.
- In the "Insert JavaScript" dialog, left-click the "New" button to open the "New Script" dialog.
- In the "New Script" dialog's "Script name" field, type (or paste) a name for the script (for example, "setCurrentDateTimeProperty").
- In the "New Script" dialog's "Script" field, paste the following JavaScript text:
//////////////////////////////////////////////////// // Set an Input field value to the current date/time //////////////////////////////////////////////////// // Get Input field property control by its ID var elem = document.getElementById("myInputField"); //Get currentdate var currentdate = new Date(); //Format time parts to 2 digits currentHours = currentdate.getHours(); currentHours = ("0" + currentHours).slice(-2); currentMinutes = currentdate.getMinutes(); currentMinutes = ("0" + currentMinutes).slice(-2); currentSeconds = currentdate.getSeconds(); currentSeconds = ("0" + currentSeconds).slice(-2); //Create final date time format var datetime = (currentdate.getMonth()+1) + "/" + currentdate.getDate() + "/" + currentdate.getFullYear() + " " + currentHours + ":" + currentMinutes + ":" + currentSeconds; //Set the element value with the datetime $("#myInputField input").focus() $("#myInputField input").val(datetime).blur();
- In the "New Script" dialog, click "OK" to close the dialog.
- In the "Insert JavaScript" dialog, note that the new script has been added to the "Available scripts" field.
- In the "Insert JavaScript" dialog's "Available scripts" field, left-click on the new script to highlight it. Note that this also enables the dialog's "OK" button.
- In the "Insert JavaScript" dialog, left-click on the "OK" button to complete the insertion of the JavaScript. This will also close the "Insert JavaScript" dialog.
- In the upper right corner of the "Edit HTML" window, press the white-on-red [X] button to close the window.
See the attached (Filename: Set prop as current date w JS 7.5+.dxp) for reference.
Note:
For versions 7.5 to 7.14, the last section in the script in Step 6 should be modified to:
For versions 7.5 to 7.14, the last section in the script in Step 6 should be modified to:
//Set the element value with the datetime $("#myInputField input").val(datetime).blur();For versions 7.0 and lower, the last section in the script in Step 6 should be modified to:
//Set the element value with the datetime elem.childNodes[1].value = datetime;
Disclaimer:
The script code in this article is only a sample to be used as a reference. It is not intended to be used "As Is" in a Production environment. Always test in a Development environment. Make modifications to the script in accordance with your implementation specifications that best suit your business requirements. Refer to the API reference(s) cited in this article for usage of the classes and methods used in the script.
Comments
0 comments
Article is closed for comments.