简体   繁体   English

将对象数组显示为表格

[英]Display array of objects as a table

I have an array of Candle objects as shown below:我有一个Candle对象数组,如下所示:

在此处输入图像描述

Is there a way in Pharo to display the array in a tabular format? Pharo 有没有办法以表格格式显示数组?

|       date | open | high | low | close |
|------------+------+------+-----+-------|
| 2018-12-28 |   10 |   20 |  30 |    40 |
| 2019-12-28 |   50 |   60 |  70 |    80 |

gtoolkit工具包

The gtoolkit tour has a slide that seems to mention this: gtoolkit 之旅有一张幻灯片似乎提到了这一点:

在此处输入图像描述

However, it seems that I'd have to define a special collection for holding Candle objects and then define a gtViewCandlesOn method on it to customize how the candles will be rendered.但是,似乎我必须定义一个特殊的集合来保存Candle对象,然后在其上定义一个gtViewCandlesOn方法来自定义蜡烛的渲染方式。

Just wondering if there's an approach that will work with normal arrays or if the gtoolkit approach is the way to go.只是想知道是否有一种方法可以与普通的 arrays 一起使用,或者 gtoolkit 方法是否是通往 go 的方法。

It depends on context of use.这取决于使用环境。 You can use GToolkit , if you use it on other activities as well.如果您也将它用于其他活动,您可以使用GToolkit GToolkit is whole framework and probably it is quite overkill to use it just for this tabular view. GToolkit是一个完整的框架,仅将它用于此表格视图可能有点过头了。

Anyway, I did something similar for Pharo launcher command line interface - calling it ConsoleListFormatter .无论如何,我为 Pharo 启动器命令行界面做了类似的事情——称之为ConsoleListFormatter It is pretty straightforward to use, you need to define 2 methods on your candle class ( #listPrintAttributeBlocks: #listPrintAttributeLabels: ) and set collection of Candles to formatter instance.它使用起来非常简单,您需要在蜡烛 class ( #listPrintAttributeBlocks: #listPrintAttributeLabels: ) 上定义 2 个方法并将蜡烛集合设置为格式化程序实例。 Calling printList will do the job.调用 printList 将完成这项工作。

You can use it here ConsoleListFormatter class你可以在这里使用ConsoleListFormatter class

Furthermore, if you want to print as collection in the inspector, you would need to override original implementation of #printOn: (or gtViewCandlesOn: in case of use GToolkit inspector ) method on Collection class.此外,如果要在检查器中打印为集合,则需要覆盖集合 class 上#printOn: (或gtViewCandlesOn:在使用GToolkit 检查器的情况下)方法的原始实现。 Rather I would define model class, eg CandleList that would use instance var with collection of candles, then you can define your own #printOn: on CandleList without interfering original collection implementation.相反,我会定义 model CandleList ,例如将使用实例 var 和蜡烛集合的 CandleList,然后您可以在CandleList上定义自己的#printOn:而不干扰原始集合实现。

Let me know, if you need more details如果您需要更多详细信息,请告诉我

Cheers, David干杯,大卫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM