Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Harisul_Ishbah's avatar

Export Excel (With Criteria) Using Maatwebsite Excel

I will export excel using criteria in datatable, I have been able to find the id that I entered in the rowId variable, how to execute it in the root, controller and export to excel. here's the script :

Ajax : $(function () { $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); let tblMassy = $('#tblMassy').DataTable({ processing: true, serverSide : true, autoWidth : false, pageLength : 10 , "order" : [[ 0, "desc"]], ajax : '{{ URL::to('http://localhost:8000/admin/getMassy') }}',

       columnDefs: [
            {   
                'targets': 0,
                'checkboxes': true
            }
        ],
            columns : [
                {data : 'id' , name: 'id'},
                {data : 'assymainharness' , name: 'assymainharness'},
                {data : 'assyab' , name: 'assyab'},
                {data : 'assyab1' , name: 'assyab1'},
                {data : 'assyab2' , name: 'assyab2'},
                {data : 'assyab3' , name: 'assyab3'},
                {data : 'type' , name: 'type'},
                {data : 'assycode' , name: 'assycode'},
                {data : 'suffixlevel' , name: 'suffixlevel'},
                {data : 'desccarline' , name: 'desccarline'},
                {data : 'factory' , name: 'factory'},
                {data : 'customer' , name: 'customer'},
                {data : 'name' , name: 'name'},
                {data : 'Actions' , name: 'Actions'}
            ]
        });

        $('#export').click(function (e) {
        let rows_selected = tblMassy.column(0).checkboxes.selected();
                    if (rows_selected.length >= 1)  {

                        $.each(rows_selected, function(index, rowId){

                            // console.log( "/eksportfilter/" + rowId ) ;
                            // location.href= "/eksportfilter/" + rowId;
                            $.ajax({
                                type:'GET',
                                url: "/eksportfilter/" + rowId,
                                dataType: "JSON",
                                // async: true,
                                success: function (response) {
                                    console.log( response )
                                }
                            });
                            
                        });
                            console.log(rows_selected.length);

                            swal("Good job!", "Eksport data successfully!", "success");
                            
                            $('#tblMassy').DataTable().ajax.reload();
                            // location.reload();
                    } else {
                        swal("Failed Export!", "No Data Delected!", "error");
                    }
    });

        $('.show_confirm').click(function(event) {
            var form =  $(this).closest("form");
            var name = $(this).data("name");
            event.preventDefault();
            swal({
                title: `Are you sure you want to delete this record?`,
                text: "If you delete this, it will be gone forever.",
                icon: "warning",
                buttons: true,
                dangerMode: true,
            })
            .then((willDelete) => {
                if (willDelete) {
                    // $('#btn_delete_all').click(function (e) {
                        // e.preventDefault();
                    let rows_selected = tblMassy.column(0).checkboxes.selected();
                    console.log( rows_selected.length );
                    if (rows_selected.length >= 1)  {

                        $.each(rows_selected, function(index, rowId){

                            console.log( "http://localhost:8000/admin/deleteAssy/" + rowId ) ;

                            $.ajax({
                                type: "POST",
                                url: "http://localhost:8000/admin/deleteAssy/" + rowId ,
                                dataType: "JSON",
                                success: function (response) {
                                    console.log( response )
                                }
                            });
                        });
                            console.log(rows_selected.length);

                            swal("Good job!", "Delete data successfully!", "success");
                            
                            $('#tblMassy').DataTable().ajax.reload();
                            location.reload();
                    } else {
                        swal("Failed Delete!", "No Data Delected!", "error");
                    }
                    // });
                }
            });
        });

               // submit btndelete all

    });
</script>

Root : //Master Assy Route::resource('admin/m_assy', MAssyController::class) ; Route::get('admin/getMassy', [MAssyController::class , 'getMassy']); Route::get('eksportfilter/{id}', [MAssyController::class , 'eksportfilter']);

Controller :

0 likes
0 replies

Please or to participate in this conversation.