ColdFusion 数组和结构
我在会话中存储了一个购物车数组:
我让用户能够选择所有用于删除或通过复选框选择单个项目的项目。
我通过表单发布将数组索引发送到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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用这种管理购物车的方法将会遇到更多问题。使用 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.
问题是您要删除计数器的位置,而不是传入的表单字段。请尝试以下操作:
更新:要解决泰勒提到的问题,您可以将索引列表从
转换使用
转换为数组,然后使用ListToArray
将 FORM.leadIDArraySort
按必要的顺序获取它们,以确保删除正确。虽然我的回答确实解决了您眼前的问题,但您最好遵循泰勒的建议,并为购物车中的每件商品使用一把钥匙,以确保您正在管理您实际上认为应该管理的商品:)
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:
UPDATE: To get around the issue Tyler mentioned, you can convert your list of indexes from
FORM.leadID
to an Array usingListToArray
and thenArraySort
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 :)
在 CF 10 或 Railo 4 中,可以使用 最新版本 来完成此操作a href="http://russplaysguitar.github.com/UnderscoreCF/#reject" rel="nofollow">Underscore.cfc 库:
你会看到我将购物车复制到变量范围中,对其进行编辑,然后在必要时将其复制回来(带锁)。如果可以在 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:
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)