0

I have a table like so for Outbound usage of Pharmacies

Pharmacy Name | Pharmacy ID | Call | SMS | Email | P | Total
------------------------------------------------------------
Test 1        | 123456      |  1   | 2   |  3    | 4 |  10
------------------------------------------------------------
Test 2        | 123457      |  1   | 2   |  3    | 4 |  10
-----------------------------------------------------------

I am trying to export this table to an excel sheet. How would I go about this when clicking a button and passing this data to my controller?

So far I have

$("tr").each(function () {

     alert( $("td").text());
});

But it is giving me all the tds.

I would like to be able to store the values of each row into an object.

1
  • there are several plugins to convert html table to csv, use your favorite search engine to find them Commented Sep 26, 2014 at 17:20

2 Answers 2

1

I think what you're looking for is:

$('tr').map(function(i, tr) {
  return $(tr).find('td').map(function(j, td) {
    return $(td).text();
  });
});

EDIT: how weird. Jquery seems to flatten them.

var parentArr = [];
$('tr').each(function() {
  var children = [];
  $(this).find('td').each(function(){
    children.push($(this).text());
  });
  parentArr.push(children);
});

this could definitely be solved a little more elegantly using ecmascript 5 or underscore/lodash methods.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that is what I am looking for, now I am trying to create an array of arrays. Basically with my output above it would have an array with 2 objects. I posted what I have below
0

Here is what I have so far

    var items;
    var data = [];

    $('tr').map(function (i, tr) {
        items = [];
        return $(tr).find('td').map(function (j, td) {

            items[j] = $(td).text();
            data[i].push(items[j]);
            return $(td).text();
        });

        alert(data);
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.