tableEditor.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // var table = document.createElement("table");
  2. // for (var i = 0; i < persons.length; i++) {
  3. // tr = document.createElement("tr");
  4. // var key = Object.keys(persons[i]);
  5. // for (var c = 0; c < key.length; c++) {
  6. // if(c < 3)
  7. // str += "<td>" + key[c] + ": " + persons[i][key[c]] + "</td>";
  8. // if(c >= 3 && key[c] in persons[i])
  9. // str += "<td>" + key[c] + ": " + persons[i][key[c]] + "</td>";
  10. // }
  11. // str += "</tr>"
  12. // }
  13. // str += "</table>";
  14. // document.write(str);
  15. var a = {
  16. name: "Ivan",
  17. surname: "Ivanov",
  18. }
  19. var b = {
  20. name: "Petr",
  21. surname: "Petrov",
  22. }
  23. var c = {
  24. name: "Alex",
  25. surname: "Sidarov",
  26. }
  27. a.age = 20;
  28. b.fathername = "Nikolai";
  29. c.sex = "male";
  30. var persons = [a,b,c,
  31. {
  32. name: "pasha",
  33. surname: "efimenko",
  34. age: 22
  35. }
  36. ]
  37. var element = document.createElement("div");
  38. document.body.appendChild(element);
  39. element.setAttribute("id","myId")
  40. var result = tableEditor(element, persons)
  41. console.log(result)
  42. function tableEditor(container, arr){
  43. function dblfunc(){
  44. var cont = document.createElement("input")
  45. cont.setAttribute("type","text");
  46. this.innerHTML = "";
  47. this.appendChild(cont);
  48. cont.ondblclick = inputDbl;
  49. }
  50. function inputDbl(){
  51. var val = this.value;
  52. var objKeyss = Object.keys(arr[this.parentElement.parentElement.rowIndex]);
  53. if(!(this.parentElement.cellIndex % 2)){
  54. arr[this.parentElement.parentElement.rowIndex][val] = this.parentElement.nextSibling.innerHTML;
  55. if(this.parentElement.cellIndex === 0)
  56. delete(arr[this.parentElement.parentElement.rowIndex][objKeyss[this.parentElement.cellIndex]]);
  57. else
  58. delete(arr[this.parentElement.parentElement.rowIndex][objKeyss[this.parentElement.cellIndex - 1]]);
  59. }
  60. if(this.parentElement.cellIndex % 2 > 0)
  61. arr[this.parentElement.parentElement.rowIndex][objKeyss[(this.parentElement.cellIndex - 1)/2]] = val;
  62. this.parentElement.ondblclick = function(){};
  63. this.parentElement.innerHTML = val;
  64. console.log(arr);
  65. this.parentElement.removeChild(this);
  66. console.log(arr);
  67. }
  68. var table = document.createElement("table");
  69. for (var i = 0; i < arr.length; i++) {
  70. var tr = document.createElement("tr");
  71. for (var j = 0; j < 3; j++) {
  72. var td1 = document.createElement("td");
  73. var td2 = document.createElement("td");
  74. var objKeys = Object.keys(arr[i])
  75. td1.innerHTML = objKeys[j] + ": ";
  76. td2.innerHTML = arr[i][objKeys[j]];
  77. td1.ondblclick = dblfunc;
  78. td2.ondblclick = dblfunc;
  79. tr.appendChild(td1);
  80. tr.appendChild(td2);
  81. }
  82. table.appendChild(tr);
  83. }
  84. container.appendChild(table);
  85. return arr;
  86. }