Loading ...
Sorry, an error occurred while loading the content.

trouble with plaintext DataSource

Expand Messages
  • Juan Leon
    I am trying to display some data in a spiffy DataTable. So far, the only way I have been able to successfully load data is by manually creating a JSON-style
    Message 1 of 2 , Apr 30 1:30 PM
    View Source
    • 0 Attachment
      I am trying to display some data in a spiffy DataTable. So far, the
      only way I have been able to successfully load data is by manually
      creating a JSON-style object and feeding it to the DataSource
      contructor.

      However, my real data is in plain-text. I can get it into JS memory,
      as either a string or standard numerically indexed array, but I cannot
      seem to get DataSource configured properly for it. I have tried
      passing it the location of the local text file, and the plaintext
      string variable. Both produce "Data Error" in the table, and then "no
      records found" when I click on a column header for sorting.

      output is a string in memory or a flat text file with this type of format:

      J Smith,j.smith@...
      J Doe,j.doe@...

      I call this method:

      function buildTable(output) {
      var myColumnHeaders = [
      {key:"name", abbr:"Name", sortable:true, resizeable:true},
      {key:"email", sortable:true, resizeable:true}
      ];

      var myColumnSet = new YAHOO.widget.ColumnSet(myColumnHeaders);
      var myDataSource = new YAHOO.util.DataSource(output);
      //have also tried: var myDataSource = new
      YAHOO.util.DataSource('addresses.txt');
      myDataSource.responseType = YAHOO.util.DataSource.TYPE_TEXT;


      myDataSource.responseSchema = {
      recordDelim: '\n',
      fieldDelim: ',',
      fields: ["name","email"]
      };

      var myDataTable = new YAHOO.widget.DataTable("basic", myColumnSet,
      myDataSource,{caption:"Email Contacts"});
      }
      }


      Anyone spot something clearly wrong?
      Thanks in advance for any pointers.
      JI
    • Juan Leon
      Hey folks, Since I could not figure out how to get the DataSource to take my plain text file or csv string, I wrote some pre-processing code to turn it into an
      Message 2 of 2 , Apr 30 7:31 PM
      View Source
      • 0 Attachment
        Hey folks,
        Since I could not figure out how to get the DataSource to take my plain text file or csv string, I wrote some pre-processing code to turn it into an array of associative arrays: This workaround is OK for me since I am only dealing with 5-8k of data.
        I thought I'd post it in case others may find this code useful.

        var outputLinesArray = output.split('\n');
        var addressBook = [];
        var tempLine = null;
        for (var i=0; i < outputLinesArray.length; i++) {
            tempLine = outputLinesArray[i].split(',');
            addressBook.push({name: tempLine[0], email:tempLine[1]});

        }

        I then setup rest of the code just like the in-memory array example in the documentation.

        Hope that helps.
        JI


        On 4/30/07, Juan Leon <jdash.lists@...> wrote:
        I am trying to display some data in a spiffy DataTable. So far, the
        only way I have been able to successfully load data is by manually
        creating a JSON-style object and feeding it to the DataSource
        contructor.

        However, my real data is in plain-text. I can get it into JS memory,
        as either a string or standard numerically indexed array, but I cannot
        seem to get DataSource configured properly for it. I have tried
        passing it the location of the local text file, and the plaintext
        string variable. Both produce "Data Error" in the table, and then "no
        records found" when I click on a column header for sorting.

        output is a string in memory or a flat text file with this type of format:

        J Smith,j.smith@...
        J Doe,j.doe@...

        I call this method:

        function buildTable(output) {
                        var myColumnHeaders = [
                                {key:"name", abbr:"Name", sortable:true, resizeable:true},
                                {key:"email",  sortable:true, resizeable:true}
                                ];

                        var myColumnSet = new YAHOO.widget.ColumnSet(myColumnHeaders);
                        var myDataSource = new YAHOO.util.DataSource(output);
        //have also tried: var myDataSource = new
        YAHOO.util.DataSource ('addresses.txt');
                        myDataSource.responseType = YAHOO.util.DataSource.TYPE_TEXT;


                        myDataSource.responseSchema = {
                                recordDelim: '\n',
                                fieldDelim: ',',
                                fields: ["name","email"]
                        };

                        var myDataTable = new YAHOO.widget.DataTable("basic", myColumnSet,
                        myDataSource,{caption:"Email Contacts"});
                }
        }


        Anyone spot something clearly wrong?
        Thanks in advance for any pointers.
        JI

      Your message has been successfully submitted and would be delivered to recipients shortly.