[英]Define in Smarty the format to be returned by AJAX
I have a html form in a Smarty template. 我在Smarty模板中有一个html表单。 A
select
in the form is populated via AJAX. 表单中的
select
通过AJAX填充。 I would like to specify in the Smarty template, the format to be returned by the AJAX-call. 我想在Smarty模板中指定AJAX调用返回的格式。 This way I can use the same request-url for different purposes.
这样,我可以将相同的请求网址用于不同的目的。 For example if the
select
contains users, sometimes I might be interested in knowing the user's email address (format: "{$Option->GetName()} ({$Option->Email})"
), and other times the user's organisation (format: "{$Option->GetName()} ({$Option->Organisation})"
). 例如,如果
select
包含用户,有时我可能想知道用户的电子邮件地址(格式: "{$Option->GetName()} ({$Option->Email})"
),而有时是用户的组织(格式: "{$Option->GetName()} ({$Option->Organisation})"
)。
I have a few ideas of how to achieve this: 我对如何实现这一点有一些想法:
$Option
object, and do the formatting using JavaScript. $Option
对象,并使用JavaScript进行格式化。 Drawback : Cannot use the PHP object's methods, such as $Option->GetName()
. $Option->GetName()
。 Do anyone have any other/better ideas, or any experience with doing something like this? 是否有人有其他/更好的主意,或有做类似事情的经验?
I don't think you need to set some format. 我认为您不需要设置某种格式。 You can create a javascript function with 3 parameters.
您可以使用3个参数创建一个javascript函数。 The
<select>
tag identifier, the name of the key used for options value, and the key name used for options text. <select>
标记标识符,用于选项值的键的名称以及用于选项文本的键名。 The function will make the ajax request. 该函数将发出ajax请求。 That way you can call the function with settings you need for particular situation.
这样,您可以使用特定情况所需的设置来调用该函数。
Further details depend on your current code. 更多详细信息取决于您当前的代码。
I ended up using option 4, and I'm very happy with the result. 我最终使用了选项4,对此结果我感到非常满意。
If anyone come across this and need information on how I implemented it, just post a comment, and I'll try to explain. 如果有人遇到这个问题,并且需要有关我如何实现的信息,只需发表评论,我将尽力解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.