简体   繁体   English

如何从JavaScript函数中的Smarty模板发送数组作为参数

[英]How to send an array as param from smarty template in javascript function

Problem description: 问题描述:

I have an Array($all_article_data) in a .tpl File with 200 items. 我在200个项目的.tpl文件中有一个Array($ all_article_data)。 Now I want to send this array as Param in a javascript function. 现在,我想在JavaScript函数中将此数组作为Param发送。

<span onclick="on('{$all_article_data}')">Example</span>

JAVASCRIPTFUNCTION JAVASCRIPTFUNCTION

function on(data){
     alert(data);
}

And I recive in text "array" :( To see if there is something in this array i used: 我在文本“ array”中朗读:(看看我使用的这个数组中是否有东西:

function on(data) {
   var i;
   for(i=0 ; i<=data.length; i++){
       alert(data[i]);
   }
}

This shows in differents alerts array I need to manipulate this array in javascript and create a table with the values. 这显示了在Alerts警报数组中的差异,我需要在javascript中操纵该数组并使用值创建一个表。

Thanks for Tipps 感谢小费

Best regards 最好的祝福

You should use php's json_encode on your array: 您应该在数组上使用php的json_encode

$encoded= json_encode($all_article_data);

<span onclick="on('{$encoded}')">Example</span>

Then in your javascript, parse it back: 然后在您的JavaScript中将其解析回:

let jsonData = JSON.parse(data);

You can then use jsonData as you intend. 然后,您可以根据需要使用jsonData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM