weixin_33725270 2015-04-18 20:38 采纳率: 0%
浏览 24

Laravel 5和AJAX

I want to display a partial form using ajax when I select the client name. So far I have managed to fetch data from database using this:

<script>
  $('#client').change(function(){
    var client = $(this).val();
    var host = "{{URL::to('/')}}";
    var url = host + '/newinvoice/' + client;
    $.ajax({
      url: url,
      type: 'GET',
      beforeSend: function(){
        $('.client_services').html('Loading...');
      },
      success: function(data){
        $('.client_services').html(data);
      }
    });

    return false;
  });
</script>

Here is my Controller.

public function getClientServices(Client $client) {
  return json_encode($client->client_service()->get());
}

So instead of $('.client_services').html(data); I want to display text inputs depending on the number of available services for that particular client.

I don't know how to do that.

The above snippet returns results like:

[{"id":1,"client_id":1,"service_id":1,"frequency":90,"deleted_at":null,"created_at":"2015-04-15 10:20:33","updated_at":"2015-04-15 12:22:48"},{"id":2,"client_id":1,"service_id":1,"frequency":0,"deleted_at":null,"created_at":"2015-04-15 13:10:41","updated_at":"2015-04-15 13:24:41"},{"id":3,"client_id":1,"service_id":1,"frequency":180,"deleted_at":null,"created_at":"2015-04-15 13:58:39","updated_at":"2015-04-15 15:34:46"}]

I'm interested on id only.

What I want to do after that is enter the charges for that particular service for that particular client.

I hope I am clear.

  • 写回答

1条回答 默认 最新

  • weixin_33704234 2015-04-19 14:26
    关注

    Build a partial view "_clientservice" with the final html structure you want to inject.

    In your controller:

    public function getClientServices(Client $client)()
    {
        $clientService = $client->clientService()->get();
        return \Response::json(['html' => view('_clientservices', compact('clientService'))->render()]);
    }
    

    and then:

    <script>
      $('#client').change(function(){
        var client = $(this).val();
        var host = "{{URL::to('/')}}";
        var url = host + '/newinvoice/' + client;
        $.ajax({
          url: url,
          type: 'GET',
          beforeSend: function(){
            $('.client_services').html('Loading...');
          },
          success: function(data){
            $('.client_services').html(data.html);
          }
        });
    
        return false;
      });
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题