在PHP中有没有像样的HTML表格生成器类? [英] Is there a decent HTML-table-generator class in PHP?

查看:164
本文介绍了在PHP中有没有像样的HTML表格生成器类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个体面的PHP类,它将生成复杂的HTML表格,也就是说,它应该支持colspan / rowspan以及用于行,列和单元格的单个CSS类。

解决方案

看来您的问题与我正在处理的一个小项目密切相关。

我刚更新带有 2.x 版本的 naomik / htmlgen

 使用函数htmlgen \html作为h; 
使用函数htmlgen \ map;

$ beeData = [
'pop'=> 'yup',
'candy'=> '有时',
'花'=> '这么多',
'water'=> '不是真的',
'sand'=> '无动于衷',
'donuts'=> '绝对'
];
$ b $ h('''','b'('''','item'),$ b $('''',
h bh('td','bees like it?')

),
h('tbody',
map($ beeData,function($ value,$ key ){返回
h('tr',
h('td',$ key),
h('td',$ value)
);
})

);

输出(实际输出中未包含空格)

 <表> 
< thead>
< tr>
< td>项目< / td>
< td>做蜜蜂喜欢它吗?< / td>
< / tr>
< / thead>
< tbody>
< tr>
< td> pop< / td>
< td> yup< / td>
< / tr>
< tr>
< td>糖果< / td>
< td>有时< / td>
< / tr>
< tr>
< td>花< / td>
< td>这么多< / td>
< / tr>
< tr>
< td>水< / td>
< td>不是真的< / td>
< / tr>
< tr>
< td>沙< / td>
< td>无所谓< / td>
< / tr>
< tr>
< td>甜甜圈< / td>
< td>绝对是< / td>
< / tr>
< / tbody>
< / table>

尽管有些例子看起来可能有点冗长,但我认为能够利用DSL以非常程序化的方式使它非常强大。



如果您真的有兴趣创建自己的图书馆,我很乐意与您合作。请检查该项目,并让我知道您的想法:)



PS 请参阅 example 目录以获得更多乐趣!


I'm looking for a decent class in PHP which will generate complex HTML tables, i.e. it should support colspan/rowspan and individual CSS classes for rows, columns and cells.

解决方案

It seems like your question is pretty closely related to a little project I'm working on.

I just updated naomik/htmlgen with a 2.x release.

use function htmlgen\html as h;
use function htmlgen\map;

$beeData = [
  'pop' => 'yup',
  'candy' => 'sometimes',
  'flowers' => 'so much',
  'water' => 'not really',
  'sand' => 'indifferent',
  'donuts' => 'most definitely'
];

echo h('table',
  h('thead',
    h('tr',
      h('td', 'item'),
      h('td', 'do bees like it?')
    )
  ),
  h('tbody',
    map($beeData, function($value, $key) { return
      h('tr',
        h('td', $key),
        h('td', $value)
      );
    })
  )
);

Output (whitespace not included in actual output)

<table>
  <thead>
    <tr>
      <td>item</td>
      <td>do bees like it?</td>
    </tr>
  </thead>
  <tbody>
   <tr>
     <td>pop</td>
     <td>yup</td>
   </tr>
   <tr>
     <td>candy</td>
     <td>sometimes</td>
   </tr>
   <tr>
     <td>flowers</td>
     <td>so much</td>
   </tr>
   <tr>
     <td>water</td>
     <td>not really</td>
   </tr>
   <tr>
     <td>sand</td>
     <td>indifferent</td>
   </tr>
   <tr>
     <td>donuts</td>
     <td>most definitely</td>
   </tr>
 </tbody>
</table>

Even though some of the examples seem like they might be somewhat verbose, I think the ability to utilize the DSL in a very programmatic way makes it very powerful.

If you are actually interested in creating your own library, I'd love to collaborate with you. Please check out the project and let me know what you think :)

PS See other code in the example directory for more fun !

这篇关于在PHP中有没有像样的HTML表格生成器类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆