- CFScript Example
- 使用方法
CFScript Example
代码语言:javascript复制<cfscript>
data = { "FirstMetricGoal"=6.283185307179586476925286766559,
"FirstMetricRate"=3.1415926535897932384626433832795,
"SecondMetricGoal"=5.4365636569180904707205749427053,
"SecondMetricRate"=2.7182818284590452353602874713527
};
// Data for round Charts
data.roundChartAttributes = arrayNew(1);
for(i=1; i<=5 ; i=i 1)
{
data.roundChartAttributes[i] = structNew();
data.roundChartAttributes[i].currentValue = 0.5;
data.roundChartAttributes[i].yellowThreshold = 0.3;
data.roundChartAttributes[i].greenThreshold = 0.5;
data.roundChartAttributes[i].topScale = 1.0;
data.roundChartAttributes[i].allTransparent = "true";
data.roundChartAttributes[i].meterTitle = "Chart 1";
data.roundChartAttributes[i].boxHeight = "120";
data.roundChartAttributes[i].boxWidth = "120";
data.roundChartAttributes[i].radius = "50";
}
// Data for TriangleChart
data.arrTriangleChart = arrayNew(1);
// Format: Title | Position in Triangle (1 is the top) | Value to display | Color to display (Hex or string if available) | Text to display on helptext
data.arrTriangleChart[1] = "test1|1|1111|ffffff|test.";
data.arrTriangleChart[2] = "test2|2|222|ffffff|test22.";
data.arrTriangleChart[3] = "test3|3|333|ffffff|test33.";
data.arrTriangleChart[4] = "test3|4|333|ffffff|test33.";
data.arrTriangleChart[5] = "test3|5|333|ffffff|test33.";
// Data for Regulatory Training Completion
data.TrainCompletion_YTDElapsed="0.5";
data.TrainCompletion_YTD2014="0.8";
// Data for First Aid/NM/Concerns Rate
data.FA_NM_Concern_Rate="0.5";
// Data for Calendar Closure Rate
data.Calendar_Closure_Rate="0.8";
// Data for Incurred Costs
data.IncurredCosts_3YrAvg="2000";
data.IncurredCosts_2014="3000";
// Data for Finding Closure Rate
data.FindingClosureRate_PrioA="0.9";
data.FindingClosureRate_PrioB="0.9";
data.FindingClosureRate_PrioC="0.9";
data.FindingClosureRate_PrioD="0.9";
// Data for Finding Ratio
data.FindingRatio_Site="250";
data.FindingRatio_Crop="750";
</cfscript>
SerializeJSON
代码语言:javascript复制<cfparam name="IncludeSubOrgs" default="">
<!--- <cfheader name="Content-Type" value="application/json"> --->
<cfquery name="getSites" datasource="">
……
</cfquery>
<cfcontent reset="true" />
<cfoutput>
#SerializeJSON(getSites,true)#
</cfoutput>
Query of Query
代码语言:javascript复制<cfset subCategoryIDs=valueList(getsubcategories.subCategoryID)>
代码语言:javascript复制<cfquery name="getproducts">
SELECT *
FROM products
WHERE subCategoryID in (<cfqueryparam cfsqltype="cf_sql_integer" value="#subCategoryIDs#" list="true">)
ORDER BY RAND()
</cfquery>
Object
FindStructFindValue
代码语言:javascript复制<cfset sbiztemplist = "">
<cfset arrSearch = structfindvalue(structConcerns.IncidentsDetail,subbusiness,"all")>
<cfloop from="1" to="#arrayLen(arrSearch)#" index="arr">
<cfif arrSearch[arr].key eq "SubBusiness">
<cfif NOT listfindNoCase(sbiztemplist,arrSearch[arr].owner.Orgname,"|")>
<cfset sbiztemplist = listAppend(sbiztemplist,arrSearch[arr].owner.Orgname,"|")>
</cfif>
</cfif>
</cfloop>
Create a Excel and write data and download
代码语言:javascript复制<!--- Create a obj --->
<cfset decSheetObj=SpreadsheetNew()>
<!--- Write something --->
<cfscript>
spreadsheetSetCellValue(variables.decSheetObj, 123, 56, 4);
</cfscript>
<!--- Write to server --->
<cfspreadsheet action="write" filename="/report.xlsx" sheetname="Declaration" name="decSheetObj" overwrite="true" />
<!--- Download --->
<cfheader name="Content-Disposition" value="attachment; filename=Report_#dateFormat(now(), 'yyyymmdd ')##timeFormat(now(), 'HHmmss')#.xlsx" />
<cfcontent file="/report.xlsx" type="application/vnd.ms-excel" deletefile="true" />
ColdFusion to js
代码语言:javascript复制JSStringFormat(string)
Special Characters & Quotations
写成两个就行了
代码语言:javascript复制<cfset mystring="We all said ""Happy birthday to you.""">
<cfset mystring2='Then we said "How old are you now?"'>
<cfoutput>
#mystring#<br>
#mystring2#<br>
Here is a number sign: ##
</cfoutput>
Output:
代码语言:javascript复制We all said "Happy birthday to you."
Then we said "How old are you now?"
Here is a number sign: #
List
在 HTML 中以
代码语言:javascript复制<cfset local.errorRowNum="123,321,124" />
的形式出现 一开始可以使用空值, 不需要 ListNew()这样的方法来新建 List, 之后再使用 ListAppend 来添加数据
ListAppend
一定要赋值给原来的 list, 单独调用一下函数不赋值是不会有效果的.
代码语言:javascript复制<cfset fruitlist = ListAppend(fruitList, "kiwi") />
Query to List: ValueList
将 Query 的 Result 转换为 List
代码语言:javascript复制valueList(column, [delimiter])
ListLen
获取 List 的长度, 在 Loop 的时候可能会用到
ListContains
代码语言:javascript复制ListContains(list, substring [, delimiters, includeEmptyValues ])
##ListContains(aList, "wo")#
Description
Determines the number of elements in a list. Integer; the number of elements in a list.
Function syntax
代码语言:javascript复制ListLen(list[, delimiters])
ListLen(list[, delimiters, includeEmptyValues])
Parameters
- list
A list or a variable that contains one.
- delimiters
A string or a variable that contains one. Characters that separate list elements. The default value is comma. If this parameter contains more than one character, ColdFusion processes each occurrence of each character as a delimiter.
- includeEmptyValues
If includeEmptyValues is set to true, all empty values in the list will be considered when computing length. If set to false, the empty list elements are ignored.
Example
代码语言:javascript复制<cfset list="Partner,Response Date,Response Status" />
<cfloop from="1" to="#listLen(list)#" index="i">
<cfoutput>
listGetAt(list, i)
</cfoutput>
</cfloop>
Array
在 cfscript
中以
data=[0,0,0,0,0]
的形式出现
在 HTML 中以
代码语言:javascript复制<!--- 这句声明可以省略 --->
<cfset local.errorRowNum=arrayNew(1) />
<cfset local.errorRowNum=[1,2,3] />
<!--- 可以不按顺序赋值,但是使用未赋值的 index 可能报错 --->
<cfset local.errorRowNum[3]=arrayNew(1) />
<cfset local.errorRowNum[4]=arrayNew(1) />
<cfset local.errorRowNum[5]=arrayNew(1) />
以及
代码语言:javascript复制<cfset myarray=ArrayNew(1)>
<cfset myotherarray=ArrayNew(2)>
<cfset biggerarray=ArrayNew(3)>
<cfset biggerarray[1][1][1]=myarray>
<cfset biggerarray[1][1][1][10]=3>
<cfset biggerarray[2][1][1]=myotherarray>
<cfset biggerarray[2][1][1][4][2]="five deep">
<cfset biggestarray=ArrayNew(3)>
<cfset biggestarray[3][1][1]=biggerarray>
<cfset biggestarray[3][1][1][2][3][1]="This is complex">
<cfset myarray[3]="Can you see me">
的形式出现
几个特性
- 效率比 Struct 要快
ArrayAppend
代码语言:javascript复制<cfset ArrayAppend(array_name, {value})>
放在 cfset 之中, 或者直接在 output 的时候用双井号调用 注意如果赋值的话就是 True or False
ListToArray
格式
代码语言:javascript复制listToArray("list", [delimiters])
Demo
代码语言:javascript复制<h1>Demo for listToArray</h1>
<cfquery name="query" datasource="CC_TEST">
select top(3) * from ltbContact;
</cfquery>
<cfdump var="#query#" expand="false">
<cfset xAxisLabelsdata=listToArray(ValueList(query.CONTACT_LASTNAME,'|'),'|') />
<cfdump var="#xAxisLabelsdata#" expand="false">
Query to Array: toArray()
代码语言:javascript复制qSites.siteid.toArray()
Debug Tips
由于 ColdFusion 是服务器端进行解析的, 一般不能够分步执行
一般使用的调试方法
- 使用
<cfabort>
来中断某段代码后方的所有代码 - 使用
<cfdump>
来进行查看, 甚至可以查看 - 可以直接 dump 出表单名来查看所有数据
- 在
<cfscript>
中可以使用
WriteDump(vars)
来进行 dump
cfinclude & include in cfscript
用于文件引用
示例
代码语言:javascript复制<cfinclude template="myinclude.cfm">
<cfinclude template="……/myinclude.cfm">
<cfinclude template="/CFIDE/debug/myinclude.cfm">
<cfscript>
include "myinclude.cfm";
</cfscript>
ColdFusion Linebreak
tags: 换行, line break, coldfusion
Solution1&2
Use Chr(10)
as n
There is no support for n or r in CF. From the Live Docs
- Chr(10) returns a linefeed character
- Chr(13) returns a carriage return character
- The two-character string Chr(13) & Chr(10) returns a Windows newline
Available if for normal string var output
代码语言:javascript复制<cfset someStr="This is line 1" & Chr(10) & "This is line 2" & Chr(10) & "This is line 3" />
代码语言:javascript复制<cfset NL=Chr(10) />
<cfset someStr="This is line 1 #NL# This is line 2#NL#This is line 3" />
Solution3
If you are into platform-independent development, you can do:
代码语言:javascript复制<cfset NL=CreateObject("java", "java.lang.System" ).getProperty("line.separator")>
For example, in your application.cfm/cfc
or somewhere else high-level and use that.
Solution4
use
代码语言:javascript复制<*br*>
example:
代码语言:javascript复制"labelFontSize" = 7,
"labels" = ["Near Misses & Concern <*br*> Reports Logged", "First Aid Cases", "Other Recordable Cases", "Job Transfer or Restriction", "DAFW", "Fatalities"]
cfparam
代码语言:javascript复制<cfparam name="FORM.username" default="NULL"/>
使用方法
- 可以直接写对应的 CF 变量
- 可以使用表单提交过来的数据, 注意是获取表单里面对应 name 的数据
- 可以使用
type
参数设定获取过来的类型, 如果不是对应类型就会报错?