Coldfusion MX PageList cainiao tutorial

Source: Internet
Author: User

Originally written:

1. functions that can be implemented:
Navigation of the home page, last page, Previous Page, next page, and specified page.
The last page of the home page is automatically hidden.
The jump drop-down menu displays the current page number and total page number dynamically.

2. Principles

For core principles, refer to the paging principle of tripdetail. cfm In the Compass Travel example included in Codefusion MX. Assume that the current page is 6th pages, and the database is queried. The number of returned maxRows is set to 1. The previous page is used to query the number of rows smaller than 6 in reverse order. The result is 5, 4, 3 ..., because only one value is returned, 5 is returned. Others.

3. Usage

Place the code in the place where pagelist is implemented, replace the cfsnippets, centers, and center_ID in the modification with the actual Database Name, table name, and field name.

4. limitations and deficiencies

Because I have learned cf for less than a week and I am not very familiar with cfml, some code is still very complicated. I think there are some shortcomings:
(1) styles have limitations. Because form is used, only buttons or images can be used for display, rather than plain text.
(2) You must replace the modification with search. It was originally assumed that only the three variables defined in the previous database could be modified, but the query results must be determined in <cfout>, for example, # gotopage. currentrow #, instead of using dynamic parameters in it. What are the best solutions for the experts?

<! --- Database definition --->
<Cfset databasename = "cfsnippets"> <! --- Database name --->
<Cfset tablename = "centers"> <! --- Table name --->
<Cfset targetname = "center_ID"> <! --- Field name (generally ID). When defining this field, replace the center_ID in all gotopage. center_ID with search. --->

<! --- Handle Jump Actions --->
<Cfif IsDefined ("Form. RecordID")> <! --- Determine whether a jump request exists --->
<Cfquery name = "pageQuery" datasource = "# databasename #" maxrows = "1">
SELECT # targetname # FROM # tablename #
<Cfif IsDefined ("Form. btnPrev")> <! --- Previous Page code --->
WHERE # targetname # <# Form. RecordID #
Order by # targetname # DESC
<Cfelseif IsDefined ("Form. btnNext")> <! --- Next page code --->
WHERE # targetname #> # Form. RecordID #
Order by # targetname #
<Cfelseif IsDefined ("Form. btnFirst")> <! --- Homepage page number --->
Order by # targetname #
<Cfelseif IsDefined ("Form. btnLast")> <! --- Last page number --->
WHERE # targetname #> # Form. RecordID #
Order by # targetname # DESC
<Cfelseif IsDefined ("Form. goto")> <! --- Specify page number --->
WHERE # targetname # = # Form. goto #
</Cfif>
</Cfquery>
<Cfif pageQuery. RecordCount is 1>
<Cflocation url = "# cgi. SCRIPT_NAME #? ID = # pageQuery. center_ID # "> <! --- Jump --->
<Cfelse>
<Cflocation url = "# cgi. SCRIPT_NAME #? ID = # page. RecordID # ">
</Cfif>
</Cfif>

<! --- Get the ID of the home page and the last page --->
<Cfquery name = "gotopage" datasource = "# databasename #">
SELECT # targetname # FROM # tablename #
</Cfquery>
<Cfoutput query = "gotopage">
<Cfif gotopage. currentrow is 1>
<Cfset firstid = gotopage. center_ID> <! --- ID of the home page --->
<Cfelseif gotopage. currentrow is gotopage. recordcount>
<Cfset lastid = gotopage. center_ID> <! --- ID corresponding to the last page --->
</Cfif>
</Cfoutput>

<! --- Get the ID corresponding to this page. If it is not passed, the default value is the homepage ID. --->
<Cfif isdefined ("url. id")>
<Cfset pageid = url. id>
<Cfelse>
<Cfset pageid = firstid>
</Cfif>

<! --- Flip the main part --->
<Form action = "# cgi. SCRIPT_NAME #" method = "post">
<Input type = "hidden" name = "RecordID" value = "<cfoutput> # pageid # </cfoutput>"> <! --- Transfer the ID of this page by hiding fields --->

<! -- Homepage/Previous Page -->
<Cfif pageid neq firstid>
<Input type = "submit" name = "btnFirst" value = "Homepage">
<Input type = "submit" name = "btnPrev" value = "Previous Page">
</Cfif>

<! -- Page number, jump -->
<B> jump to: </B> NO. <select name = "goto">
<Cfoutput query = "gotopage">
<Cfif gotopage. center_ID is pageid>
<Option value = "# gotopage. center_ID #" selected> # gotopage. currentrow # <! --- Make the number on this page in the selected status --->
<Cfelse>
<Option value = "# gotopage. center_ID #"> # gotopage. currentrow #
</Cfif>
</Cfoutput>
</Option> </select>/<cfoutput> # gotopage. recordcount # </cfoutput> page
<Input name = "Go" type = "submit" value = "GO">

<! -- Last page/next page -->
<Cfif pageid neq lastid>
<Input type = "submit" name = "btnNext" value = "Next">
<Input type = "submit" name = "btnLast" value = "last">
</Cfif>
</Form>

Later, I found that something was wrong and modified it again:

The code above can only be used to list pages with only one record on each page. If one page has multiple records, the above method will not work.

The following is the modified Code. Multiple records can be stored on a page. The number of records on each page can be defined in pagerow, in addition, you don't need to change it by searching and replacing it. You just need to set the four parameters in the initialization as their own content, and you don't need to change them elsewhere.

The code is much simpler than the original one.

<! --- Initialization --->
<Cfset databasename = "cfsnippets"> <! --- Database name --->
<Cfset tablename = "centers"> <! --- Table name --->
<Cfset targetname = "center_ID"> <! --- Field name (generally ID) --->
<Cfset pagerow = 1> <! --- Number of records per page --->
<! --- Handle Jump Actions --->
<Cfif IsDefined ("Form. thispage")> <! --- Determine whether a jump request exists --->
<Cfif IsDefined ("Form. btnPrev")> <! --- Previous Page code --->
<Cfset pageQuery = # Form. thispage #-1>
<Cfelseif IsDefined ("Form. btnNext")> <! --- Next page code --->
<Cfset pageQuery = # Form. thispage # + 1>
<Cfelseif IsDefined ("Form. btnFirst")> <! --- Homepage page number --->
<Cfset pageQuery = 1>
<Cfelseif IsDefined ("Form. btnLast")> <! --- Last page number --->
<Cfset pageQuery = # Form. lastpage #>
<Cfelseif IsDefined ("Form. goto")> <! --- Specify page number --->
<Cfset pageQuery = # Form. goto #>
</Cfif>
<Cflocation url = "# cgi. SCRIPT_NAME #? Page = # pageQuery # "> <! --- Jump --->
</Cfif>
<! --- Get the last page number --->
<Cfquery name = "gotopage" datasource = "# databasename #">
SELECT # targetname # FROM # tablename #
</Cfquery>
<Cfset lastpage = # gotopage. recordcount # \ pagerow> <! --- Last page number --->
<! --- Obtain the page number of the current page. If the page number is not transmitted, the default value is 1 --->
<Cfif isdefined ("url. page")>
<Cfset pageid = url. page>
<Cfelse>
<Cfset pageid = 1>
</Cfif>
<! --- Flip the main part --->
<Form action = "" method = "post">
<Input type = "hidden" name = "thispage" value = "<cfoutput> # pageid # </cfoutput>"> <! --- Transfer the ID of this page by hiding fields --->
<Input type = "hidden" name = "lastpage" value = "<cfoutput> # lastpage # </cfoutput>"> <! --- Hide the last page number of the field transfer --->
<! -- Homepage/Previous Page -->
<Cfif pageid neq 1>
<Input type = "submit" name = "btnFirst" value = "Homepage">
<Input type = "submit" name = "btnPrev" value = "Previous Page">
</Cfif>
<! -- Page number, jump -->
<B> jump to: </B> NO. <select name = "goto">
<Cfloop index = "pagenumber" from = "1" to = "# lastpage #">
<Cfoutput>
<Cfif # pagenumber # is pageid>
<Option value = "# pagenumber #" selected> # pagenumber # <! --- Make the number on this page in the selected status --->
<Cfelse>
<Option value = "# pagenumber #"> # pagenumber #
</Cfif>
</Cfoutput>
</Cfloop>
</Option> </select>/<cfoutput> # lastpage # </cfoutput> page
<Input name = "Go" type = "submit" value = "GO">
<! -- Last page/next page -->
<Cfif pageid neq lastpage>
<Input type = "submit" name = "btnNext" value = "Next">
<Input type = "submit" name = "btnLast" value = "last">
</Cfif>
</Form>

Haha, A cainiao version of pagelist has finally been completed, which is very simple and stupid to use.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.