将帕格列和行导出为CSV

时间:2019-03-09 19:56:16

标签: javascript row pug export-to-csv col

extends layout

block content

  p#demo

  h1 #[Repository Name :] #{repo.name}

  dd #[ IBM Github URL:]
    a(href='/'+repo.url) #{repo.url}
  dd #[ Repository ID:] #{repo._id}
  dd #[ Language Type:] #{repo.type}




  div(class='col')
  div(class='col-sm-9')
  p
  button.hidediv Hide dependencies
  button.showdiv Show dependencies
  .divdemo
    | Dependencies
    .button
    .container
    #dvData
    .col-sm-3(style='background-color:#5596e9;')
      div(class='col-sm-2')
      table
        tbody
        tr: th Name
        tbody

          each D in list_dependencies
            tr
              td 
               td #{D.name}
    .col-sm-3(style='background-color:#f5f7fa;')
     div(class='col-sm-2')
      table
        thead
        tr: th Version
        tbody

           each D in list_dependencies
            tr
              td 
                td #{D.version}
    .col-sm-3(style='background-color:#ADD8E6;') 
      div(class='col-sm-2')
      table
        thead
        tr: th repository ID
        tbody

          each D in list_dependencies
            tr
              td 
               td #{D.repo_id}


    .button.col-sm-6
      a#export(href='#', role='button')
        | Click On This Here Link To Export The Table Data into a CSV File









    script(type='text/javascript', src='https://code.jquery.com/jquery-1.11.0.min.js')

      //script(src='https://cdn.jsdelivr.net/npm/json2csv')
      //script(src='/node_modules/table2csv/dist/table2csv.min.js')



  //script(src='/javascript/client.js')  

$(function() {
    $('#register').on('click', function(event) {
        event.preventDefault();
        var fullname = $('#fullname').val();
        var email = $('#email').val();
        var password = $('#password').val();
        var cpassword = $('#cpassword').val();

        if (!fullname || !email || !password || !cpassword) {
            $('#msgDiv').show().html('All fields are required.');
        } else if (cpassword != password) {
            $('#msgDiv').show().html('Passowrds should match.');
        } else {
            $.ajax({
                url    : '/register',
                method : 'POST',
                data   : { full_name: fullname, email: email, password: password, cpassword: cpassword }
            }).done(function(data) {
                if (data) {
                    if (data.status == 'error') {
                        var errors = '<ul>';
                        $.each(data.message, function(key, value) {
                            errors = errors + '<li>' + value.msg + '</li>';
                        });

                        errors = errors + '</ul>';
                        $('#msgDiv').html(errors).show();
                    } else {
                        $('#msgDiv').removeClass('alert-danger').addClass('alert-success').html(data.message).show();
                    }
                }
            });
        }
    });
});
function myFunction() {
    var x = document.getElementById('myDIV');
    if (x.style.display === 'none') {
        x.style.display = 'block';
    } else {
        x.style.display = 'none';
    }
}
$(document).ready(function() {
    $('.hidediv').click(function() {
        $('.divdemo').hide('slow');
    });

    $('.showdiv').click(function() {
        $('.divdemo').show(2000);
    });
});
$(document).ready(function() {
    console.log('HELLO');
    function exportTableToCSV($table, filename) {
        var $headers = $table.find('tr:has(th)'),
            $rows = $table.find('tr:has(td)'),
            // Temporary delimiter characters unlikely to be typed by keyboard
            // This is to avoid accidentally splitting the actual contents
            tmpColDelim = String.fromCharCode(11), // vertical tab character
            tmpRowDelim = String.fromCharCode(0), // null character
            // actual delimiter characters for CSV format
            colDelim = '","',
            rowDelim = '"\r\n"';

        // Grab text from table into CSV formatted string
        var csv = '"';
        csv += formatRows($headers.map(grabRow));
        csv += rowDelim;
        csv += formatRows($rows.map(grabRow)) + '"';

        // Data URI
        var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);

        // For IE (tested 10+)
        if (window.navigator.msSaveOrOpenBlob) {
            var blob = new Blob([ decodeURIComponent(encodeURI(csv)) ], {
                type : 'text/csv;charset=utf-8;'
            });
            navigator.msSaveBlob(blob, filename);
        } else {
            $(this).attr({
                download : filename,
                href     : csvData
                //,'target' : '_blank' //if you want it to open in a new window
            });
        }

        //------------------------------------------------------------
        // Helper Functions
        //------------------------------------------------------------
        // Format the output so it has the appropriate delimiters
        function formatRows(rows) {
            return rows.get().join(tmpRowDelim).split(tmpRowDelim).join(rowDelim).split(tmpColDelim).join(colDelim);
        }
        // Grab and format a row from the table
        function grabRow(i, row) {
            var $row = $(row);
            //for some reason $cols = $row.find('td') || $row.find('th') won't work...
            var $cols = $row.find('td');
            if (!$cols.length) $cols = $row.find('th');

            return $cols.map(grabCol).get().join(tmpColDelim);
        }
        // Grab and format a column from the table
        function grabCol(j, col) {
            var $col = $(col),
                $text = $col.text();

            return $text.replace('"', '""'); // escape double quotes
        }
    }

    // This must be a hyperlink
    $('#export').click(function(event) {
        // var outputFile = 'export'
        var outputFile =
            window.prompt("What do you want to name your output file (Note: This won't have any effect on Safari)") ||
            'export';
        outputFile = outputFile.replace('.csv', '') + '.csv';

        // CSV
        exportTableToCSV.apply(this, [ $('#dvData > table'), outputFile ]);

        // IF CSV, don't do event.preventDefault() or return false
        // We actually need this to be a typical hyperlink
    });
});

var filter = document.getElementById('filter');

// Filter event
filter.addEventListener('keyup', filterItems);

// Filter Items
function filterItems(e) {
    // convert text to lowercase
    var text = e.target.value.toLowerCase();
    // Get lis
    var items = itemList.getElementsByTagName('dl');
    //var items = itemList.querySelectorAll('dd');
    // Convert to an array
    Array.from(items).forEach(function(item) {
        var itemName = item.firstChild.textContent;
        if (itemName.toLowerCase().indexOf(text) != -1) {
            item.style.display = 'block';
        } else {
            item.style.display = 'none';
        }
    });
}

嗨,我正尝试从我的哈巴狗文件下载到CSV文件。我想以三个标题名称,版本和存储库作为列下载到csv。然后对于每一列,我希望有一个包含(名称d.name,版本d.version,存储库d.repo_id中)的行列表。我正在使用此代码,但是当我尝试下载时,csv为空。我是帕格的新手,我想知道是否有办法解决这个问题,是否有人可以帮助我,因为我已经花了很多时间来解决这个问题。

0 个答案:

没有答案