扫描二维码关注官方公众号
返回列表 发布新帖

关于对discuz论坛手机上无法正常显示超宽的excel表格的bug优化方案

394 1
发表于 2024-9-29 11:09:19| 字数 7,242 | 查看全部 阅读模式 来自 浙江杭州
在discuz官方论坛,我也发过本解决方案,这是我的原创链接,很多盗用的,有的还去做了插件,谁买谁上当
我的discuz官方论坛帖子:https://www.dismall.com/thread-23261-1-1.html
我的站点测试链接:看本帖子二楼表格效果

问题展示
image.webp



我修改后的效果
145549pthfaot434xq4zff.jpg.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,在文件底部添加如下代码
【手机端】,找到文件/template/default/touch/common/header.htm,将下面代码复制进这个文件底部,</head>代码之前,即可


  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、好啦,快工具里,清理缓存,刷新看看
145549pthfaot434xq4zff.jpg.jpg

免责声明

信息及内容: 内容仅代表用户个人观点,与本论坛无关。本论坛不保证内容的准确性、完整性或时效性,使用请自行判断和承担风险。

法律责任: 用户对其发布内容负全责,包括但不限于版权、隐私、诽谤等。因使用本论坛内容导致的任何损失,本论坛不承担责任。

版权与隐私: 请勿发布侵犯他人版权或涉及隐私的信息。发现侵权行为,请立即联系我们处理。

第三方链接及广告: 本论坛不对第三方网站或资源的内容负责。用户因访问或使用第三方内容或广告进行的交易和产生的损失,本论坛不承担责任。

用户行为: 用户需遵守论坛规则和法律法规,不得发布违法、侮辱性或不恰当内容。我们有权删除违反规则的内容并采取相应措施。

联系我们: 如有疑问或发现违规行为,请联系管理员:865817711@qq.com

评论1

865817711楼主Lv.9 发表于 2024-9-29 23:57:57| 字数 265 | 查看全部 来自 浙江杭州
这是测试表格,效果看这个,可以在手机上打开查看效果



23562
1531.41
0.064994907
月租金
4500
292.4770817
个人所得税
94.25
0.004000085
税率
0.064994907
18.00038197
房产税
1413.72
0.06
税费/月
292.4770817
270
印花税
11.78
0.000499958
1年税金
3509.724981
2.249809015
土地使用税
11.66
0.000494865
含税/月
4792.477082
2.226890756
343343
0
1年租金
57509.72498
5646


回复

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

本版积分规则

投诉/建议联系

865817711@qq.com

未经授权禁止转载,复制和建立镜像,
如有违反,追究法律责任
  • 关注公众号
  • 添加微信客服
Copyright © 2001-2025 第一论坛网 版权所有 All Rights Reserved. 浙ICP备2023009069号-17|浙公网安备33010602013740号
关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表