dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: JavaScript to read Text fill in a table

  1. #1
    Join Date
    Aug 2013
    Posts
    2

    JavaScript to read Text fill in a table

    I am trying to make the javascript read txt file which is working, and I want it to add each row in the txt file in a Column in the table and after 3 Columns start a new row in the table and so on. also while the javascript reading my txt and once it sees \n it moves to the next row in text file and add it to a column in the table. Then display it in the div

    Here is what I have

    test.html

    HTML Code:
        <script>
        if (window.XMLHttpRequest) {
        //for firefox, opera and safari browswers
        var txtFile = new XMLHttpRequest();
        }
        if (typeof XMLHttpRequest == "undefined")
        XMLHttpRequest = function () {
        try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
        catch (e) {}
        try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
        catch (e) {}
        try { return new ActiveXObject("Msxml2.XMLHTTP"); }
        catch (e) {}
        throw new Error("This browser does not support XMLHttpRequest.");
        };
        txtFile.open("GET","text.txt");
        txtFile.send(false);
        // var allText = txtFile.responseText;
        function clickyHere(){
        var info = txtFile.responseText;
        var infoArray = [];
        infoArray = info.split(/,|,+\s|\s+,\n|\r/);
        var tableCount = info.split(/,|\$/g).length;
        var rowCount = info.split(/\r|[\r]/g).length;
        var endLine = info.split(/\$/g).length;
        var newLine = info.replace(/\r\n/g,'\n').length;
        var columnTry = tableCount/endLine;
        document.write("<table border='1' columns='"+columnTry+"'>");
        var count;
        count = 0;
        for (i=0;i<infoArray.length;i++){
        x = infoArray[i];
        document.write("<td>"+x+" ");
        if (count != columnTry){
        count ++;
        }
        if (count >= columnTry){
        document.write("</tr><tr>");
        count = 0;
        }
        }
        document.write("</tr></table>");
        }
        </script>
        <div id="test" onload="clickyHere"></div>
    text.txt

    HTML Code:
        1, 1, aaa \n
        2, 2, aaa \n
        3, 3, aaa \n
        4, 4, aaa \n
        5, 5, aaa \n
        6, 6, aaa \n
        7, 7, aaa \n

  2. #2
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,143
    JavaScript cannot write to files. You'll need to use a server side language.

  3. #3
    Join Date
    Aug 2013
    Posts
    2
    I am not writing I am only reading the text file and viewing it in a table.

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,273
    It's probably possible to work directly on your info string with replace (and without split) to insert a HTML table in your page and to work with this DOM Table for your transformation.

    Don't use document.write !
    But insert your script at the end of the body after a <div id="result"></div>.
    Then a document.getElementById('result').innerHTML=myTableString; in the script is call when the div exists.

    A replace by '</td></tr><tr><td>' on each new lines (\r\n or\n or \r - see this page) and a replace by '</td><td>' on each comma, with CSV, or tab (\t), will probably build a good string to insert between '<table><tr><td>' and '</tr></table>';.

    All depends on the form of your text files. Some tests will probably allow it to finalize...
    Last edited by 007Julien; 08-10-2013 at 08:15 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles