Solved

jquery data table that exports to csv?

Posted on 2010-08-13
5
1,931 Views
Last Modified: 2012-05-10
Hello,

Anyone know of a good jquery data table plugin that gives you an option to select columns of data to export?  I would like to export an email column that contains email addresses so that I can create a mass email with all my contacts. I'm guessing that a csv export is the best option, but I'm looking for good ideas?
0
Comment
Question by:cgray1223
  • 3
  • 2
5 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33434872
Not a plugin, save the following as cgray1223.php

<?php
    function add_key_value($key,$value){
        $file = "emails.txt";
        $fh = fopen($file, 'a') or die("can't open file");
        $stringData = "$key=$value\r\n";
        fwrite($fh, $stringData);
        fclose($fh);
    }
    
    if(isset($_POST["emails"])) {
        foreach($_POST[emails] as $email) {
            add_key_value($email,$email);
        }
        echo "happy end!";
    }
    else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script language="javascript">
    $(document).ready(function() {
        $("th").click(function() {
            var json = {emails:[]};
            var columnNumber = $(this).parent().children().index($(this))
             $(this).parents("table").children("tbody").children("tr").each(function() {
                json.emails[json.emails.length] = $(this).children("td:eq(2)").text();
            });
            $.post("cgray1223.php", json, function(data) { $("#div2update").html(data); });
        });
    });
</script>
</head>
<body>
<table border="2">
<thead>
    <tr><th>First Name</th><th>Last Name</th><th>mail</th></tr>
</thead>
<tbody>
    <tr><td>Bob</td><td>Sponge</td><td>bob.sponge@hotmail.com</td></tr>
    <tr><td>Bob</td><td>Dylan</td><td>bob.dylan@hotmail.com</td></tr>
</tbody>
</table>
<div id="div2update"></div>
</body>
</html>
<?     
    }
?>

Open in new window

0
 

Author Comment

by:cgray1223
ID: 33438496
This is very nice thanks for the help!  Do you have any ideas on how to pull in data like the below and populate the table dynamically?

I have data stored as below in a file mailinglist.properties:
test@gmail1.com=test@gmail.com,first name,last name
test@gmail2.com=test@gmail.com,first name,last name
test@gmail3.com=test@gmail.com,first name,last name
.....
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 500 total points
ID: 33438576
check this :

            $.get("mailinglist.properties",function(data) {
                  $.map(data.split("\n"),function(row) {
                        var item = row.split(",");
                        if(item.length == 3) {
                              $("#tableID").append("" + item[0] + "" + item[1] + "" + item[2] + "");      
                        }
                  });
            })

test page :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script language="javascript">
	$(document).ready(function() {
		$.get("mailinglist.properties",function(data) {
			$.map(data.split("\n"),function(row) {
				var item = row.split(",");
				if(item.length == 3) {
					$("#tableID").append("<tr><td>" + item[0] + "</td><td>" + item[1] + "</td><td>" + item[2] + "</td></tr>");	
				}
			});
		})
	});
</script>
</head>
<body>
<table id="tableID" border="1">
</table>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:cgray1223
ID: 33442957
Thanks!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33442959
You're welcome! Thanks for the points!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question