【问题标题】:how to show all record with AJAX and JSON如何使用 AJAX 和 JSON 显示所有记录
【发布时间】:2015-03-01 08:37:47
【问题描述】:

我使用 Laravel 4.2.0。我想创建表单搜索, 这是我在 Blade.php 中的代码:

var date1 = $('[name=date1]').val();
var date2 = $('[name=date2]').val();
$.ajax({
   type: 'POST',
   url: "{{URL::to('filterCode')}}",
   data: "date1="+date1+"&date2="+date2, 
   dataType: 'json',
   success: function(data){
      console.log(data.code);                     
   }
});

这是控制器中的代码filterCode

$date1 = Input::get('date1');
$date2 = Input::get('date2');

$filter = DB::table('orders')
                ->whereBetween('orders.date',array($date1,$date2))
                ->get();
foreach ($filter as $f) {
    $data = array('code' => $f->code);
    echo json_encode($data);
}

在数据库中记录:

----------------------
| code  |    date    |
----------------------
| A001  | 2015-01-08 |
----------------------
| A002  | 2015-01-18 |
----------------------
| A003  | 2015-01-20 |
----------------------

如果我选择日期 2015-01-08 到 2015-01-10,它将显示代码 A001。 如果我选择日期 2015-01-08 到 2015-01-18,它应该显示代码 A001 到 A002。 但它没有显示任何东西。你能帮帮我吗?

【问题讨论】:

    标签: php ajax json laravel


    【解决方案1】:

    在你的控制器上试试这个。

    $date1 = Input::get('date1');
    $date2 = Input::get('date2');
    
    $filter = DB::table('orders')
                    ->whereBetween('date',array($date1,$date2))
                    ->get();
    $data = [];
    foreach ($filter as $f) {
        $data = array_add($data, 'code', $f->code);
    }
    return Response::json(['data' => $data]);
    

    【讨论】:

      猜你喜欢
      • 2021-12-27
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      相关资源
      最近更新 更多