简体   繁体   中英

Codeigniter - Get table data in view, send table data and retrieve table data on controller

i'm new on Codeigniter. I want to know how to get table data in view, send it through ajax(maybe), and retrieve the table data in controller(i want to export table data into an excel file). Thanks in advance.

My view:

<table id="mytable">
                    <?php 
                        $i=1;
                        foreach ($placeout as $placeout_item){
                    ?>
                            <tr class="gradeX">
                                <td class="center"><?php echo $i; $i++; ?></td>
                                <td><?php echo $placeout_item['pjname']; ?></td>
                                <td><?php echo $placeout_item['entries_date']; ?></td>
                                <td><?php echo $placeout_item['lg_id']; ?></td>
                                <td class="right"><?php echo $placeout_item['entries_amt']; ?></td>
                                <td class="right"><?php echo $placeout_item['comm']; ?></td>
                                <td class="center"><?php echo $placeout_item['curr_id']; ?></td>
                            </tr>
                    <?php 
                        }
                    ?>
</table>
<button type="reset" class="btn btn-icon btn-default" onclick="exportExcel('Placeout')"><i></i>Export to Excel</button>

My jquery:

<script>
var data_table = [];
$('#mytable td').each(function() {
    data_table.push($(this).html());   //make an array from table data
});

function exportExcel(name) {
    $.ajax(
    {
        url: "<?php echo site_url('export/excel/"+name+"'); ?>",
        type:'POST', //data type
        dataType : "json",         //this line should be erased???
        data : {tes:data_table}
    });
}
</script>

My controller:

function excel($name="")
{
    $data_table=$this->input->post('tes');

    $objPHPExcel = new PHPExcel();

    if($data_table!=null)
    {
        foreach ($data_table as $data)
        {
            $exceldata[] = $data;
        }
        $objPHPExcel->getActiveSheet()->fromArray($exceldata, null, 'A2');
    }
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="'.$name.'.xls"');
    header('Cache-Control: max-age=0');
    $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');                
    $objWriter->save('php://output');
}

i have some modify excel function in controller files.

function excel($name="")
{
     $filename =  "excel_report.csv";   
     $fp = fopen('php://output', 'w');            
     header('Content-Type: text/csv; charset=utf-8');       
     header('Content-type: application/csv');
     header('Content-Disposition: attachment; filename='.$filename );

    $data_table=$this->input->post('tes');

    if($data_table!=null)
    {
        foreach ($data_table as $data)
        {
            $exceldata = array(
                    $data['column_name_1'],   // dummy column name here 
                    $data['column_name_2'],
                    $data['column_name_3'],
                    $data['column_name_4'],
                    );
            fputcsv($fp, $exceldata );
        }

    }
 die(); 

}

Your jquery should be like this:

 <script>
 var data_table = [];
 $('#mytable tr').each(function() {
    var tr = [];
    $(this).find('td').each(function(){
       tr.push($(this).html())
    });
    data_table.push(tr);   //make an array from table data
 });

 function exportExcel(name) {
     $.ajax(
     {
         url: "<?php echo site_url('export/excel/"+name+"'); ?>",
         type:'POST', //data type
         dataType : "json",         //this line should be erased???
         data : {tes:data_table}
     });
 }
 </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM