Drupal的Views设置中本来就有一个选项是用于去掉重复的,Drupal 6中叫做Distinct:
Defaults: Display only distinct items, without duplicates. Distinct This will make the view display only distinct items. If there are multiple identical items, each will be displayed only once. You can use this to try and remove duplicates from a view, though it does not always work. Note that this can slow queries down, so use it with caution.
而在Drupal 7中这个选项是放在Advanced -> Query settings:Settings,这里面比Drupal 6还多了一个Pure Distinct子选项:
Distinct This will make the view display only distinct items. If there are multiple identical items, each will be displayed only once. You can use this to try and remove duplicates from a view, though it does not always work. Note that this can slow queries down, so use it with caution. Pure Distinct This will prevent views from adding the base column to the distinct field. If this is not selected and the base column is a primary key, then a non-pure distinct will not function properly because the primary key is always unique.
设置的帮助中可以看到,都说到了“it does not always work”,也就是不能100%保证有效。
我们现在的需求是在一个美国名录站点中在只有具体名录数据(每条包含公司名、所属city、county、state、sic行业代码等)的情况下,把county下的city列出来,以及把county下的sic行业代码列出来,因为views的FILTER CRITERIA是Content: Type (= Business Directory),每个公司的名称等信息不一样,虽然可以把Content: County作为CONTEXTUAL FILTERS,在显示的FIELDS中也只留下Content: City,但最后显示出来的列表却是有很多重复的city(因为一个city有很多名录页面),这不符合我们的需求,即使用到Distinct也不行。
这几天在做Drupal 6到Drupal 7的升级设置,在Drupal 7中设置遇到问题,但在Drupal 6中输出结果却是正确的,仔细查看后发现几年前在设置Drupal 6的时候就想办法解决了这个问题,现在才回想起来,于是把办法记录在博客中:
- 使用Unformatted list格式,在Settings中将Grouping field Nr.1设置为Content: City,这样列表中就会显示分组的City链接了;
- 再把FIELDS中的Content: City设置为Exclude from display,也就是不显示每个页面的City信息了;
- 但如果FIELDS中没有任何显示项就会有报错提示:Display "Page" uses fields but there are none defined for it or all are excluded.,这时候可以添加一个Global: Custom text,里面什么内容也没有就可以了。
这个办法从显示上是可以实现需要的效果(例子见:http://ak.mingluji.com/zh-hans/city_of_county/Aleutians%20West ),不过在效率上肯定不高,如果是数据量少的网站没有大的影响,如果是数据量大的站点就可能对服务器造成比较大的负担,要根据实际情况来取舍采用。
评论1
usa站点中的city_of_state/修改的方法:
usa站点中的city_of_state/修改的方法: 1)、使用Unformatted list格式,在Settings中将Grouping field Nr.1设置为(term) Taxonomy term: Name 2)、field中的(term) Taxonomy term: Name选择为Exclude from display 3)、在field中增加Global: Custom text,防止页面报错