【问题标题】:Dynamically generate table in form using Ajax with symfony使用带有 symfony 的 Ajax 以表格形式动态生成表格
【发布时间】:2016-04-21 19:32:04
【问题描述】:

我正在使用 symfony 项目。在表单中选择某个选项时,我想在表单中生成表格。

这是我的任务。

表格中有名称选项(例如 John 、 Mia 、 Lia .. 等) 当我们选择 "John" 时。我想使用表格显示有关“约翰”的所有详细信息。 该表应位于表单中。

这个链接中有一些例子http://www.w3schools.com/php/php_ajax_database.asp

但我想使用 symfony 来做到这一点。

最好的方法是什么。

请举个例子。

在 Symfony 中使用 Javascript、Jquery 或 ajax

【问题讨论】:

  • 告诉我们您尝试了什么,以及您失败的地方。

标签: javascript php jquery ajax symfony


【解决方案1】:

Symfony 关键字在这里具有误导性,因为您会从客户端 javascript 代码生成表。在您的 javascript 中,您启动一​​个 Ajax 调用,该调用将请求发布到您的 PHP/Symfony 应用程序。一个返回一个 JSON 对象数组,例如,您的 Ajax 完成/完成方法使用 jQuery 创建表。这是一个例子:

<html>
<head>
    <script src="https://code.jquery.com/jquery-2.2.0.js"></script>
    <script>
    $(document).ready (function () {

    }) ;

    function go () {
        $.post ('/index.php/api/mycall',
        //$.post ('/php.php',
            {
                "param1": "param1",
                "param2": 2
            })
            .done (function (ret) {
                var tbl =$('#mytable') ;
                tbl.empty () ;
                $(document.createElement('tr'))
                    .append ('<th>id</th><th>firstname</th><th>lastname</th>')
                    .appendTo (tbl) ;
                $.each (ret, function (index, val) {
                    $(document.createElement('tr'))
                        .append ('<td>' + val.id + '</td><td>' + val.firstname + '</td><td>' + val.lastname + '</td>')
                        .appendTo (tbl) ;
                }) ;
            }) ;
    }
    </script>
</head>
<body>
    <input type="button" onclick="go()" />
    <div id="mytable"></div>
</body>
</html>

在你的交响乐代码中处理 /api/mycall 的 post 查询的地方

$results =[
  (object)[
    "id" => 1,
    "firstname" => "John",
    "lastname" => "Doe"
  ],
  (object)[
    "id" => 2,
    "firstname" => "Jane",
    "lastname" => "Doe"
  ]
] ;
return (new JsonResponse ($results, Response::HTTP_OK)) ;

希望有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-19
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    • 2013-09-22
    相关资源
    最近更新 更多