如何修复使用ajax从控制器传递的表中的行值?

时间:2019-01-01 07:41:21

标签: laravel

我必须使用ajax从控制器传递表值以进行查看。我成功传递了表值,但所有数据都在同一列中。我想在行中显示数据。

我尝试了可能的情况。因为,这是我第一次遇到这个问题,所以我要求指导我。

//这是视图中的我的表代码:

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="test app">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>PWA Testing</title>
<link rel="apple-touch-icon" href="images/apple-touch-icon.png">
<link rel="icon" sizes="32x32" href="images/favicon-32x32.png">
<link rel="icon" sizes="16x16" href="images/favicon-16x16.png">
<link rel="icon" sizes="48x48" href="images/favicon-48x48.png">
<link rel="icon" sizes="72x72" href="images/icon-72x72.png">
<link rel="icon" sizes="96x96" href="images/icon-96x96.png">
<link rel="icon" sizes="144x144" href="images/icon-144x144.png">
<link rel="icon" sizes="192x192" href="images/icon-192x192.png">
<link rel="icon" sizes="512x512" href="images/icon-512x512.png">

<meta name="msapplication-TileImage" content="images/icon-144x144.png">

<link rel="manifest" href="manifest.json">
<link rel="mask-icon" href="images/safari-pinned-tab.svg" color="#840012">
<meta name="msapplication-TileColor" content="#410101">
<meta name="theme-color" content="#410101">
<meta name="msapplication-config" content="browserconfig.xml" />

<meta name="application-name" content="PWA Testing Project">
<meta name="apple-mobile-web-app-title" content="PWA Testing Project">

<meta name="mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-capable" content="yes" />

<meta name="apple-mobile-web-app-status-bar-style" content="black">

<meta property="og:title" content="test app">
<meta property="og:type" content="website">
<meta property="og:image" content="https://airhorner.com/images/touch/Airhorner_192.png">
<meta property="og:url" content="https://example.com/">
<meta property="og:description" content="test app">
<meta name="twitter:card" content="summary">
<meta name="twitter:url" content="https://example.com/">
<meta name="twitter:title" content="Air Horn">
<meta name="twitter:description" content="test app">
<meta name="twitter:image" content="https://example.com/images/touch/Airhorner_192.png">
<meta name="twitter:creator" content="@ahmed_badawy_x">

//这是我的ajax:

 <table class="report">
    <tr>
   <th class="report-th"> Module ID </th>
   <th class="report-th"> Module Name </th>
   <th class="report-th"> Module Status </th>
</tr>
<tr id='values'>
</tr>

//这是我的控制器:

 $(function () {
        $(".report").click(function () {
            var title = $(this).data('title');

            var id=$(this).data('id');
            $("#project-title").val(title);

             $('#project-id').val(id);


                $.ajax({


                    url: "{{url('tms/projects/',[null])}}/"+id,
                       method: 'GET',

                       success:function(response){
                     // console.log(response);
                       $('#values').html(response);
                 }
          });
        });

    });

2 个答案:

答案 0 :(得分:0)

您的ajax调用功能,

$(function () {
        $(".report").click(function () {
            var title = $(this).data('title');

            var id=$(this).data('id');
            $("#project-title").val(title);

             $('#project-id').val(id);


                $.ajax({


                    url: "{{url('tms/projects/',[null])}}/"+id,
                    //type instead of method
                    type: 'GET',

                       success:function(response){
                     // console.log(response);
                       // append instead of html
                       $('#values').append(response.html);
                 }
          });
        });

    });

您的控制器,

 public function showajax($id){
       $modules= modules::where('project_id',$id)->get();

    foreach($modules as $row)
    {
        $html[]=
              '<td>' . $row->id . '</td>' .
                 '<td>' . $row->title . '</td>' .
                 '<td>' . $row->status. '</td>'.
                 '<br/>';
    }  

     return Response::json(['success' => true, 'html'=>$html]);

}

答案 1 :(得分:0)

根据您的回答,在行中添加类不能解决您的问题。您必须使用insertAfter()

var $tableRow = $('.report tr');

$.ajax({
    url: "{{url('tms/projects/',[null])}}/"+id,
    method: 'GET',
    success:function(response){
        $(response).insertAfter( $tableRow );
    }
});