ColdFusion 数组和结构

发布于 2024-11-30 11:20:44 字数 485 浏览 1 评论 0原文

我在会话中存储了一个购物车数组:购物车数组的屏幕截图

我让用户能够选择所有用于删除或通过复选框选择单个项目的项目。

我通过表单发布将数组索引发送到arrayDeleteAt

现在,如果我选择底部 3 项,它不会删除它。

这是我的删除代码:

<cfif isDefined("form.leadId") AND  listLen(form.leadId)>
  <cfloop from="#listLen(form.leadId)#" to="1" step="-1" index="i">
    <cfset temp = arrayDeleteAt(session.shoppingcart, #i#)>
  </cfloop> 
</cfif>

I have a shopping cart array stored in a session:Screen Shot of Shopping Cart Array

I give my user the ability to select all items to delete or select individual items via a checkbox.

I am sending the array index via form post to arrayDeleteAt.

Now if I select the bottom 3 items, it does not delete it.

Here is my delete code:

<cfif isDefined("form.leadId") AND  listLen(form.leadId)>
  <cfloop from="#listLen(form.leadId)#" to="1" step="-1" index="i">
    <cfset temp = arrayDeleteAt(session.shoppingcart, #i#)>
  </cfloop> 
</cfif>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风不识月 2024-12-07 11:20:44

使用这种管理购物车的方法将会遇到更多问题。使用 ArrayDeleteAt 后,数组的索引将被重新计算,因此当您很可能从数组中删除错误的项目时,或者当您尝试删除超出范围的项目时可能会收到错误。

看到你正试图通过向后处理你的列表来解决这个问题,丹对于上面代码的问题是正确的,但是如果列表以错误的顺序传递,那么你就会陷入一个受伤的世界。

我建议不要使用数组,而是使用带有代理键(例如 UUID)的结构,然后通过该键删除项目。

You are going to have more issues with this method of managing your cart. After using ArrayDeleteAt the array's indexes will be recalculated, so when you will most likely delete the wrong item from the array, or you can get an error when you try and delete an item that is out of bounds.

Is see that you are trying to get around this by working backwards through your list, and Dan is right for what your issue is with the code above, but if the list is passed in in an incorrect order then you are in a world of hurt.

I would suggest instead of using an array, use a struct with a surrogate key, such as a UUID, then delete items by that key.

子栖 2024-12-07 11:20:44

问题是您要删除计数器的位置,而不是传入的表单字段。请尝试以下操作:

<cfset temp = arrayDeleteAt(session.shoppingcart, ListGetAt(FORM.leadID, i) />

更新:要解决泰勒提到的问题,您可以将索引列表从 转换使用 ListToArray 将 FORM.leadID 转换为数组,然后使用 ArraySort 按必要的顺序获取它们,以确保删除正确。

虽然我的回答确实解决了您眼前的问题,但您最好遵循泰勒的建议,并为购物车中的每件商品使用一把钥匙,以确保您正在管理您实际上认为应该管理的商品:)

The problem is that you're deleting at the position of the counter, and not the form field that's being passed in. Try this instead:

<cfset temp = arrayDeleteAt(session.shoppingcart, ListGetAt(FORM.leadID, i) />

UPDATE: To get around the issue Tyler mentioned, you can convert your list of indexes from FORM.leadID to an Array using ListToArray and then ArraySort to get them in the order necessary to make sure your deletes are correct.

While my answer does fix your immediate problem, you would definitely be better off following Tyler's advice and using a key for each item in the cart to make sure you're managing the one you actually think you're supposed to be managing :)

独自唱情﹋歌 2024-12-07 11:20:44

在 CF 10 或 Railo 4 中,可以使用 最新版本 来完成此操作a href="http://russplaysguitar.github.com/UnderscoreCF/#reject" rel="nofollow">Underscore.cfc 库:

<cfscript>
if (structKeyExists(form, 'leadId') && listlen(form.leadId)) {
    _ = new Underscore();

    variables.shoppingCart = duplicate(session.shoppingCart);

    variables.newCart = _.reject(variables.shoppingCart, function(val, index){
        return _.include(form.leadId, index);
    });
}
</cfscript>

<cfif structKeyExists(variables, "newCart") >
    <cflock scope="session" type="exclusive" timeout="10">
        <cfset session.shoppingCart = variables.newCart>
    </cflock>
</cfif>

你会看到我将购物车复制到变量范围中,对其进行编辑,然后在必要时将其复制回来(带锁)。如果可以在 cfscript 中编写 cflock,我会在 cfscript 中完成这一切。

(免责声明:我写了Underscore.cfc)

In CF 10 or Railo 4, this can be done using the most recent version of the Underscore.cfc library:

<cfscript>
if (structKeyExists(form, 'leadId') && listlen(form.leadId)) {
    _ = new Underscore();

    variables.shoppingCart = duplicate(session.shoppingCart);

    variables.newCart = _.reject(variables.shoppingCart, function(val, index){
        return _.include(form.leadId, index);
    });
}
</cfscript>

<cfif structKeyExists(variables, "newCart") >
    <cflock scope="session" type="exclusive" timeout="10">
        <cfset session.shoppingCart = variables.newCart>
    </cflock>
</cfif>

You'll see that I'm copying the shopping cart into the variables scope, editing it, then copying it back (with a lock) if necessary. I would've done this all in cfscript if it were possible to write cflocks in cfscript.

(Disclaimer: I wrote Underscore.cfc)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文