获取coldfusion列表中的下三个元素

时间:2015-10-15 16:59:34

标签: list coldfusion

我有一个这样的列表,例如,7141,6881,5821,8001,7904,6601,7961,6021,4721,其中每个都是员工的ID。我有一个页面,其中包含员工描述,以及下一个按钮。点击下一步,我需要让下一个员工的详细信息如下所示。

<cfset getempls(arguments.id) />

    <cfset local.emp_id_list = valueList(VARIABLES.employee.emp_id) />
    <cfset emp_index = listFind(local.emp_id_list, arguments.emp_id) />
    <cfset local.emp_id = "" />
    <cfif emp_index LT VARIABLES.employee.recordcount>
        <cfset local.emp_id = listGetAt(local.emp_id_list, emp_index + 1) />
    </cfif> 

我还需要在详细页面上预览下3名员工,这意味着,在emp_ID 7141的详细页面上,我还需要显示6881,5821,8001的详细信息;需要经过清单直到最后。任何人都可以建议最好的方法来做到这一点。我做了类似下面的事情,但即使名单少于3名员工,我也需要这样做。有什么想法吗?感谢

     <cfif listlen(local.emp_id_list) eq 3> 
            <cfset local.next_three_emp_id_list = listappend(local.next_3_emp_id_list, listGetAt(local.emp_id_list, emp_index + 2)) />  
            <cfset local.next_three_emp_id_list = listappend(local.next_three_emp_id_list, listGetAt(local.emp_id_list, emp_index + 3)) />
        </cfif> 

2 个答案:

答案 0 :(得分:1)

虽然我通常会尽量避免使用会话变量,但这可能是适当的情况。

在第一页上,更改:

<cfquery name = "employee">

<cfquery name = "session.employee">

然后这样做:

<cfoutput query = "session.employee">
<a href=page2.cfm?row=#rownumber#">somethigng</a>
第2页上的

执行此操作:

<cfoutput query = "session.employee" startrow = url.row maxrows="3">
display code

答案 1 :(得分:0)

当一个人有一个数组开始时,永远不应该使用一个列表(好吧:一个人应该尽可能不使用列表)。考虑到这一点,here's a solution处理ID数组,而不是ID列表。

<cfscript>
function nextIds(ids, id, count){
    var thisIdIdx = ids.find(id);
    var idsLen = ids.len();
    if (thisIdIdx == 0 || thisIdIdx == idsLen){
        return [];
    }
    return ids.slice(thisIdIdx+1, min(idsLen-thisIdIdx, count));
}
</cfscript>