Guest User

Untitled

a guest
Aug 28th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>JS Class</title>
  4. </head>
  5. <body>
  6. <h1>erg</h1>
  7. Search: <input type="text"></input><button>Find</button>
  8. <table class="output">
  9. </table>
  10. <script>
  11. contact = {
  12. protoContact : {
  13. toString : function() {
  14. return this.first + " " + this.last + " " + this.email;
  15. }
  16. },
  17.  
  18. init : function() {
  19. this.Contact.prototype = this.protoContact;
  20. },
  21.  
  22. Contact : function() {},
  23.  
  24. items : [],
  25. seed : 0,
  26.  
  27. build : function(args) {
  28. var newObj = new this.Contact();
  29. for (var k in args)
  30. newObj[k] = args[k];
  31. newObj.id = this.seed++;
  32. this.items.push(newObj);
  33. },
  34.  
  35. find : function(field, value) {
  36. if (value === undefined) {
  37. value = field;
  38. field = "last";
  39. }
  40. for (var i=0; i<this.items.length; i++) {
  41. if (this.items[i][field] == value)
  42. return this.items[i];
  43. }
  44. },
  45.  
  46. testdata : function() {
  47. this.build({first:"Joe1", last:"Smith1", email:"joe1@gmail.com"});
  48. this.build({first:"Joe2", last:"Smith2", email:"joe2@gmail.com"});
  49. this.build({first:"Joe3", last:"Smith3", email:"joe3@gmail.com"});
  50. }
  51. };
  52.  
  53. contact.init();
  54. contact.testdata();
  55. document.querySelector("button").addEventListener("click", function() {
  56. var c = contact.find(document.querySelector("input").value);
  57. var view = viewFromModel(c);
  58. view.style.background = "yellow";
  59. });
  60.  
  61. render = function(contacts) {
  62. var t = document.querySelector("table");
  63. t.innerHTML = "<tr><th>First</th><th>Last</th><th>Email</th>";
  64. for (var i=0; i<contacts.length; i++) {
  65. var row = document.createElement("tr");
  66. t.appendChild(row);
  67. ["first", "last", "email"].forEach(function(k) {
  68. var cell = document.createElement("td");
  69. cell.innerHTML = contacts[i][k];
  70. row.appendChild(cell);
  71. });
  72. row.setAttribute("class", "contact-" + contacts[i].id);
  73. }
  74.  
  75. };
  76.  
  77. viewFromModel = function(m) {
  78. return document.querySelector(".contact-" + m.id);
  79. };
  80.  
  81. render(contact.items);
  82. document.body.removeChild(document.querySelector("h1"));
  83. var head = document.createElement("h2");
  84. head.innerHTML = "Contacts!";
  85. document.body.insertBefore(head, document.body.firstChild);
  86.  
  87. </script>
  88.  
  89. </body>
  90. </html>
Add Comment
Please, Sign In to add comment