如何将数据从视图传递到控制器并以.csv格式导出

时间:2015-06-29 09:04:44

标签: php jquery ajax csv php-5.3

我有问题,我不明白在哪里。所以我有一个控制器:

public function getSupplier(){
    if(isset($_POST['id_supplier'])){
        $iIdSupplier    = $_POST['id_supplier'];
    }
    $aOrdersByFournisseur = \Administration\Logistic\Order::getGiftListBySupplier($iIdSupplier);
    if (isset($_POST['export'])) {
        $sFileName = 'OrdersBySupplier.csv';
        header('Content-Encoding: UTF-8');
        header("Content-Type:application/csv;charset=UTF-8");
        header('Content-Disposition: attachement; filename="' . $sFileName . '";');
        header("Pragma: no-cache");
        header("Expires: 0");
        echo "\xEF\xBB\xBF";
        $output = fopen('php://output', 'w');
        fputcsv($output, array('Re_c', 'Re_f', 'Des', 'Url','Pr','Dis','Qt'), ";");
        foreach ($aOrdersByFournisseur as $value) {
            unset($value['ref_article']);
            unset($value['ref_fournisseur']);
            unset($value['lien_fournisseur']);
            unset($value['prix_ht']);
            unset($value['fournisseur_id']);
            unset($value['nom_article']);
            unset($value['nom_fournisseur']);
            fputcsv($output, $value, ";");
        }
        fpassthru($output);
        fclose($output);
        exit;
    }
    return $this->renderJson($aOrdersByFournisseur );
}

我的观点:

<form id="form_order" action="{{ 'gestion_dotation_get_supplier'|url_route }}" method="post">
    <div class="col-xs-12 col-sm-12 col-md-12">
        <table class="not-display" id="order_information" style="margin-top: 10px;font-size: 14px;">
            <thead>
            <tr style="font-size: 14px;">
                <th>Ref cadeau</th>
                <th>Ref fournisseur</th>
                <th>Description</th>
                <th>Url</th>
                <th>Prix</th>
                <th>Disponibilite</th>
                <th>Quantite</th>
            </tr>
            <tbody id="content-order">
            </tbody>
        </table>
    </div>
    <div style="padding-top: 25px;" id="export-button" class="not-display">
        <input type="submit" style="padding-left: 20px;margin-left: 10px;" class="btn btn-succes" name="export" value="Exporter en CSV"></input>
    </div>
    </form>
    <script>
    $('#suppliers').change(function () {
        var id = $("#suppliers option:selected").val();
        url_deploy = "http://"+window.location.hostname+"/gestion_dotation/getSupplier";
        $.post(
                url_deploy,
                {id_supplier: id},
                function(result) {
                    $("#content-order").empty();
                    if ($.isEmptyObject(result)) {
                        document.getElementById('order_information').setAttribute('class','display-on');
                        document.getElementById('order_information').setAttribute('class','table');
                        document.getElementById('export-button').setAttribute('class','not-display');
                        var html =  '<tr>'+
                                        '<td>'+ 'Aucune donne pour ce fournisseur'+ '</td>'+
                                    '</tr>';
                        $("#content-order").html(html);
                    }
                    for (var item in result) {
                        document.getElementById('order_information').setAttribute('class','display-on');
                        document.getElementById('order_information').setAttribute('class','table');
                        document.getElementById('export-button').setAttribute('class','display-on');
                        var html =  '<tr>'+
                                        '<td>'+ result[ item ]['ref_article']+ '</td>'+
                                        '<td>'+ result[ item ]['ref_fournisseur']+ '</td>'+
                                        '<td>'+ result[ item ]['description']+ '</td>'+
                                        '<td>'+ result[ item ]['lien_fournisseur']+ '</td>'+
                                        '<td>'+ result[ item ]['prix_ht']+ '</td>'+
                                        '<td>'+ 'disponibilite'+ '</td>'+
                                        '<td>'+ result[ item ]['gifts_number']+ '</td>'+
                                     '</tr>';

                        $("#content-order").append(html);
                    }
            });
    });

</script>

当我尝试从表中导出csv数据时,.csv为空,它只包含列的名称。可能我需要在csv之后将数据从视图传递到控制器以进行发送。我不明白该怎么做,因为我在视图中填写了一个请求ajax。当我在if(isset($ _ POST ['export']){}为空时制作print_r($ aOrdersByFournisseur)你能帮我吗?请提前做好。

0 个答案:

没有答案