要实现根据输入次数自动换行的表格,可以使用Canvas的绘制方法来实现。以下是一个示例代码:

// 在wxml文件中添加一个canvas组件
<canvas canvas-id="myCanvas" style="width: 100%; height: 100%;" />

// 在js文件中获取canvas组件
const ctx = wx.createCanvasContext('myCanvas');

// 在获取到输入次数后,根据次数计算所需的行数和列数
const inputCount = 10; // 假设输入次数为10
const rowCount = Math.ceil(inputCount / 5); // 每行显示5个输入,向上取整得到行数
const columnCount = Math.min(inputCount, 5); // 每行最多显示5个输入,取输入次数和5的最小值得到列数

// 定义表格的单元格宽度和高度
const cellWidth = 100;
const cellHeight = 50;

// 绘制表格
for (let i = 0; i < rowCount; i++) {
  for (let j = 0; j < columnCount; j++) {
    const x = j * cellWidth;
    const y = i * cellHeight;
    const inputIndex = i * columnCount + j; // 计算当前单元格对应的输入索引

    // 绘制单元格边框
    ctx.strokeRect(x, y, cellWidth, cellHeight);

    // 绘制输入内容
    ctx.fillText(`Input ${inputIndex}`, x + 10, y + 30);
  }
}

// 绘制完成后调用draw方法进行绘制
ctx.draw();

以上代码会根据输入次数自动计算并绘制相应的行数和列数的表格,每个表格单元格的宽度为100,高度为50。输入内容可以根据实际需求进行修改

微信小程序canvas绘制表格根据input次数自动换行

原文地址: https://www.cveoy.top/t/topic/h8to 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录