[js] import file

https://www.youtube.com/watch?v=kJT-iMYMPKg&ab_channel=neilscodetutorials


<script src=".../jquery.min.js"></script>

<div id="buttondiv" >

  <input type="file" id="inputfile" />

  <input type="button" id="viewfile" value="Import file" />

</div>

<div class="container" id="container">

  <table class="gridtable" id="tableMain">

    <thead>

       <tr class="tableheader">

          <th> column1</th>

          <th> column2</th>

          <th> column3</th>

          <th> column4</th>

       </tr>

     </thead>

  </table>

</div>


<script>

   $(document).ready(function(){

     $('#viewfile').click(function(){

         var rdr = new FileReader();

          

         rdr.onload= function(e){

          //get the row into a array

            var therows =  e.target.result.split("\n");

                console.log(therows);


               //loop through the rows

          for(var row=0; row<therows.length;row++){

            //build a new table row

            var newrow="";

            //get the columns into an array

            var columns = therows[row].split(",");

            //get number of columns

            var colcount = columns.length;

            

            if(colcount!=4){

                newrow="<tr><td>incorrect number of columns</td> <td></td> <td></td> <td></td></tr>"

             }else{

                  newrow="<tr><td>" +columns[0]+"</td><td>"+columns[1]+"</td><td>"+columns[2]+"</td<td>"+columns[3]+"

                       </td>";   

             }

            //  $('#tableMain').append(newrow);

              $('#dg').datagrid('appendRow',{

            code: 'new name',

              name: 'test',

          }


             }

          rdr.readAsText($("#inputfile")[0].files[0]);

         


      });

   


});

</script>

留言

這個網誌中的熱門文章

考績被打差了 輕率離職會更傷

Arrays - DS (Reverse array) [Easy]

WireMock