使 html 表格列可选择排序
我有一个 html 表,它由文本文件填充,格式化并用分号分隔。 我希望用户在单击列名称(标题)时可以选择按任意列的字母顺序排序。
我如何在 php 中做到这一点?或者还有其他方法可以做到这一点吗?感谢您的帮助。
示例原始数据/输入如下所示:
TYPE=abc;PART=georgetown;FILE=goog_abc.dat.2010122211.gz
TYPE=xyz;PART=ucny;FILE=aol_xyz.dat.2010122209.gz
表的 PHP 代码:
$lines = preg_split('~\s*[\r\n]+\s*~', file_get_contents('/temp/tab.txt'));
foreach($lines as $i => $line) {
$pairs = explode(';', $line);
foreach($pairs as $pair) {
list($column, $value) = explode('=', $pair, 2);
$columns[$column] = true;
$rows[$i][$column] = $value;
}
}
$columns = array_keys($columns);
echo '<table><thead><tr>';
foreach($columns as $column) {
echo '<th>'.$column.'</th>';
}
echo '</tr></thead><tbody>';
foreach ($rows as $row) {
echo '<tr>';
foreach($columns as $column){
echo '<td>'.$row[$column].'</td>';
}
echo '</tr>';
}
echo '</tbody></table>';
I have an html table that is populated from a text file, formatted and semi colon separated.
I would like the user to have the option of sorting alphabetically with either of the columns when clicking on the column name (header).
How do I do this in php?? Or is there another way of doing this? Thanks for your help.
Sample raw data/input looks like this:
TYPE=abc;PART=georgetown;FILE=goog_abc.dat.2010122211.gz
TYPE=xyz;PART=ucny;FILE=aol_xyz.dat.2010122209.gz
Php code for table:
$lines = preg_split('~\s*[\r\n]+\s*~', file_get_contents('/temp/tab.txt'));
foreach($lines as $i => $line) {
$pairs = explode(';', $line);
foreach($pairs as $pair) {
list($column, $value) = explode('=', $pair, 2);
$columns[$column] = true;
$rows[$i][$column] = $value;
}
}
$columns = array_keys($columns);
echo '<table><thead><tr>';
foreach($columns as $column) {
echo '<th>'.$column.'</th>';
}
echo '</tr></thead><tbody>';
foreach ($rows as $row) {
echo '<tr>';
foreach($columns as $column){
echo '<td>'.$row[$column].'</td>';
}
echo '</tr>';
}
echo '</tbody></table>';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用一些 jQuery。事实上,这看起来正是您所需要的。
编辑 将其放在
标记之间
或者您可以将其放入单独的文件中并像这样链接它。 (可能只有当您打算编写更多 JavaScript 时才这样做。
I'd recommend using some jQuery. In fact, this looks like exactly what you need.
Edit Put this in between your
<head>
tagsOr you can put it in a separate file and link it like this. (Probably only do this if you intend to write more javascript.
绘制表格后,使用基于 javascript 的内容对表格进行排序。尝试从 PHP 内部添加这种排序是痛苦的,并且不需要,除非您有多个数据页(然后您需要 PHP 在数据库端对其进行排序)。下面的列表为您提供了很多可排序表的选项 - 在使用 PHP 生成表后,您应该能够将其中的许多选项应用到您的表中。
http://www.webdesignbooth.com/15-伟大的jquery-plugins-for-better-table-manipulation/
Use something javascript based to sort the table after you draw it. Trying to add this kind of sorting from within PHP is painful and not needed unless you have multiple pages of data (then you need PHP to sort it in the database side). Here is a list that gives you a LOT of options for sortable tables - you should be able to apply many of those to your table after you generate it using PHP.
http://www.webdesignbooth.com/15-great-jquery-plugins-for-better-table-manipulation/