12
返回列表 发帖
楼主: 铁板鱿鱼烧

[已解决] 手机版表格显示不全怎么办?x3.5

6

主题

801

回帖

2121

积分

已臻大成

贡献
140 点
金币
85 个
发表于 2024-8-20 17:44:14 | 查看全部
或许你可以试试我这个方法------https://boonkiong.com/thread-3699-1-1.html
回复

使用道具 举报

3

主题

23

回帖

33

积分

初学乍练

贡献
0 点
金币
5 个
QQ
 楼主| 发表于 2024-8-20 18:42:34 | 查看全部
文強 发表于 2024-8-20 17:44
或许你可以试试我这个方法------https://boonkiong.com/thread-3699-1-1.html

这个方法确实不错,就是如果表格特别宽,全都都挤到这里,也很麻烦看不清,能够水平往右边滑动看到更多,其实效果更好,有没有想法修改下
回复

使用道具 举报

75

主题

477

回帖

710

积分

应用开发者

贡献
15 点
金币
26 个
QQ
发表于 2024-8-20 22:34:08 | 查看全部
在模板目录下 如默认模板template/default/touch/forum/viewthread.htm
文件倒数第二行增加以下代码(<!--{template common/footer}-->之前)




  1. <style>
  2.          
  3.         .message table td {
  4.                 padding: 5px;
  5.                 white-space: nowrap;
  6.                 overflow: hidden;
  7.                 text-overflow: ellipsis;
  8.                 border: 1px solid #ddd;
  9.         }
  10. </style>
  11. <script type="text/javascript">
  12.     function optimizeTables() {
  13.         var tables = document.querySelectorAll('.message table');
  14.         tables.forEach(function(table) {
  15.             // 设置表格样式
  16.             table.style.width = '100%';
  17.             table.style.maxWidth = 'none'; // 移除最大宽度限制
  18.             table.style.tableLayout = 'fixed';
  19.             table.style.overflowX = 'auto';
  20.             table.style.display = 'block';
  21.             
  22.             var cells = table.rows[0].cells;
  23.             var cellCount = cells.length;
  24.             var minCellWidth = 100; // 最小列宽
  25.            
  26.             
  27.             // 设置每列的宽度
  28.             for (var i = 0; i < cellCount; i++) {
  29.                 cells[i].style.minWidth = minCellWidth + 'px';
  30.                 cells[i].style.width = (100 / cellCount) + '%';
  31.             }
  32.             
  33.             // 处理所有单元格
  34.             var allCells = table.getElementsByTagName('td');
  35.             for (var i = 0; i < allCells.length; i++) {
  36.                 allCells[i].style.whiteSpace = 'nowrap';
  37.                 allCells[i].style.overflow = 'hidden';
  38.                 allCells[i].style.textOverflow = 'ellipsis';
  39.             
  40.             }
  41.         });
  42.     }

  43.     // 在页面加载完成和窗口大小改变时调用此函数
  44.     window.addEventListener('load', optimizeTables);
  45.     window.addEventListener('resize', optimizeTables);
  46.     </script>
复制代码
回复

使用道具 举报

6

主题

1062

回帖

1469

积分

应用开发者

贡献
28 点
金币
179 个
QQ
发表于 2024-8-21 08:38:49 | 查看全部
用编辑器自带的试试看,如果不行的话,只能单独对手机版做页面优化。
回复

使用道具 举报

3

主题

23

回帖

33

积分

初学乍练

贡献
0 点
金币
5 个
QQ
 楼主| 发表于 2024-8-23 15:20:58 | 查看全部
感谢各位的回答,我组织了一下,这个问题修改下代码就好了,也希望官方可以把这个代码整合进下一个版本更新!
我的站点测试链接:https://blog.haodaima.cn/forum.php?mod=viewthread&tid=1
截图202408231455498832.jpg

我修改后的代码,不仅可以实现手机上左右滑动,正确显示表格状态,左上角还有一个按钮,支持一键复制表格内容,同时【全选表格】告知用户复制了哪些,弹出提示框:“表格内容已复制”。每次点击,提示框的颜色都不一样,方便区分有时候复制错表格再来复制这个,提醒当前复制的是哪个。


代码如下:

1、打开文件/source/function/function_discuzcode.php

约438行左右,找到表格显示函数,function parsetable,将其内容替换如下:意在修改表格显示样式,其中蓝色部分为修改的部分,可整个函数复制替换,只替换蓝色也行

  1. function parsetable($width, $bgcolor, $message) {
  2.         if(strpos($message, '[/tr]') === FALSE && strpos($message, '[/td]') === FALSE) {
  3.                 $rows = explode("\n", $message);
  4. <font color="#0000ff">                $s = !defined('IN_MOBILE') ? '<div style="overflow-x:auto;"><button class="copy-btn" onclick="copyTable(this)" style="margin-bottom: 10px;">一键复制</button><table cellspacing="0" class="t_table" '.
  5.                     ($width == '' ? NULL : 'style="width:'.$width.'"').
  6.                     ($bgcolor ? ' bgcolor="'.$bgcolor.'">' : '>') : '<div style="overflow-x:auto;"><button class="copy-btn" onclick="copyTable(this)" style="margin-bottom: 10px;">一键复制</button><table cellspacing="0" class="t_table">';</font>
  7.                 foreach($rows as $row) {
  8.                         $s .= '<tr><td>'.str_replace(array('\|', '|', '\n'), array('&#124;', '</td><td>', "\n"), $row).'</td></tr>';
  9.                 }
  10.                 $s .= '</table>';
  11.                 return $s;
  12.         } else {
  13.                 if(!preg_match("/^\[tr(?:=([\(\)\s%,#\w]+))?\]\s*\[td([=\d,%]+)?\]/", $message) && !preg_match("/^<tr[^>]*?>\s*<td[^>]*?>/", $message)) {
  14.                         return str_replace('\"', '"', preg_replace("/\[tr(?:=([\(\)\s%,#\w]+))?\]|\[td([=\d,%]+)?\]|\[\/td\]|\[\/tr\]/", '', $message));
  15.                 }
  16.                 if(substr($width, -1) == '%') {
  17.                         $width = substr($width, 0, -1) <= 98 ? intval($width).'%' : '98%';
  18.                 } else {
  19.                         $width = intval($width);
  20.                         $width = $width ? ($width <= 560 ? $width.'px' : '98%') : '';
  21.                 }
  22.                 $message = preg_replace_callback("/\[tr(?:=([\(\)\s%,#\w]+))?\]\s*\[td(?:=(\d{1,4}%?))?\]/i", 'parsetable_callback_parsetrtd_12', $message);
  23.                 $message = preg_replace_callback("/\[\/td\]\s*\[td(?:=(\d{1,4}%?))?\]/i", 'parsetable_callback_parsetrtd_1', $message);
  24.                 $message = preg_replace_callback("/\[tr(?:=([\(\)\s%,#\w]+))?\]\s*\[td(?:=(\d{1,2}),(\d{1,2})(?:,(\d{1,4}%?))?)?\]/i", 'parsetable_callback_parsetrtd_1234', $message);
  25.                 $message = preg_replace_callback("/\[\/td\]\s*\[td(?:=(\d{1,2}),(\d{1,2})(?:,(\d{1,4}%?))?)?\]/i", 'parsetable_callback_parsetrtd_123', $message);
  26.                 $message = preg_replace("/\[\/td\]\s*\[\/tr\]\s*/i", '</td></tr>', $message);
  27. <font color="#0000ff">                return (!defined('IN_MOBILE') ? '<div style="overflow-x:auto;"><button class="copy-btn" onclick="copyTable(this)" style="margin-bottom: 10px;">一键复制</button><table cellspacing="0" class="t_table" '.
  28.                     ($width == '' ? NULL : 'style="width:'.$width.'"').
  29.                     ($bgcolor ? ' bgcolor="'.$bgcolor.'">' : '>') : '<div style="overflow-x:auto;"><button class="copy-btn" onclick="copyTable(this)" style="margin-bottom: 10px;">一键复制</button><table cellspacing="0" class="t_table">').
  30.                     str_replace('\"', '"', $message).'</table></div>';</font>
  31.         }
  32. }
复制代码
2、修改电脑端样式,使【框线显示更清晰】【增加一键复制按钮】

找到/template/default/common/header_common.htm

在文件底部添加如下代码

  1. <script>
  2. var lastColorSchemeIndex = -1; // 初始化变量,记录上一次使用的颜色索引,初始值为-1表示尚未选择颜色

  3. function copyTable(button) {
  4.     var table = button.nextElementSibling; // 获取按钮下一个兄弟元素,即表格元素
  5.    
  6.     // 定义一个函数,用于选择整个表格内容
  7.     function selectTable() {
  8.         if (window.getSelection) {
  9.             // 创建一个新的范围对象
  10.             var range = document.createRange();
  11.             range.selectNode(table); // 将范围设置为包含整个表格
  12.             window.getSelection().removeAllRanges(); // 清除当前的所有选区
  13.             window.getSelection().addRange(range); // 将新的范围添加到选区中
  14.         } else if (document.selection) {
  15.             // 针对较旧的浏览器(如IE)的选择逻辑
  16.             var range = document.body.createTextRange();
  17.             range.moveToElementText(table); // 将范围设置为表格文本
  18.             range.select(); // 选择范围
  19.         }
  20.     }

  21.     // 首先选择表格内容,以便后续操作
  22.     selectTable();

  23.     var rows = table.rows; // 获取表格的所有行
  24.     var result = ''; // 初始化一个空字符串,用于存储表格数据

  25.     // 遍历表格的每一行
  26.     for (var i = 0; i < rows.length; i++) {
  27.         var cells = rows[i].cells; // 获取当前行的所有单元格
  28.         // 遍历当前行的每一个单元格
  29.         for (var j = 0; j < cells.length; j++) {
  30.             result += cells[j].innerText + (j === cells.length - 1 ? '' : '\t'); // 将单元格内容加入结果字符串,用制表符分隔
  31.         }
  32.         result += '\n'; // 每一行结束后,加入换行符
  33.     }

  34.     // 移除最后一个多余的换行符
  35.     if (result.endsWith('\n')) {
  36.         result = result.slice(0, -1);
  37.     }

  38.     // 将结果复制到剪贴板
  39.     var tempTextArea = document.createElement('textarea'); // 创建一个临时的文本区域元素
  40.     tempTextArea.value = result; // 将表格内容赋值给临时文本区域
  41.     document.body.appendChild(tempTextArea); // 将临时文本区域添加到文档中
  42.     tempTextArea.select(); // 选择临时文本区域的内容
  43.     document.execCommand('copy'); // 执行复制命令
  44.     document.body.removeChild(tempTextArea); // 复制完成后,移除临时文本区域
  45.    
  46.     // 再次选择表格,确保它在复制后被全选
  47.     selectTable();
  48.    
  49.     // 定义三种提示框颜色方案,包含背景色和文字颜色
  50.     var colorSchemes = [
  51.         { background: '#4CAF50', color: 'white' }, // 绿色背景,白色文字
  52.         { background: '#FF9800', color: 'black' }, // 橙色背景,黑色文字
  53.         { background: '#2196F3', color: 'white' }  // 蓝色背景,白色文字
  54.     ];
  55.    
  56.     // 确保新选择的颜色方案与上一次不同
  57.     var newColorSchemeIndex;
  58.     do {
  59.         newColorSchemeIndex = Math.floor(Math.random() * colorSchemes.length); // 随机选择一个颜色索引
  60.     } while (newColorSchemeIndex === lastColorSchemeIndex); // 如果新颜色与上一次相同,则重新选择

  61.     lastColorSchemeIndex = newColorSchemeIndex; // 更新记录的颜色索引为当前选择的颜色
  62.     var selectedColorScheme = colorSchemes[newColorSchemeIndex]; // 获取选中的颜色方案

  63.     // 创建一个提示框元素,并设置其样式和文本内容
  64.     var alertBox = document.createElement('div');
  65.     alertBox.className = 'copy-alert'; // 设置提示框的CSS类
  66.     alertBox.textContent = "表格内容已复制!"; // 设置提示框显示的文本
  67.     alertBox.style.backgroundColor = selectedColorScheme.background; // 应用随机选择的背景颜色
  68.     alertBox.style.color = selectedColorScheme.color; // 应用随机选择的文字颜色
  69.     document.body.appendChild(alertBox); // 将提示框添加到文档中

  70.     // 让提示框显示
  71.     alertBox.style.display = 'block';

  72.     // 1秒后自动隐藏提示框并移除它
  73.     setTimeout(function() {
  74.         alertBox.style.display = 'none'; // 隐藏提示框
  75.         document.body.removeChild(alertBox); // 从文档中移除提示框
  76.     }, 1000);
  77. }

  78. </script>

  79. <!--按钮样式-->
  80. <style>
  81. .copy-btn {
  82.     background: linear-gradient(45deg, #ff6b6b, #f94d6a); /* 渐变背景 */
  83.     border: none; /* 移除边框 */
  84.     color: white; /* 白色文本 */
  85.     padding: 10px 20px; /* 按钮内边距 */
  86.     text-align: center; /* 文字居中 */
  87.     text-decoration: none; /* 移除下划线 */
  88.     display: inline-block; /* 使按钮在一行内 */
  89.     font-size: 14px; /* 字体大小 */
  90.     margin: 4px 2px; /* 外边距 */
  91.     cursor: pointer; /* 鼠标指针样式 */
  92.     border-radius: 8px; /* 圆角 */
  93.     transition: background 0.3s, transform 0.1s; /* 背景色和缩放效果渐变 */
  94.     box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* 添加阴影 */
  95. }

  96. .copy-btn:hover {
  97.     background: linear-gradient(45deg, #f94d6a, #ff6b6b); /* 悬停时渐变背景 */
  98. }

  99. .copy-btn:active {
  100.     transform: scale(0.95); /* 点击时的缩放效果 */
  101.     box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); /* 点击时调整阴影 */
  102. }

  103. .copy-alert {
  104.     position: fixed;
  105.     top: 50%; /* 垂直居中 */
  106.     left: 50%; /* 水平居中 */
  107.     transform: translate(-50%, -50%); /* 将元素中心点移到正确的位置 */
  108.     background-color: #4CAF50; /* 绿色背景 */
  109.     color: white; /* 白色文本 */
  110.     padding: 10px 20px; /* 内边距 */
  111.     border-radius: 5px; /* 圆角 */
  112.     box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* 添加阴影 */
  113.     z-index: 1000; /* 确保在最前面 */
  114.     font-size: 14px;
  115.     display: none; /* 初始状态下隐藏 */
  116. }
  117. </style>

  118. <!--表格显示框线,表格单行显示-->
  119. <style>
  120. .t_table {
  121.     width: 100%; /* 设置表格的宽度为父容器的 100%,即表格会自动适应父容器的宽度 */
  122.     min-width: 600px; /* 设置表格的最小宽度为 600 像素,确保内容即使在父容器宽度较小时也能完整显示 */
  123.     table-layout: auto; /* 设置表格布局为自动,即表格列的宽度会根据内容自动调整 */
  124. }
  125. .t_table td {
  126.     padding: 4px; /* 设置单元格内容与单元格边框之间的内边距为 4 像素 */
  127.     line-height: 1.1; /* 设置单元格内容的行高为 1.1 倍的字体大小,确保内容间距适中 */
  128.     border: 1px solid #afbeca; /* 设置单元格的边框为 1 像素实线,颜色为 #afbeca(浅灰色) */
  129.     overflow: hidden; /* 当单元格内容过多时,超出部分会被隐藏,防止溢出影响布局 */
  130. }  
  131. </style>
复制代码
3、修改手机端的,找到文件/template/default/touch/common/header.htm

将【2】的代码复制进这个文件底部,</head>代码之前,即可



回复

使用道具 举报

324

主题

1352

回帖

1862

积分

已臻大成

贡献
12 点
金币
8 个
发表于 2025-1-20 11:37:41 来自手机 | 查看全部
好用,感谢
回复

使用道具 举报

324

主题

1352

回帖

1862

积分

已臻大成

贡献
12 点
金币
8 个
发表于 2025-1-20 11:57:44 | 查看全部
1、打开文件/source/function/function_discuzcode.php   
下面的代码好像不对 x3.5默认模板
IMG_20250120_115133.jpg


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 关注公众号
  • 有偿服务微信
  • 有偿服务QQ

手机版|小黑屋|Discuz! 官方交流社区 ( 皖ICP备16010102号 |皖公网安备34010302002376号 )|网站地图|star

GMT+8, 2025-5-1 03:16 , Processed in 0.071390 second(s), 31 queries .

Powered by Discuz! W1.0 Licensed

Cpoyright © 2001-2025 Discuz! Team.

关灯 在本版发帖
有偿服务QQ
有偿服务微信
返回顶部
快速回复 返回顶部 返回列表