Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. var $TABLE = $('#table');
  2. var $BTN = $('#export-btn');
  3. var $EXPORT = $('#export');
  4.  
  5. jQuery.fn.pop = [].pop;
  6. jQuery.fn.shift = [].shift;
  7.  
  8. $BTN.click(function () {
  9. var $rows = $TABLE.find('tr:not(:hidden)');
  10. var headers = [];
  11. var datas = [];
  12.  
  13. // Get the headers (add special header logic here)
  14. $($rows.shift()).find('th:not(:empty)').each(function () {
  15. headers.push($(this).text());
  16. });
  17.  
  18. // Turn all existing rows into a loopable array
  19. $rows.each(function () {
  20. var $td = $(this).find('td');
  21. var h = {};
  22.  
  23. // Use the headers from earlier to name our hash keys
  24. headers.forEach(function (header, i) {
  25. h[header.replace(/\n\s+/g, "")] = $td.eq(i).text().replace(/\n\s+/g, "");
  26. });
  27. datas.push(h);
  28. console.log(h);
  29. });
  30.  
  31. // Output the result
  32. $EXPORT.text(JSON.stringify(datas));
  33.  
  34. $.ajax({
  35. url:'/preview/',
  36. type:'POST',
  37. contentType: "application/json; charset=utf-8",
  38. data:{
  39. json_respons: datas,
  40. },
  41. dataType: 'json',
  42. success: function(datas){
  43. console.log(datas);
  44. },
  45. error: function(datas){
  46. alert(datas);
  47. },
  48. })
  49. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement