iReport 变量增量器与列表组件的使用
我有一个典型的 MySQL 报告,每页列出一个“项目”。 min_price 是其中之一。我想让我的详细信息范围列出每个项目的固定数字(比方说 15)“出价金额”,根据此起始价格计算,加上作为报告参数传入的“滑动比例”,如下所示
<parameter name="bidIncrementMap" class="java.util.TreeMap">
<defaultValueExpression><![CDATA[{10,1},{25,2},{50,5},{100,10},{250,20},{500,25},{9999,50}"]]></defaultValueExpression>
</parameter>
:目的,我已经有一个名为 Alphabet 的表,其中有 26 行 id=1..26,letter=A..Z - 这使得一个方便的表可以获取 15 个整数的简单列表。
因此,要获取与给定数字相对应的出价增量值,我认为应该是:
$P{bidIncrementMap}.floorEntry($F{each_bid}).getValue()
...但我想做的是使用每行的出价增量来获取下一行的值。
另外,出于打包原因,我应该提到,我需要将其作为独立的 .jrxml 编译为 .jasper,而类路径上没有任何新的自定义类。
我觉得我真的很接近..有人可以帮助我解决这里缺失的部分吗? (仅供参考,我是 Java 和 SQL 专家,但 Jasper 新手,喜欢我到目前为止所看到的......)我认为我需要以某种方式声明一个变量并使用此映射每次递增它,然后以某种方式获取列表组件来显示我的序列 - 我更喜欢将所有内容都放在一个报告中,因为我看不到重用这样的子报告,但无论哪种方式的示例都很棒。
我用 @ 变量和复杂的 if 语句搞乱了 SQL 表达式 - 这不是我问题的重点,但我将它包含在内是为了说明我在这里想要实现的目标:
mysql> select a.id, i.name, i.min_price, @b:=if(a.id=1,i.min_price,@b+if(@b<10,1,if(@b<25,2,if(@b<50,5,if(@b<100,10,if(@b<250,20,if
@b<500,25,50))))))) bid from items i, alphabet a where a.id<=15 and i.id=27 order by a.id;
+----+---------------+-----------+--------+
| id | name | min_price | bid |
+----+---------------+-----------+--------+
| 1 | My Item Name | 40.00 | 40.00 |
| 2 | My Item Name | 40.00 | 45.00 |
| 3 | My Item Name | 40.00 | 50.00 |
| 4 | My Item Name | 40.00 | 60.00 |
| 5 | My Item Name | 40.00 | 70.00 |
| 6 | My Item Name | 40.00 | 80.00 |
| 7 | My Item Name | 40.00 | 90.00 |
| 8 | My Item Name | 40.00 | 100.00 |
| 9 | My Item Name | 40.00 | 120.00 |
| 10 | My Item Name | 40.00 | 140.00 |
| 11 | My Item Name | 40.00 | 160.00 |
| 12 | My Item Name | 40.00 | 180.00 |
| 13 | My Item Name | 40.00 | 200.00 |
| 14 | My Item Name | 40.00 | 220.00 |
| 15 | My Item Name | 40.00 | 240.00 |
+----+---------------+-----------+--------+
15 rows in set (0.00 sec)
I have a typical MySQL report listing one 'Item' per page. One of the fields is min_price. I would like to have my detail band list a fixed number (lets say 15) 'bid amounts' for each item, computed based on this starting price, plus a 'sliding scale' passed in as a report parameter as follows:
<parameter name="bidIncrementMap" class="java.util.TreeMap">
<defaultValueExpression><![CDATA[{10,1},{25,2},{50,5},{100,10},{250,20},{500,25},{9999,50}"]]></defaultValueExpression>
</parameter>
For a separate purpose, I already have a table called alphabet with 26 rows id=1..26, letter=A..Z - this makes a convenient table to get a simple list of 15 integers.
So to get the bid increment value corresponding to a given number, I think it would be:
$P{bidIncrementMap}.floorEntry($F{each_bid}).getValue()
... but what I am trying to do is use the bid increment for each row to get the next row's value.
Also I should mention for packaging reasons, I need this to be a self-contained .jrxml that compiles to .jasper without any new custom classes on the classpath.
I feel like I'm real close.. can someone help me with the missing piece here? (FYI, I'm a Java and SQL expert but a Jasper newbie, liking what I see so far...) I think I need to somehow declare a variable and use this map to increment it each time, then somehow get the list component to show my sequence - I'd prefer to have it all one report since I can't see reusing such a subreport but an example of either way would be awesome.
I messed around a bit with a SQL expression using an @ variable and a complex if statement - This is NOT the focus of my question, but I include it to illustrate what I'm trying to achieve here:
mysql> select a.id, i.name, i.min_price, @b:=if(a.id=1,i.min_price,@b+if(@b<10,1,if(@b<25,2,if(@b<50,5,if(@b<100,10,if(@b<250,20,if
@b<500,25,50))))))) bid from items i, alphabet a where a.id<=15 and i.id=27 order by a.id;
+----+---------------+-----------+--------+
| id | name | min_price | bid |
+----+---------------+-----------+--------+
| 1 | My Item Name | 40.00 | 40.00 |
| 2 | My Item Name | 40.00 | 45.00 |
| 3 | My Item Name | 40.00 | 50.00 |
| 4 | My Item Name | 40.00 | 60.00 |
| 5 | My Item Name | 40.00 | 70.00 |
| 6 | My Item Name | 40.00 | 80.00 |
| 7 | My Item Name | 40.00 | 90.00 |
| 8 | My Item Name | 40.00 | 100.00 |
| 9 | My Item Name | 40.00 | 120.00 |
| 10 | My Item Name | 40.00 | 140.00 |
| 11 | My Item Name | 40.00 | 160.00 |
| 12 | My Item Name | 40.00 | 180.00 |
| 13 | My Item Name | 40.00 | 200.00 |
| 14 | My Item Name | 40.00 | 220.00 |
| 15 | My Item Name | 40.00 | 240.00 |
+----+---------------+-----------+--------+
15 rows in set (0.00 sec)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我想出了一个解决方案 - 可能会帮助其他人:
而不是尝试初始化 bidIncrementMap 的默认值(这是我一直在努力解决的部分 - 我认为在 Java 中没有静态块是不可能的,这里不允许) ,我赌了一把,决定改用SQL。我设置了一个包含出价序列的表,如下所示:
在插入语句中,项目表可以是带有 > 的任何方便的表。 (任意上限)1000 行。
然后,对于我的子报告,我有一个更简单的查询:(
额外的联合是因为起始值可能位于序列中的元素之间,所以我不是向上或向下舍入,而是将其添加到前面,然后联合可以防止重复。)
所以唯一的缺点是我的序列有一个固定的上限,所以我只能处理最高 48K 美元左右的价格范围 - 对我来说,这很好。
OK, I figured out a solution - might help someone else:
Rather than try initialize the default value for that bidIncrementMap (that was the part I was struggling with - I think it's not possible in Java without static block which isn't allowed here), I punted and decided to use SQL instead. I setup a table with the bid sequence as follows:
In the insert statement, the items table could be any handy table with > (arbitrary upper limit) 1000 rows.
Then, for my subreport I have this much simpler query:
(The extra union is because the starting value might be between elements in the sequence, so rather than round up or down, I just prepend it and then the union prevents a duplicate.)
So the only downside is that I have a fixed upper limit on my sequence, so I can only handle price ranges up to $48K or so - for me, that's fine.