简体   繁体   English

Codeigniter-在视图中获取表数据,发送表数据并在控制器上检索表数据

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

i'm new on Codeigniter. 我是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). 我想知道如何在视图中获取表数据,通过ajax发送它(也许),并在控制器中检索表数据(我想将表数据导出到excel文件中)。 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: 我的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. 我在控制器文件中有一些修改Excel功能。

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: 您的jquery应该是这样的:

 <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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