(提升)在CSS选择器转换过程中对项目进行分组? [英] (Lift) Grouping items during CSS selector transform?
问题描述
希望获得一些指导.
请考虑以下代码段:
val q = (for {
(d, o) <- dx innerJoin ox on (_.user === _.id)
} yield(d,o))
"div" #> q.map { case (x, y) =>
{
".dF1 *" #> x.name &
".dF2 *" #> y.id
}
}
在此查询中,我有两个表,其中"ox"表是人员列表,"dx"是与这些人员关联的项目的列表. 就本文而言,它很好用,但是我最终为一个人拥有的每一项创建一行.因此,假设有三个用户,前两个有两个项目,最后一个有1,我得到五行:
in this query, I have two tables, where table "ox" is a list of people, and "dx" is a list of items associated with those people. As it is written, it works good, but I end up creating one row for each item that a person has. So assume three users, first two have two items, and last one has 1, i get five rows:
<div class="dF1">[user1]</div><div class="dF2">[item1]</div>
<div class="dF1">[user1]</div><div class="dF2">[item2]</div>
<div class="dF1">[user2]</div><div class="dF2">[item1]</div>
<div class="dF1">[user2[</div><div class="dF2">[item2]</div>
<div class="dF1">[user3]</div><div class="dF2">[item3]</div>
我想做的是为每个用户创建一行,然后在dF2字段内创建多个div,每个项一个.布局将是:
What I'd like to do is create a single row for each user, and inside of the dF2 field create multiple divs, one for each item. The layout would then be:
<div class="dF1">[user1]</div><div class="dF2">[item1] [item2]</div>
<div class="dF1">[user2]</div><div class="dF2">[item1] [item2]</div>
<div class="dF1">[user3]</div><div class="dF2">[item1]</div>
我该怎么做?我需要使用中介馆藏吗?
How can I do this? Do I need to use an intermediary collection?
推荐答案
此处有两个选项,您可以使用数据库返回按字段分组的集合,也可以使用Scala对集合进行分组.如果使用数据库,则只能按字段分组,例如x.name
,但是执行效率可能更高.如果与Scala进行分组,则可以按整个对象x
进行分组,但是最终将需要进行更多的处理和对象创建.您需要确定最适合您的.
You have two options here, you can either use the database to return a collection grouped by a field or you can use Scala to group your collection. If you use the database, you will be limited to grouping by a field, like x.name
, but your execution will likely be more efficient. If you group with Scala you'll be able to group by the entire object x
, but you will end up doing more processing and object creation. You'd need to decide which is best for you.
现在,这样说-假设我们正在使用Scala.您将执行以下操作:
Now, that said - let's assume we're using Scala. You would do something like this:
"div" #> q.groupBy(_._1).map { case (x, y) =>
{
".dF1 *" #> x.name &
".dF2 *" #> y.map{ case (x2, y2) => y2.id}.mkString(" ")
}
}
groupBy
不会直接使用查询中包含D
和O
的List
,而是会创建一个类型为Map[D, List[(D, O)]]
的列表.在上面的示例中,我只是将o
的id
字段组合为一个字符串以输出,就像您请求的示例一样.
Instead of working directly with the List
containing D
and O
from your query, the groupBy
will create a list of type: Map[D, List[(D, O)]]
. In the example above, I am just combining the id
field of your o
into a single string to output which looked like the example you requested.
除了单个字符串,您还可以让第二个映射返回CssSel
进行进一步的转换.此示例将查找嵌套在具有类dF2
的div
中的<div class="dF2SubDiv"></div>
:
Instead of a single string, you could also have your second map return a CssSel
to do further transformations. This example which will look for <div class="dF2SubDiv"></div>
nested in the div
that has the class dF2
:
"div" #> q.groupBy(_._1).map { case (x, y) =>
{
".dF1 *" #> x.name &
".dF2" #> y.map{ case (x2, y2) =>
".dF2SubDiv *" #> y2.id
}
}
}
如果您更愿意使用Slick来进行分组,则可以在这里.
If you would rather use Slick to do the groupBy, you could find more info on it here and here.
这篇关于(提升)在CSS选择器转换过程中对项目进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!