Reputation: 821
I have the below code on a ColdFusion page. This page displays the data from the query below having 25 records per page with pagination. Now I need to provide a textbox and search button so that the user can enter the positionid and hit search....the problem I'm having here is how can the nth page be displayed if the positionid is in for example 7th page out of 200 pages. Please advice. Thanks
<cfquery name="qry_postn_detail" datasource="mbtran">
select distinct position_id,schedule_group,accrual_profile,pay_rule_name,rest_days
from kronos_if.position_detail
order by position_id
</cfquery>
<cfset perpage = 25>
<cfparam name="url.start" default="1">
<cfif not isNumeric(url.start) or url.start lt 1 or url.start gt qry_postn_detail.recordCount or round(url.start) neq url.start>
<cfset url.start = 1>
</cfif>
<cfset totalPages = ceiling(qry_postn_detail.recordCount / perpage)>
<cfset thisPage = ceiling(url.start / perpage)>
<cfset thisPage = Int(start / 25) + 1>
Page<cfoutput>
<cfloop from="1" to="#totalPages#" index="i">
<cfif i is thisPage>
#i#
<cfelse>
<cfif totalPages lte 5 or i is 1 or i is totalPages or (i gt thisPage - 3 and i lt thisPage + 3) or ((thisPage is 1 or thisPage is 2) and i lt 6) >
<a href="?start=#(i*25)-24#">#i#</a>
<cfelse>
<cfif i is 2 or i is totalPages - 1>
...
</cfif>
</cfif>
</cfif>
</cfloop>
</cfoutput>
Upvotes: 0
Views: 1093
Reputation: 13548
Based on your comment, I think all you need to do is include the search parameter in your query upon search form submission. Then the query result will only contain records matching what they searched for and your pagination code will still work as before but only for the matched records this time.
So your query would look something like:
<cfquery name="qry_postn_detail" datasource="mbtran">
select distinct position_id,schedule_group,accrual_profile,pay_rule_name,rest_days
from kronos_if.position_detail
<cfif IsDefined("form.searchfield") AND IsValid("integer", form.searchfield)>
where position_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#form.searchfield#">
</cfif>
order by position_id
</cfquery>
NOTE: I am assuming the position_id is an integer in your table. You will need to change the cfqueryparam type and validation logic appropriately based on it's actual type.
UPDATE from the new question posted in the comments below
Here is a quick and simple example of how to use JavaScript to jump to a particular page. You mentioned that you already have a select
box with the page numbers. All you need to do is add the onChange
event handler to your select
tag and call a JavaScript function that will reload the page with the selected value appended to the url.
Your select
will look something like this:
<select name="page" onChange="jump(this.value);">
<option value="101">101</option>
<option value="102">102</option>
<option value="103">103</option>
<option value="104">104</option>
</select>
A simple JavaScript example looks like this:
<script language="JavaScript">
function jump(pagenum) {
document.location.href("?start="+pagenum);
}
</script>
Remember to validate/sanitize any and all values being passed to your ColdFusion pages.
Upvotes: 1