Drupal 6、CCK 和 CCK 中的循环排序意见
今天,我收到了一位客户的有趣请求,但我不确定使用 Drupal Views 解决该问题的最佳方法。他们目前有一个代表其员工的“团队成员”内容类型。每个工作人员页面都有一个指向所有其他工作人员页面的链接。到目前为止这不是问题。
他们希望这个列表从我们当前正在查看的人开始,然后继续按“排序”顺序排在他们之后。然后,当它到达列表的末尾时,它会环绕并从头开始,直到它们回到自身(类似于循环链表的工作方式)。
例如,假设我有团队成员 A、B、C、D、E、F 和 G:
- 如果我正在查看团队成员 A,则列表顺序为:A、B、C、D、E、F、 G.
- 如果我查看团队成员 D,则列表顺序为:D、E、F、G、A、B、 C.
- 如果我查看团队成员 G,则列表顺序为:G、A、B、C、D、E, F.
如果我在常规编程环境中处理其他一些数据结构,我确信我可以想出某种算法来确定我们目前所处的位置,并从那里开始排序并伪造一个“循环列表”。视图似乎有点棘手。
我能想到的唯一可能的解决方案是创建两个单独的视图,然后在事后将结果连接在一起。
- 一个视图是排序编号大于或等于当前团队成员的所有团队成员的排序列表。
- 第二个视图是排序编号小于或等于当前团队成员的所有团队成员的排序列表。
我只是好奇是否有更有效的方法来对视图进行这种排序?
I had an interesting request from a client today and I'm not exactly sure the best way to solve it using Drupal Views. They currently have a "Team Member" content type that represents members of their staff. Each staff member page has a link to all other staff member pages. So far that's not a problem.
They want this list to start with the person we're presently looking at and then proceed with who falls right after them in the "sorted" order. Then when it reaches the end of the list it wraps around and starts back at the beginning until they get back to themselves (similar to how a Circular Linked List would work).
So for example, assume I have Team Members A,B,C,D,E,F and G:
- If I'm looking at Team Member A, the list order is: A, B, C, D, E, F,
G. - If I'm looking at Team Member D, the list order is: D, E, F, G, A, B,
C. - If I'm looking at Team Member G, the list order is: G, A, B, C, D, E,
F.
If I was dealing with some other data structure in a regular programming environment, I'm sure I could come up with some sort of algorithm to determine where we presently at and begin sorting from there and fake a "circular list". Views seems a little more tricky.
The only potential solution I can think of would be to create two separate views and then just join the results together after the fact.
- One view would be a sorted list of all team members that have a sort number greater than or equal to the present team member.
- A second view that is a sorted list of all team members that have a sort number less than or equal to the present team member.
I'm just curious if there are any more efficient ways of doing this kind of a sort with views?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要您的所有结果都可以包含在一页上,您可能最好使用您提到的解决方案:操作两个视图(可能在模板中,如奥斯瓦尔德建议的那样),以便一个在另一个之后显示。但是,如果您最终确实有很多结果,并且需要分页...您可能需要做一些花哨的工作来计算排序数字之间的差异,并在 ORDER BY 中使用该值...所有这些都需要困难重写主视图查询的任务。
几年前我尝试过这一点,但遇到了巨大的困难...Merlin of Chaos,主要的 Views 开发人员,在某处简单地指出,Views 确实不适合花哨的编程,而且它处理查询的方式不允许如果进行过多的操纵……事情就会崩溃。然而,与此同时,有些事情发生了变化……我记得在视图中看到了一个新函数,它似乎是为了重写主查询而创建的。
以下是有关重写视图查询的教程:http://blog. raiseeyebrow.com/2010/04/rewrite-a-views-query/
请注意:由于视图中发生了很多事情,您应该创建大量虚拟条目以确保没有任何损坏 -如果重写查询,它将更容易出错。
实际上,如果您熟悉 Drupal 模块构建,对于类似的事情,构建一个自定义模块可能更有意义,该模块允许您更好地控制查询和显示。这实际上可能比大量实验和测试更快。
As long as all your results can be contained on one page, you're probably best off with the solution you mention: manipulating two views (probably in the template, as Oswald suggests) so one is displayed after the other. However, if you do end up having lots of results, and needing paging ... you will probably need to do some fancy work in calculating the difference between sort numbers, and using this value in ORDER BY ... all that requiring the difficult task of rewriting the main Views query.
I tried this a few years ago and had enormous difficulties ... Merlin of Chaos, the main Views developer, simply stated somewhere that Views really wasn't meant to be for fancy programming, and that the way it handled queries wouldn't allow for much manipulation ... things would break. However, something has changed in the meantime ... I remember seeing a new function in Views which seemed to be made for the purpose of rewriting the main query.
Here is a tutorial on rewriting a views query: http://blog.raisedeyebrow.com/2010/04/rewrite-a-views-query/
Do note: due to the many things that are going on in Views, you should create a lot of dummy entries to make sure nothing is broken - if you re-write the query, it will be much more susceptible to error.
Actually, if you are acquainted with Drupal module building, for something like this it might just make more sense to build a custom module that allows you more control over the query and the display. This might actually be faster than a lot of experimentation and testing.
我的选择是主题层。我想这可以在预处理函数中完成。也许您必须覆盖模板文件。主题开发模块可以告诉您视图是如何呈现的以及可以覆盖哪些内容。
My choice would be the theming layer. I guess this can be done in a preprocess function. Maybe you have to override a template file. The Theme Developer module can tell you how the view is rendered and which overrides are possible.