a6283869
作者a6283869·2014-09-05 10:19
软件开发工程师·试试

JS控制日历控件选择时间范围

字数 5520阅读 1247评论 0赞 0

日期控件名称设置为myDatePrompt

JS代码:

 

<!-- This div is used to show the user the highest value they can choose in the prompt. -->

<div id="warningMessage_THIS_">

</div>

 

<script>

/*

 *

 * A hidden date value prompt returns the latest date from the database.

 * We get the values from a hidden date value prompt and compare it with value selected in the Date prompt control.

 * If the value is the Date prompt control is less than or equal to the hidden date value prompt value the Finish button will activate.

 * If the value is greater a warning message is displayed and the Finish button is inactive.

 *

 * The interesting part of this script is the use of prompt.getValues(True) which allows the script to

 * access all of the values in the hidden prompt regardless of user selection. Without this, the script could

 * not get the prompt value.

 *

 */

 /*

  *

  * Attach functions to an arbitrarily named object to mimic a namespace to ensure the name uniqueness

  *

  */

 var acme = {};

 

 /*

  *

  * This function checks to see what date value is greater

  *

  * Function arguments:

  *  @values is the array of values from the Date prompt the user uses to select values.

  *

  */

 acme.dateValidation = function(values){

  var dateValue = acme.rawDateToJSONSimple(values[0]['use']);

  var maxValue = acme.maxDateFetch();

  var promptDate = new Date(dateValue.y, dateValue.m, dateValue.d);

  var maxDate = new Date(maxValue.y, maxValue.m, maxValue.d);

  var result = promptDate.getTime() <= maxDate.getTime() ? true: false;

  var messageDiv = document.getElementById("warningMessage_THIS_");

  if (result) {

   messageDiv.innerHTML = "<span style='color: LightGrey; font-size: 12pt'>The largest permissible date value is: "  +  maxValue.y + "-" + maxValue.m + "-" + maxValue.d + ". </span>";

  } else {

   messageDiv.innerHTML = "<span style='color: LightCoral; font-size: 12pt'>The largest permissible date value is:  "  +  maxValue.y + "-" + maxValue.m + "-" + maxValue.d + ". Please select a lower date.</span>";

  } 

  return result;

 };

 

 /*

  *

  * This function gets the Date prompt and calls the validation function

  *

  *

  */

 acme.connectdateValidation = function(){

  var datePrompt = acme.getControl("myDatePrompt");

  datePrompt.setValidator(acme.dateValidation);

 };

 

 /*

  *

  * This function sets the latest date value from the hidden date value prompt to the parameter

  *

  *

  */

 acme.setDefaultMaxDate = function(){

  var maxDatePrompt = acme.getControl("myMaxDateHiddenPrompt");

  var values = maxDatePrompt.getValues(true);

  maxDatePrompt.setValues([values[0]]);

 };

 

 /*

  *

  * This function takes the JSON (string) date and breaks it apart into JSON representation of Year (y) Month (m) and Day (d)

  *

  * Function arguments:

  * @value is a date value in the format 2012-12-31T00:00:00.000000000

  *

  */

 acme.rawDateToJSONSimple = function(value){

  var timeRe = new RegExp("(....)-(.+)-(.+)T");

  var myDateParts = value.match(timeRe);

  var result = {

   "y":myDateParts[1],

   "m":myDateParts[2],

   "d":myDateParts[3]

  };

  return result;

 };

 

 /*

  *

  * This function gets the latest date value from the hidden date value prompt

  *

  */

 acme.maxDateFetch = function(){

  var maxDatePrompt = acme.getControl("myMaxDateHiddenPrompt");

  var values = maxDatePrompt.getValues(true);

  var rawResult;

  var result ;

  if (values.length == 1) {

   result = acme.rawDateToJSONSimple(values[0]['use']);

  }

  return (result);

 };

 

 /*

  *

  * This function gets the control by name from the report

  *

  */

 acme.getControl = function(promptName) {

  var oCR = cognos.Report.getReport("_THIS_");

  return oCR.prompt.getControlByName(promptName);

 };

 

 // Script execution starts here

 // Call the function to validate the default date prompt value

 acme.connectdateValidation();

 

 // Call the function to get the largest date from the hidden prompt

 acme.setDefaultMaxDate ();

 

</script>

<!--

Notice: Source Components and Sample Materials

The Program may include some components in source code form ("Source Components") and other materials identified as Sample Materials.

Licensee may copy and modify Source Components and Sample Materials for internal use only provided such use is within the limits of the license rights

under this Agreement, provided however that Licensee may not alter or delete any copyright information or notices contained in the Source Components

or Sample Materials. IBM provides the Source Components and Sample Materials without obligation of support and "AS IS", WITH NO WARRANTY OF ANY

KIND, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTY OF TITLE, NON-INFRINGEMENT OR NON-INTERFERENCE AND THE IMPLIED WARRANTIES

AND CONDITIONS OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

-->

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

0

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关问题

相关资料

X社区推广