@@ -8,6 +8,7 @@ var itemIdInput = document.getElementById('item-id');
8
8
var successDiv = document . getElementById ( 'success' ) ;
9
9
var reqForm = document . getElementById ( 'request-form' ) ;
10
10
var submitItemBtn = document . getElementById ( 'submit-item' ) ;
11
+ var allItemsBtn = document . getElementById ( 'all-items-btn' ) ;
11
12
12
13
function request ( url , method , cb ) {
13
14
var xhr = new XMLHttpRequest ( ) ;
@@ -107,6 +108,14 @@ searchBtn.addEventListener("click", function(e) {
107
108
}
108
109
} ) ;
109
110
111
+ allItemsBtn . addEventListener ( "click" , function ( e ) {
112
+ e . preventDefault ( ) ;
113
+
114
+
115
+ // requestData uses a callback populate/ musicPopulate to populate the DOM
116
+ request ( "/search?q=" , 'GET' , updateDom ) ;
117
+
118
+ } ) ;
110
119
111
120
function updateDom ( err , data ) {
112
121
if ( err ) {
@@ -118,26 +127,34 @@ function updateDom(err, data) {
118
127
var table = document . getElementById ( "items-table" ) ;
119
128
clearList ( table ) ;
120
129
121
- items . forEach ( function ( item ) {
122
- // adding our item names
123
- var row = document . createElement ( "tr" ) ;
124
- var name = document . createElement ( "td" ) ;
125
- var loanBtn = document . createElement ( 'button' ) ;
126
- loanBtn . textContent = 'borrow' ;
127
- loanBtn . setAttribute ( 'id' , item . id ) ;
128
- loanBtn . setAttribute ( 'onclick' , "borrow(this.id)" ) ;
129
- name . innerHTML = item . name ;
130
- row . appendChild ( name ) ;
131
-
132
- // adding our item descriptions
133
- var description = document . createElement ( "td" ) ;
134
- description . innerHTML = item . description ;
135
- row . appendChild ( description ) ;
136
- row . appendChild ( loanBtn ) ;
137
- // add everything to the table
138
- table . appendChild ( row ) ;
139
-
140
- } ) ;
130
+ if ( items . length > 0 ) {
131
+ items . forEach ( function ( item ) {
132
+ // adding our item names
133
+ var row = document . createElement ( "tr" ) ;
134
+ var name = document . createElement ( "td" ) ;
135
+ var loanBtn = document . createElement ( 'button' ) ;
136
+ loanBtn . textContent = 'borrow' ;
137
+ loanBtn . setAttribute ( 'id' , item . id ) ;
138
+ loanBtn . setAttribute ( 'onclick' , "borrow(this.id)" ) ;
139
+ name . innerHTML = item . name ;
140
+ row . appendChild ( name ) ;
141
+
142
+ // adding our item descriptions
143
+ var description = document . createElement ( "td" ) ;
144
+ description . innerHTML = item . description ;
145
+ row . appendChild ( description ) ;
146
+ row . appendChild ( loanBtn ) ;
147
+ // add everything to the table
148
+ table . appendChild ( row ) ;
149
+
150
+ } ) ;
151
+ }
152
+ else {
153
+ var errorMsg = document . createElement ( 'h3' ) ;
154
+ errorMsg . textContent = 'Sorry no items match that search' ;
155
+ table . appendChild ( errorMsg ) ;
156
+ }
157
+
141
158
}
142
159
}
143
160
0 commit comments