Solved

jquery data table that exports to csv?

Posted on 2010-08-13
5
1,918 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now