具有自定义日期范围的Laravel数据表过滤服务器端处理

时间:2016-05-17 09:04:43

标签: javascript mysql database laravel datatables

我的系统中有一个我希望优化的数据表。我有Laravel和jQuery Datatables。我的问题是,如果我的数据网格在绘制任何数据之前需要自定义过滤器,我想优化数据呈现。在我的其他数据表中,我有数据网格,它将在页面加载时提取数据,这很好,我可以按照本指南进行服务器端处理:

http://datatables.yajrabox.com/eloquent/basic

然而,如果我有3个过滤器,如$("#FormListBtn").click(function() { var table = $('#FormList').DataTable(); $.ajax({ url: "form-list", type: 'GET', data: {"from" : $("#datePickerFrom").val(), "to" : $("#datePickerTo").val(), "agent_id" : $("#agent_id").val()}, success: function(result){ var myObj = $.parseJSON(result); $.each(myObj, function(key,value) { var t = $('#FormList').DataTable(); t.row.add( [ value.id, value.calldatetime, value.survey, value.agent, value.campaign, value.name, value.phone, value.average_score, "<a class='btn btn-small btn-info' href='<?php echo URL::to('form').'/';?>"+value.id+"/edit'><span class='glyphicon glyphicon glyphicon-edit' aria-hidden='true'></span></a>", "<form method='POST' action='<?php echo URL::to('form').'/';?>"+value.id+"' accept-charset='UTF-8' class='pull-left' >"+ "<input name='_method' type='hidden' value='DELETE'>"+ "<button type='submit' class='btn btn-warning'><span class='glyphicon glyphicon-trash' aria-hidden='true'></span></button>"+"</form>", ] ).draw(); }); }}); }); ,我可以使它工作,但我担心的是我的代码是在获取ajax响应时编写数据客户端,如果我有非常庞大的数据集需要时间。我目前的代码是:

public function getFormList()
{
    $from = Input::get("from");
    $to   = Input::get("to");
    $agent_id   = Input::get("agent_id");

    if($agent_id != '')
    {
        $data2 = DB::table('forms')
        ->join('agents', 'agents.id', '=', 'forms.agent_id')
        ->join('campaigns', 'campaigns.id', '=', 'forms.campaign_id')
        ->join('surveys', 'surveys.id', '=', 'forms.survey_id')
        ->join('users', 'users.id', '=', 'forms.evaluatedby_id')
        ->select(
            'forms.id', 
            'forms.calldatetime', 
            'forms.phone', 
            'forms.calldatetime', 
            'forms.average_score', 
            'forms.auto_fail', 
            'forms.yes_count', 
            'forms.no_count', 
            'forms.na_count', 
            'users.name', 
            'agents.name as agent', 
            'campaigns.name as campaign',
            'surveys.name as survey'
            )
        ->where('calldatetime', '>=', $from)
        ->where('calldatetime', '<=', $to)
        ->where('forms.agent_id', '=', $agent_id)
        ->get();
        return json_encode($data2);
    }
    else
    {
        $data = DB::table('forms')
        ->join('agents', 'agents.id', '=', 'forms.agent_id')
        ->join('campaigns', 'campaigns.id', '=', 'forms.campaign_id')
        ->join('surveys', 'surveys.id', '=', 'forms.survey_id')
        ->join('users', 'users.id', '=', 'forms.evaluatedby_id')
        ->select(
            'forms.id', 
            'forms.calldatetime', 
            'forms.phone', 
            'forms.calldatetime', 
            'forms.average_score', 
            'forms.auto_fail', 
            'forms.yes_count', 
            'forms.no_count', 
            'forms.na_count', 
            'users.name', 
            'agents.name as agent', 
            'campaigns.name as campaign',
            'surveys.name as survey'
            )
        ->where('calldatetime', '>=', $from)
        ->where('calldatetime', '<=', $to)
        ->get();
        return json_encode($data);
    }

}

正如您所看到的,我正在循环中写入行,这将耗费大量数据。还有另一种方法可以让它更快吗?

过程是:用户将选择过滤器的日期范围和代理名称,然后单击“提交”,然后生成表格。

我的控制器

checkbox

0 个答案:

没有答案