Angularjs承诺订单下载excel

问题描述 投票:0回答:1

[我正在尝试从后端(java)获取代码,填充一个隐藏表(Angularjs)并像excel一样导出,但是我执行诺言时遇到了问题,因为excel会在表填充之前导出:

Angularjs

consultaService.detalleDashboard({'idBusqueda': 0,'identificador' : 0,'tipo': 0},null)
.$promise.then(function (result) {
    $scope.dataExportar = {
        data:result.item1 //I get data well
    }               
 }, function (error) {
        $log.error("** Error al obtener Info para excel", error);
}).then($scope.generaExcel()) //this 'then' execute before mi table was filled            

查看代码:

<table id="headerTable">
        <tbody>
            <tr>
                <td>ID OFICINA</td>
                <td>ID SUPERVISORIA</td>                        
            </tr>                   
            <tr ng-repeat="l in dataExportar.data">
                <td>{{l.idOficina}}</td>
                <td>{{l.idSupervisoria}}</td>
            </tr>
        </tbody>
</table>

用于导出Excel的代码

$scope.generaExcel = function(){
     //export to excel file
    var tab_text = '<table border="1px" style="font-size:20px" ">';
    var textRange;
    var j = 0;
    var tab = document.getElementById('headerTable'); // id of table
    var lines = tab.rows.length;

    // the first headline of the table
    if (lines > 0) {
        tab_text = tab_text + '<tr bgcolor="#DFDFDF">' + tab.rows[0].innerHTML + '</tr>';
    }

    // table data lines, loop starting from 1
    for (j = 1 ; j < lines; j++) {
        tab_text = tab_text + "<tr>" + tab.rows[j].innerHTML + "</tr>";                                
    }

    tab_text = tab_text + "</table>";
    tab_text = tab_text.replace(/<A[^>]*>|<\/A>/g, "");          //remove if u want links in your table
    tab_text = tab_text.replace(/<img[^>]*>/gi, "");             // remove if u want images in your table
    tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params

    // console.log(tab_text); // aktivate so see the result (press F12 in browser)               
    var fileName = 'report.xls'                            
    var exceldata = new Blob([tab_text], { type: "application/vnd.ms-excel;charset=utf-8" }) 

    if (window.navigator.msSaveBlob) { // IE 10+
        window.navigator.msSaveOrOpenBlob(exceldata, fileName);
        //$scope.DataNullEventDetails = true;
    } else {
        var link = document.createElement('a'); //create link download file
        link.href = window.URL.createObjectURL(exceldata); // set url for link download
        link.setAttribute('download', fileName); //set attribute for link created
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    }
}

因为我的文件是在得到此文件之前生成的

excel fail

查看更新良好(后端过程完成后:)

View updated

UPDATE

我更改了代码,现在它等待后台处理,但是我需要等待刷新表,因为excel之前生成过:

 $scope.savePolizasVigentes = function() {                  
           consultaService.detalleDashboard({'idBusqueda': 0,'identificador' : 0,'tipo': 0},null)
           .$promise.then(function (result) {
              $scope.dataExportar = {
                data:result.item1                           
                                    }               
           }, function (error) {
               $log.error("** Error al obtener Info para excel", error);
              }).then(function callback(response){                      
               $('#headerTable').html(response);
              }).then($scope.generaExcel)                                                                                               
          }
javascript html angularjs angular-promise
1个回答
0
投票

不要调用该函数:

© www.soinside.com 2019 - 2024. All rights reserved.