通过 JSON 传递 HTML(PHP 和 DOJO)
我有一个正在查询数据库并输出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议分别传递链接 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?
您可以在 dojo 网格中使用
formatter
来格式化每个单元格中显示的 HTML。创建网格时,您可以为每一列设置一个格式化程序
。formatter
是一个 JavaScript 函数,它有两个参数,第一个value
表示单元格的值,第二个rowIndex
表示索引当前行的。formatter
函数的返回值是单元格中显示的 HTML 内容。对于您的情况,我建议您对链接 URL 和锚文本使用单列。您可以使用简单的编码,例如
http://www.google.com$$$Clicky
,其中$$$
用于分隔这两个字段。 PHP 代码为:然后在您的
formatter
函数中,您可以使用:如果您喜欢为每个字段使用一列,例如
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 aformatter
for each column. Theformatter
is a JavaScript function that takes two parameters, the first onevalue
means the value of the cell, the second onerowIndex
means the index of current row. The return value of theformatter
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:Then in your
formatter
function, you can use :If you prefer to use one column for each field, e.g.
url
for URL andanchorText
for the anchor text. Then you need to get the value of another column when formatting the cell. Suppose the grid uses theurl
field. Then theformatter
function may look like below: