2012-10-03 129 views
0

我在ColdFusion页面上有下面的代码。此页面显示下面的查询中的数据,每页有25条记录并附有分页。现在我需要提供一个文本框和搜索按钮,以便用户可以输入positionid并点击搜索....我在这里遇到的问题是,如果positionid在例如第7页出现时如何显示第n页200页。请指教。谢谢Coldfusion分页和搜索

<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> 
+0

你能提供一些关于你如何工作的更多细节吗?你提到添加一个文本框进行搜索,但你说他们输入一个特定的positionid。您想要在搜索后再次显示所有结果,还是只想记录匹配的内容?还是你想显示可点击的页码在这些页面的结果之间来回切换?更多详细信息请... –

+0

当用户输入positionid并点击搜索结果必须显示匹配的搜索结果....与可点击的页码来回去......抱歉不清楚.... – user747291

回答

1

根据您的评论,我认为你需要做的就是在搜索表单提交时在你的查询中包含搜索参数。然后,查询结果将只包含与他们搜索的内容匹配的记录,并且您的分页代码仍然可以像以前一样工作,但这次只能用于匹配的记录。

所以您的查询看起来是这样的:

<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> 

注意:我假设position_id是表中的整数。您需要根据实际类型适当地更改cfqueryparam类型和验证逻辑。从张贴在评论下方

新问题

更新这里是如何使用JavaScript跳转到一个特定的页面快速和简单的例子。你提到你已经有一个带有页码的select框。您只需将onChange事件处理程序添加到select标记中,然后调用JavaScript函数,该函数将使用附加到url的选定值重新加载页面。

select会是这个样子:

<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> 

一个简单的JavaScript的例子看起来是这样的:

<script language="JavaScript"> 
    function jump(pagenum) { 
     document.location.href("?start="+pagenum); 
    } 
</script> 

记住验证/进行杀毒传递给您的ColdFusion页面的任何和所有值。

+0

非常感谢它的工作! – user747291

+0

@ user747291太好了!很高兴我能帮上忙。如果您不介意,请将其作为答案,以便其他人发现此问题可以找到答案。谢谢。 –

+0

如何为我想要去的特定页面添加搜索选项。就像我有200页。为了跳到第120页,而不是点击直到第120页。请建议。谢谢 – user747291