diff options
| -rw-r--r-- | HISTORY | 4 | ||||
| -rw-r--r-- | framework/Exceptions/messages.txt | 4 | ||||
| -rw-r--r-- | framework/Web/UI/WebControls/TDataGridColumn.php | 26 | 
3 files changed, 30 insertions, 4 deletions
| @@ -1,3 +1,7 @@ +Version 3.1.0 To be released
 +============================
 +ENH: Format string in TDataGrid columns can now evaluate an expression (Qiang)
 +
  Version 3.0RC1 April 5, 2006
  ============================
  BUG: Ticket#85 - Undefined TDataGrid::setSelectedIndex (Qiang)
 diff --git a/framework/Exceptions/messages.txt b/framework/Exceptions/messages.txt index dd680098..6e2d676f 100644 --- a/framework/Exceptions/messages.txt +++ b/framework/Exceptions/messages.txt @@ -277,4 +277,6 @@ htmlarea_tarfile_invalid				= THtmlArea is unable to locate the TinyMCE tar file  parametermodule_parameterfile_unchangeable = TParameterModule.ParameterFile is not changeable because the module is already initialized.
  parametermodule_parameterfile_invalid	= TParameterModule.ParameterFile '{0}' is invalid. Make sure it is in namespace format and the file extension is '.xml'.
 -parametermodule_parameterid_required	= Parameter element must have 'id' attribute.
\ No newline at end of file +parametermodule_parameterid_required	= Parameter element must have 'id' attribute.
 +
 +datagridcolumn_expression_invalid		= {0} is evaluating an invalid expression "{1}" : {2}
\ No newline at end of file diff --git a/framework/Web/UI/WebControls/TDataGridColumn.php b/framework/Web/UI/WebControls/TDataGridColumn.php index de9d9d5c..a22fa240 100644 --- a/framework/Web/UI/WebControls/TDataGridColumn.php +++ b/framework/Web/UI/WebControls/TDataGridColumn.php @@ -331,17 +331,37 @@ abstract class TDataGridColumn extends TApplicationComponent  	}
  	/**
 -	 * Formats the text value according to format string.
 -	 * This method invokes the {@link sprintf} to do string formatting.
 +	 * Formats the text value according to a format string.
  	 * If the format string is empty, the original value is converted into
  	 * a string and returned.
 +	 * If the format string starts with '#', the string is treated as a PHP expression
 +	 * within which the token '$$' is translated with the data value to be formated.
 +	 * Otherwise, the format string and the data value are passed
 +	 * as the first and second parameters in {@link sprintf}.
  	 * @param string format string
  	 * @param mixed the data associated with the cell
  	 * @return string the formatted result
  	 */
  	protected function formatDataValue($formatString,$value)
  	{
 -		return $formatString===''?TPropertyValue::ensureString($value):sprintf($formatString,$value);
 +		if($formatString==='')
 +			return TPropertyValue::ensureString($value);
 +		else if($formatString[0]==='#')
 +		{
 +			$expression=strtr(substr($formatString,1),array('$$'=>'$value'));
 +			try
 +			{
 +				if(eval("\$result=$expression;")===false)
 +					throw new Exception('');
 +				return $result;
 +			}
 +			catch(Exception $e)
 +			{
 +				throw new TInvalidOperationException('datagridcolumn_expression_invalid',get_class($this),$expression,$e->getMessage());
 +			}
 +		}
 +		else
 +			return sprintf($formatString,$value);
  	}
  }
 | 
