通过 JSON 传递 HTML(PHP 和 DOJO)

发布于 2024-10-05 12:12:02 字数 330 浏览 0 评论 0原文

我有一个正在查询数据库并输出 JSon 的数据存储...类似这样的内容:

$data[] = array('id' => $i, 'prod_id' => $product_id, 'link' => $link);

我想知道如何使用 $link 变量传回链接。例如,如果我有这个:

$link = "<a href=\"google.com\"> Clicky </a>";

数据网格将显示 Clicky 而不是实际的 html 链接...有没有办法传回 html?

I have a datastore that is querying a database and outputting JSon... something like this:

$data[] = array('id' => $i, 'prod_id' => $product_id, 'link' => $link);

I'm wondering how you can pass back a link using the $link variable. If I had this for example:

$link = "<a href=\"google.com\"> Clicky </a>";

The datagrid would display Clicky and not the actual html link... Is there anyway to pass back html?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山人契 2024-10-12 12:12:02

我建议分别传递链接 URL 和链接文本,然后在客户端将它们重构为 JavaScript 中的锚链接。

您还可以尝试转义 HTML,然后在客户端取消转义。

我不知道为什么它不会发送链接 - 也许浏览器试图过早解析发送的 HTML?

I would suggest passing the link URL and the link text separately, then reconstructing them into an anchor link in JavaScript on the client-side.

You could also try escaping the HTML, then unescaping on the client-side.

I have no idea why it won't send links- perhaps the browser is trying to parse the sent HTML too early?

十级心震 2024-10-12 12:12:02

您可以在 dojo 网格中使用 formatter 来格式化每个单元格中显示的 HTML。创建网格时,您可以为每一列设置一个格式化程序formatter 是一个 JavaScript 函数,它有两个参数,第一个 value 表示单元格的值,第二个 rowIndex 表示索引当前行的。 formatter 函数的返回值是单元格中显示的 HTML 内容。

对于您的情况,我建议您对链接 URL 和锚文本使用单列。您可以使用简单的编码,例如 http://www.google.com$$$Clicky,其中 $$$ 用于分隔这两个字段。 PHP 代码为:

$link = "http://www.google.com$$Clicky";

然后在您的 formatter 函数中,您可以使用:

function(value, rowIndex) {
    var parts = value.split('$

如果您喜欢为每个字段使用一列,例如 url 用于 URL 和 anchorText 作为锚文本。然后在格式化单元格时需要获取另一列的值。假设网格使用 url 字段。那么 formatter 函数可能如下所示:

function(value, rowIndex) {
   var item = grid.getItem(rowIndex); // Get the store item by index, need the reference of the grid.
   var anchorText = grid.store.getValue(item, 'anchorText');
   return "<a href='" + value + "'>" + anchorText + "</a>";
}
); return "<a href='" + parts[0] + "'>" + parts[1] + "</a>"; }

如果您喜欢为每个字段使用一列,例如 url 用于 URL 和 anchorText 作为锚文本。然后在格式化单元格时需要获取另一列的值。假设网格使用 url 字段。那么 formatter 函数可能如下所示:

You can use formatter in dojo grid to format the HTML displayed in each cell. When creating the grid, you can set a formatter for each column. The formatter is a JavaScript function that takes two parameters, the first one value means the value of the cell, the second one rowIndex means the index of current row. The return value of the formatter function is the HTML content displayed in the cell.

For your case, I would suggest that you use a single column for both the link URL and anchor text. You can use a simple encoding, like http://www.google.com$$$Clicky, where $$$ is used to separate these two fields. The PHP code would be:

$link = "http://www.google.com$$Clicky";

Then in your formatter function, you can use :

function(value, rowIndex) {
    var parts = value.split('$

If you prefer to use one column for each field, e.g. url for URL and anchorText for the anchor text. Then you need to get the value of another column when formatting the cell. Suppose the grid uses the url field. Then the formatter function may look like below:

function(value, rowIndex) {
   var item = grid.getItem(rowIndex); // Get the store item by index, need the reference of the grid.
   var anchorText = grid.store.getValue(item, 'anchorText');
   return "<a href='" + value + "'>" + anchorText + "</a>";
}
); return "<a href='" + parts[0] + "'>" + parts[1] + "</a>"; }

If you prefer to use one column for each field, e.g. url for URL and anchorText for the anchor text. Then you need to get the value of another column when formatting the cell. Suppose the grid uses the url field. Then the formatter function may look like below:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文