javascript 数组对象中的href链接

6ovsh4lw  于 2023-10-14  发布在  Java
关注(0)|答案(2)|浏览(115)

我正在使用Grid.js库来构建WordPress网站中的表格。搜索了很多文档,但找不到 * 如何添加链接 * 到名称的第一列中,以在新的选项卡中打开,该选项卡是数组对象。

HTML

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  7. <link
  8. href="https://unpkg.com/gridjs/dist/theme/mermaid.min.css"
  9. rel="stylesheet"
  10. />
  11. <title>Grid.js Hello World</title>
  12. </head>
  13. <body>
  14. <h1>
  15. Grid.js Hello World
  16. </h1>
  17. <div id="wrapper"></div>
  18. <script src="https://unpkg.com/gridjs/dist/gridjs.umd.js"></script>
  19. <script src="src/index.js"></script>
  20. </body>
  21. </html>

JS文件:

  1. new gridjs.Grid({
  2. columns: ["Name", "Email", "Phone Number"],
  3. search: true,
  4. data: [
  5. ["John", "[email protected]", "(353) 01 222 3333"],
  6. ["Mark", "[email protected]", "(01) 22 888 4444"],
  7. ["Eoin", "[email protected]", "0097 22 654 00033"],
  8. ["Sarah", "[email protected]", "+322 876 1233"],
  9. ["Afshin", "[email protected]", "(353) 22 87 8356"]
  10. ]
  11. }).render(document.getElementById("wrapper"));

输出

View Code in Sandbox

gajydyqb

gajydyqb1#

您可以使用formatter选项和gridjs.html()函数添加自定义HTML。举例来说:

  1. new gridjs.Grid({
  2. // Converted the columns to objects, instead of strings, to pass options
  3. columns: [
  4. { name: "id", hidden: true },
  5. {
  6. name: "Name",
  7. // Added a `formatter` function
  8. formatter: (cell, row) => {
  9. // Uses the `html` function to add elements
  10. // Note that we're pulling a value for the link from the
  11. // data set as well for a more complete, real-world example
  12. return gridjs.html(
  13. `<a href='mypage.html?id=${row.cells[0].data}'>${cell}</a>`
  14. );
  15. }
  16. },
  17. {
  18. name: "Email",
  19. formatter: (cell, row) => {
  20. return gridjs.html(`<a href='mailto:${row.cells[2].data}'>${cell}</a>`);
  21. }
  22. },
  23. { name: "Phone Number" }
  24. ],
  25. search: true,
  26. // Added unique id, per OP's comments
  27. data: [
  28. [1, "John", "[email protected]", "(353) 01 222 3333"],
  29. [2, "Mark", "[email protected]", "(01) 22 888 4444"],
  30. [3, "Eoin", "[email protected]", "0097 22 654 00033"],
  31. [4, "Sarah", "[email protected]", "+322 876 1233"],
  32. [5, "Afshin", "[email protected]", "(353) 22 87 8356"]
  33. ]
  34. }).render(document.getElementById("wrapper"));
  • 根据OP的评论,使用唯一ID更新示例 *

工作代码沙箱:https://codesandbox.io/s/gridjs-hello-world-forked-u52kyg?file=/src/index.js
来源:https://gridjs.io/docs/examples/html-cells

展开查看全部
gopyfrb3

gopyfrb32#

您可以使用gridjs提供的自定义单元格格式化程序。
参考编号:https://gridjs.io/docs/examples/html-cells/
我的工作代码沙盒链接:https://codesandbox.io/s/gridjs-hello-world-forked-uvsgom

相关问题