Recently I was working on a web page that was overburdened with tabular data. The original goal was to database the information and just read/format the data with some server side code (PHP). The problem was that I had FTP access to the site but no administrative logins to setup and configure MySQL or another data store. This felt like a good time to experiment with another great extension to jQuery: $.csv() [http://plugins.jquery.com/project/csv ].
Drop the source code into an existing .js file or create a new one and make sure you link to it after you've loaded jQuery to the page. Now lets take a look at our HTML.
<h4>Select a year <select id="year"> <option>1900</option> <option>1910</option> <option>1920</option> <option>1930</option> </select> </h4> <table class="tableContent"> <tr style="background-color:#516DB6; color: #fff;"> <th>NAME</th><th>M/F</th><th>BORN</th><th>RESIDENCE AT HH</th> </tr> </table>
Now for the controls that will cause the update. You can see in the screen shot that we want to keep the update controls very simple. When a visitor changes the drop down list we want to take the new SELECT box value and search for that data.
Now that we have our HTML setup, lets start writing some javascript. We need to start by loading the CSV document on page load or once the document is ready. For this we can use the jQuery AJAX method $.get().
$.get("documents/1900_Census.csv", function(data) { array = $.csv() (data); });
Now that we have our data in an Array, lets use jQuery to loop through each row.
$.each(array, function() { var row = String(this).split(","); }
The jQuery .each() function will setup each new row for our table. The line inside of this function splits the row into an array representing each column of data. We can later use those array elements to output our data.
Now that we can load the CSV data into our table. To clean things up a bit, lets put our code into a more complete function that will cause the table to fade out, load the data, then fade back in. Removing table rows with jQuery is simple, all we need to do is find every row but the first and call the .remove() method. Below you can see how we load the data once the document is ready and bind the change event of the drop down list.
<script type="text/javascript"> $(document).ready(function() { loadCSVFile(1900); $('#year').change(function() { loadCSVFile($('#year').val()); }); }); function loadCSVFile(year) { $('.tableContent').hide(); $('#loading').fadeIn(); $('.tableContent').find("tr:gt(0)").remove(); $.get('documents/' + year + '_Census.csv', function(data) { array = $.csv() (data); $.each(array, function() { var row = String(this).split(","); if (row[0] != "") { $('.tableContent').append("" + row[0] + "," + row[1] + ""); $('.tableContent').append("" + row[2] + "" + row[3] + "" + row[4] + ""); } }); $('.tableContent tr:odd').css('background-color', '#ffc'); $('.tableContent').fadeIn(); }); </script>
You can check out the final result here.
Related posts
Comments
April 22. 2009 13:18
nices article.
saç ekimi
May 7. 2009 11:20
Some solutions have been documented but I ran into other issues and ended up the solution a little further. Long story short, I ended up creating a custom Pipeline Component to strip out DTD’s and add Namespaces using regular expressions. At first I created a cXML cleaner component that worked specifically for my scenario. This worked fine but eventually I noticed that I was using a regular expression to find and replace the DTD which led me to think “Why can’t I use any regex I pass in and replace anything I want to?” So I did. I even made it open source so no one ever has to feel the anguish of having to develop custom BizTalk Pipeline Components, because it sucks. Bad.
saç ekimi istanbul
June 8. 2009 21:44
Some solutions have been documented but I ran into other issues and ended up the solution a little further. Long story short, I ended up creating a custom Pipeline Component to strip out DTD’s and add Namespaces using regular expressions.
kale kasa
July 1. 2009 17:52
I blog and I have a love and www.estetik-merkezleri.com/burun-estetigi.html burun estetiği burun estetiği hate relationship with the comment feature.Commenting can lead to unintended flame wars,and is guaranteed to require investing time in http://www.tupbebegim.org tüp bebek merkezi (tüp bebek merkezi) which has been getting better only slowly. Many of the early anti-comment spam techniques were quickly hacked. Not everyone has enough time to keep their blog entries updated, http://www.sacekimii.com saç ekimi ( saç ekimi ) and http://www.sacekimiestetik.com saç ekimi saç ekim merkezi let alone delete a hundred poker spam entries every day or http://www.gogusestetigii.org göğüs estetiği göğüs meme estetiği install the blog patch of the month. Admitedly this is a http://www.vajinismustedavisi.org vajinismus vajinismus tedavisi ilişkiye girememe developer website, but not all developers want to spend their time specificly in web development.Only when the blog technology is http://www.otoarackiralama.com araç kiralama kiralık oto araç kiralama kiralık araç effortless can we assert a lack of blog commenting means the blogger is http://www.ankaralazer.net/ankara-lazer-epilasyon ankara lazer epilasyon laser lazer epilasyon unwilling to take what he is dishing out.
vajinismus
Home
© Rapidparts, Inc 2008 | 2950 Walkent Ct. NW, Grand Rapids, MI 49544 | Phone 616.647.2500 | info@rpionline.com