【问题标题】:Get data from controller and then print it via ajax从控制器获取数据,然后通过 ajax 打印
【发布时间】:2015-08-21 10:45:36
【问题描述】:

我有打印操作,女巫从 db 获取我的所有记录

public function printAction()
{
    $users = $this->getDoctrine()->getRepository('ModelBundle:Users')->findAll();
    $resp = new JsonResponse($users, 200);
    return $resp;
}

我想在 div 元素中使用这些数据并通过 ajax 打印,但我不明白,我该怎么做。也许有人知道。请帮帮我。

【问题讨论】:

    标签: php jquery ajax symfony


    【解决方案1】:

    您的用户实体中是否有序列化程序?因为您将所有用户作为实体。我建议你这样做:

    当你点击一个按钮或任何元素时,像这样调用你的路由:

    //change this element to your button
    $('a.press-me').click(function() {
        $.ajax({
            url: 'YOUR ROUTE HERE',
            dataType: 'HTML',
            success: function (data) {
                $('#your-div').html(data);
            }
        });
    });
    
    //create a view users.html.twig or something like that...
    {% if users is defined and users is not empty %}
        {% for user in users %}
            Name: {{ user.name }}
        {% endfor %}
    {% else %}
        <p class="error">No data!</p>
    {% endif %}
    
    //add this line to top of your controller
    use Symfony\Component\HttpFoundation\Request;
    
    //add request variable to your action
    public function printAction(Request $request)
    {
        //you want to get users via ajax right? so check if the request is ajax or not
        if($request->isXmlHttpRequest()) {
            // Get all users
            $users = $this->getDoctrine()->getRepository('ModelBundle:Users')->findAll();
    
        //render a view and pass it as variable
        return $this->renderView('ModelBundle:Default:users.html.twig', ['users'] => $users);
        }
    }
    

    【讨论】:

    • 好的。你能解释一下,当我返回响应时,我总是用 json 编码。我该如何解决?
    • 我认为您的实体文件上有一个序列化程序。在您的实体上查找 getUsers 函数。
    【解决方案2】:

    假设在你看来,

    <div class="dynamic-data">
    
    </div>
    
    
    $.ajax({
             url: "path to your print function",
             type: "POST",
             dataType: "HTML",
             async: false,
             data: {"u": url},
             success: function(data) {
                      $(".dynamic-data").html(data);  
                   // here directly manipulate the data in controller or get the data in success function and manipulate .                   
              }
          });
    

    在打印函数中而不是返回打印数据

    【讨论】:

    • 我必须返回数据,不要打印
    • 有了这些数据你想做什么??这样我可以帮助你
    【解决方案3】:

    使用 jquery ajax() 函数将数据渲染到 html 标签。最好的方法是创建一个通用方法如下,并使用 url 和 div id 对其进行分类

    function renderPartialInDiv(url, divID) {
        $.ajax({
            cache: false,
            async: true,
            type: "POST",
            url: url,
            data: null,
            success: function (data) {
                $(divID).html(data);
            },
            processData: false,
            async: false
        });
    

    【讨论】:

      猜你喜欢
      • 2015-01-03
      • 2018-10-11
      • 2020-08-10
      • 1970-01-01
      • 1970-01-01
      • 2013-05-08
      • 1970-01-01
      • 1970-01-01
      • 2017-09-06
      相关资源
      最近更新 更多