【问题标题】:Get next three elements in a coldfusion list获取coldfusion列表中的下三个元素
【发布时间】:2015-10-16 20:35:19
【问题描述】:

我有一个这样的列表,例如 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> 

【问题讨论】:

  • 使用数组更容易做到这一点。您可以利用数组索引作为偏移量。您可以使用listToArray 函数转换您的列表。
  • 我很想将列表加载到数组中,然后将该数组放入会话变量中。然后我会推送当前索引,而不是整个列表
  • 你能举个例子吗?
  • 查询 variables.employee 中还包含什么?

标签: list coldfusion


【解决方案1】:

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

在第一页,更改:

<cfquery name = "employee">

<cfquery name = "session.employee">

然后这样做:

<cfoutput query = "session.employee">
<a href=page2.cfm?row=#rownumber#">somethigng</a>

在 page2.cfm 上执行此操作:

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

【讨论】:

    【解决方案2】:

    当一个人从一个数组开始时,永远不应该使用一个列表(好吧:一个人不应该在可能的情况下使用列表)。考虑到这一点,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>
    

    【讨论】:

      猜你喜欢
      • 2015-08-06
      • 2011-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多