lundi 4 avril 2016

How to export all records in yajra Datatables package for laravel using server side proccessing

I'm using jQuery Datatables plugin combined with Laravel (v 5.1) and yajra Datatables package.

I need to mention - my application use mongodb.

Everything works great except the export button exports only the records currently shown in the Datatable.

My code look like this:

Initialize Datatable (client side):

me.dataTable_obj = $('#leads_table').DataTable({
            processing: true,
            serverSide: true,
            deferRender: true,
            paging: true,
            dom: 'Blfrtip',
            "buttons":me.setLeadsExportButtons(),
            ajax: {
                url: '/leads/test/'+window.client_id,
                data : function (d) {
                   d = me.setFiltersAjaxData(d);
                }
            },
            columns: cols,
           .........

 setLeadsExportButtons: function(){
        var me = this;
        return [
            {
                extend        : 'excel',
                className     : 'btn btn-white',
                defaultContent: '',
                title         : 'leads',
                text          : '<i class="fa fa-file-excel-o"></i> Export',
                exportOptions : {
                    columns: ':visible',
                    format: {
                        body: function(data, column, row) {
                            return me.setExportData(data,column,row);
                        }
                    }
                }
            },
        ];
    },

setExportData : function(data,column,row){
        if(data.indexOf('select') > -1) {
            var select_elem = $.parseHTML(data)[0];
            data = $(select_elem).find('option:selected').text();
        }else if(data.indexOf('last-feed-col') > -1 || data.indexOf('title=') > -1){
            var elem = $.parseHTML(data)[0];
            data = $(elem).prop('title');
        }else if(data.toLowerCase() == "owner"){
            data = "No owner selected";
        }else if(data.toLowerCase() == "quality"){
            data = "No quality selected";
        }else if(data.indexOf('btn-circle') > -1){
            var elem = $.parseHTML(data)[0];
            data = $(elem).text();
        }else if(data.indexOf('single-lead-trigger') > -1){
            var elem = $.parseHTML(data)[0];
            data = $(elem).text();
        }
        return data;

    },

And the server side code look like this:

public function getLeads(Request $request,$client_id = null)
{
    $client_id = is_null($client_id) ? \Auth::user()->client_id : $client_id;
    $columns = $this->account_settings->getColumnsDef($client_id);
    $select = ['_id'];
    foreach($columns as $k => $v){
        $select[] = $k;
    }
    $query = Lead::select($select)->where('deleted',0)->where('client_id',strval($client_id));
    if(\Auth::user()->salesMan()){
        $query->where('owner.owner_id',intval(\Auth::user()->id));
    }
    $query = $this->setQueryDates($request,$query);

    $table = \Datatables::of(collect($query->get()));
    if(\Auth::user()->canDeleteLeads()){
        $table->addColumn('checkbox', function($lead){
            return "<input type='checkbox' class='bulk-delete-leads-checkbox' name='bulk_delete_leads[]' id='".$lead->_id."' /><label for='".$lead->_id."'></label>";
        })->setRowId('_id');
    }
    $unique = ["the_lead.lead_comment", "the_lead.full_name"];
    foreach($columns as $k => $v){
        if(preg_match("/^the\_lead\..*/",$k) && !in_array($k,$unique)){
            $col_name = str_replace("the_lead.","",$k);
            $table->editColumn($col_name, function($lead) use ($col_name){
                $rtn = count($lead->the_lead) && isset($lead->the_lead[$col_name]) ? $lead->the_lead[$col_name] : null;
                return $this->cutStr($rtn);
            });
        }elseif(preg_match("/^ga\_details\..*/",$k)){
            $col_name = str_replace("ga_details.","",$k);
            $table->editColumn($col_name, function($lead) use ($col_name){
                if(count($lead->ga_details) > 1 && isset($lead->ga_details[$col_name])){
                   return $this->cutStr($lead->ga_details[$col_name]);
                }

                return null;
            });
        }else{
            $this->setColumns($k,$table,$client_id);
        }
    }
    $table = $this->leadsFiltersController->filterLeads($request,$table);
    return $table->make(true);
}

The exported excel file shows only the first 10 records (I set the view to 10 records per page).

I saw some question about it but none of the answers could help me.

does anyone know any solution for this?

anything will help!!! thank you very much.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire