【问题标题】:display data using Ajax in laravel在 laravel 中使用 Ajax 显示数据
【发布时间】:2019-03-31 04:01:11
【问题描述】:

我正在尝试从数据库中的“公司”表中获取数据,它以 json 格式输出,我必须将其显示在数据表中。

控制器

public function ex(Request $request){
    $table =  \DB::table('company')->select('name','type','address','city','email','description')->get();
    //return view('mydata')->with('company',$table);
    return response($table);
}

路线

Route::get('display','Test@ex');

刀片页面

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        $("button").click(function(){
           $("table").toggle();
           $.get("{{URL::to('display')}}",function (data) {
               $.each(data,function (i,value) {
                 var  tr =$("<tr/>");
                 tr.append($("<td/>",{
                     text : value.name /*Get first column*/
                 }))
               })
           })
        });
    });
    </script> 
</head> 

<body> 
<button>Click Me</button> 
<table border="1px" class="table table-striped table-bordered" style="width:100%">
    <thead>
    <tr>
        <th>Name</th>
        <th>Type</th>
        <th>Address</th>
        <th>City</th>
        <th>Email</th>
        <th>Description</th>
    </tr>
    </thead> 
    </table> 
</body> 
</html>

输出如下图所示,但我想在数据表中显示它

【问题讨论】:

    标签: html ajax database laravel-5.4


    【解决方案1】:

    你可以使用 yajjra laravel 包通过 ajax 显示数据

    这里是通过 ajax 在数据表中显示数据的完整教程 Datable Tutorial

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      相关资源
      最近更新 更多