質問編集履歴
1
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -196,7 +196,7 @@
|
|
196
196
|
|
197
197
|
```
|
198
198
|
|
199
|
-
```
|
199
|
+
```index.js
|
200
200
|
|
201
201
|
const functions = require('firebase-functions')
|
202
202
|
|
@@ -252,278 +252,12 @@
|
|
252
252
|
|
253
253
|
### 試したこと
|
254
254
|
|
255
|
-
|
256
|
-
|
257
|
-
こ
|
255
|
+
javascriptは初心者で検索できるところは試したのですがうまくいきません。
|
256
|
+
|
257
|
+
<div id ="replies"></div> が表示するテキストをデータテーブルに表示するにはどうしたらいいのでしょうか。また、別の方法がありましたら教えてください。よろしくお願いいたします。
|
258
|
+
|
259
|
+
|
258
260
|
|
259
261
|
|
260
262
|
|
261
263
|
### 補足情報(FW/ツールのバージョンなど)
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
ここにより詳細な情報を記載してください。
|
266
|
-
|
267
|
-
### 前提・実現したいこと
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
現在、こちらを参考に(https://qiita.com/nacasora/items/713919f7a56024f5c59f)掲示板を作ろうとしており、書き込まれた情報をjquaryのデータテーブルに表示しようと思っております。色々試したのですが、データテーブル内に書き込みを表示できずにいます。
|
272
|
-
|
273
|
-
現在の状態:
|
274
|
-
|
275
|
-
書き込みページ https://myapplication-d9e40.firebaseapp.com/
|
276
|
-
|
277
|
-
表示ページ https://myapplication-d9e40.firebaseapp.com/link/index.html
|
278
|
-
|
279
|
-
### 発生している問題・エラーメッセージ
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
```
|
284
|
-
|
285
|
-
エラーメッセージ
|
286
|
-
|
287
|
-
```
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
### 該当のソースコード
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
```index.html
|
296
|
-
|
297
|
-
<script>
|
298
|
-
|
299
|
-
$(document).ready(function () {
|
300
|
-
|
301
|
-
var url = "https://myapplication-d9e40.firebaseio.com/simplebbs/posts";
|
302
|
-
|
303
|
-
// Jsonデータ取得
|
304
|
-
|
305
|
-
$.get(url, function (jsonData) {
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
});
|
310
|
-
|
311
|
-
});
|
312
|
-
|
313
|
-
</script>
|
314
|
-
|
315
|
-
<script>
|
316
|
-
|
317
|
-
jQuery(function($){
|
318
|
-
|
319
|
-
$("#foo-table").DataTable( {
|
320
|
-
|
321
|
-
data: data,
|
322
|
-
|
323
|
-
columns: [
|
324
|
-
|
325
|
-
{ data: 'content' },
|
326
|
-
|
327
|
-
{ data: 'date' },
|
328
|
-
|
329
|
-
{ data: 'locate0' },
|
330
|
-
|
331
|
-
{ data: 'locate1' },
|
332
|
-
|
333
|
-
{ data: 'locate2' },
|
334
|
-
|
335
|
-
{ data: 'name' },
|
336
|
-
|
337
|
-
]
|
338
|
-
|
339
|
-
});
|
340
|
-
|
341
|
-
});
|
342
|
-
|
343
|
-
</script>
|
344
|
-
|
345
|
-
</head>
|
346
|
-
|
347
|
-
<body>
|
348
|
-
|
349
|
-
<div id ="replies"></div>
|
350
|
-
|
351
|
-
<table id="foo-table" class="table table-bordered">
|
352
|
-
|
353
|
-
<thead>
|
354
|
-
|
355
|
-
<tr><th>名前</th>
|
356
|
-
|
357
|
-
<th>日付</th>
|
358
|
-
|
359
|
-
<th>内容</th>
|
360
|
-
|
361
|
-
<th>県</th>
|
362
|
-
|
363
|
-
<th>市</th>
|
364
|
-
|
365
|
-
<th>詳細</th></tr>
|
366
|
-
|
367
|
-
</thead>
|
368
|
-
|
369
|
-
<tbody>
|
370
|
-
|
371
|
-
</tbody>
|
372
|
-
|
373
|
-
</table>
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
<script src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
|
380
|
-
|
381
|
-
<script src="//cdn.datatables.net/1.10.16/js/dataTables.bootstrap4.min.js"></script>
|
382
|
-
|
383
|
-
</body>
|
384
|
-
|
385
|
-
</html>
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
```
|
390
|
-
|
391
|
-
```main.js
|
392
|
-
|
393
|
-
const main = ()=>
|
394
|
-
|
395
|
-
firebase.database().ref('/simplebbs/posts').limitToLast(10).on('value', snapshot=>{
|
396
|
-
|
397
|
-
const posts = snapshot.exists() ? snapshot.val() : {}
|
398
|
-
|
399
|
-
let html = ''
|
400
|
-
|
401
|
-
for(const [id, {name, content, date, locate0, locate1, locate2}] of Object.entries(posts).reverse())
|
402
|
-
|
403
|
-
html += makeReply(id, name, content, date, locate0, locate1, locate2)
|
404
|
-
|
405
|
-
document.querySelector('#replies').innerHTML = html
|
406
|
-
|
407
|
-
})
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
const makeReply = (id, name, content, locate0, locate1, locate2, date) => `<div class="reply">
|
412
|
-
|
413
|
-
<div class="head">Name: ${name} <span class="date">${date}</span></div>
|
414
|
-
|
415
|
-
<div class="content">${content}</div>
|
416
|
-
|
417
|
-
<div class="locate0">${locate0}</div>
|
418
|
-
|
419
|
-
<div class="locate1">${locate1}</div>
|
420
|
-
|
421
|
-
<div class="locate2">${locate2}</div>
|
422
|
-
|
423
|
-
<button class="delete" onclick="deleteReply('${id}')">delete</button> </div>`
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
const postReply = ()=> post('/api/post', {
|
428
|
-
|
429
|
-
name: document.querySelector('#reply-name').value,
|
430
|
-
|
431
|
-
content: document.querySelector('#reply-body').value,
|
432
|
-
|
433
|
-
key: document.querySelector('#reply-key').value,
|
434
|
-
|
435
|
-
locate0: document.querySelector('#reply-locate0').value,
|
436
|
-
|
437
|
-
locate1: document.querySelector('#reply-locate1').value,
|
438
|
-
|
439
|
-
locate2: document.querySelector('#reply-locate2').value,
|
440
|
-
|
441
|
-
}).then(e=>{ document.querySelector('#reply-body').value='' })
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
const deleteReply = id => post('/api/delete', {id, key: prompt('key?') || ''})
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
const post = (path, jsonData) => fetch(path, {
|
450
|
-
|
451
|
-
method: 'POST', body: JSON.stringify(jsonData),
|
452
|
-
|
453
|
-
headers: {'Accept': 'application/json', 'Content-Type': 'application/json'},
|
454
|
-
|
455
|
-
})
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
document.addEventListener('DOMContentLoaded', main)
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
```
|
464
|
-
|
465
|
-
```index.js
|
466
|
-
|
467
|
-
const functions = require('firebase-functions')
|
468
|
-
|
469
|
-
const admin = require('firebase-admin')
|
470
|
-
|
471
|
-
admin.initializeApp(functions.config().firebase)
|
472
|
-
|
473
|
-
const db = admin.database()
|
474
|
-
|
475
|
-
const rep = s=> s.replace(/</g, '<').replace(/>/g, '>').replace(/\n/g,'<br>')
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
exports.post = functions.https.onRequest((request, response)=>{
|
480
|
-
|
481
|
-
const {name, content, key, locate0, locate1, locate2} = request.body
|
482
|
-
|
483
|
-
const date = new Date().toLocaleDateString()
|
484
|
-
|
485
|
-
db.ref('/simplebbs/posts').push({name:rep(name), content:rep(content), locate0:rep(locate0), locate1:rep(locate1), locate2:rep(locate2), date})
|
486
|
-
|
487
|
-
.then(e=> db.ref(`/simplebbs/keys/${e.key}`).set(key))
|
488
|
-
|
489
|
-
.then(e=> response.status(200).end())
|
490
|
-
|
491
|
-
})
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
exports.delete = functions.https.onRequest((request, response)=>{
|
496
|
-
|
497
|
-
const {id, key} = request.body
|
498
|
-
|
499
|
-
db.ref(`/simplebbs/keys/${id}`).once('value').then(sKey=>{
|
500
|
-
|
501
|
-
if(!sKey.exists() || sKey.val() !== key)
|
502
|
-
|
503
|
-
return response.status(400).send('invalid id or incorrect key').end()
|
504
|
-
|
505
|
-
db.ref(`/simplebbs/posts/${id}`).remove()
|
506
|
-
|
507
|
-
.then(e=> sKey.ref.remove())
|
508
|
-
|
509
|
-
.then(e=> response.status(200).end())
|
510
|
-
|
511
|
-
})
|
512
|
-
|
513
|
-
})
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
```
|
518
|
-
|
519
|
-
### 試したこと
|
520
|
-
|
521
|
-
javascriptは初心者で検索できるところは試したのですがうまくいきません。
|
522
|
-
|
523
|
-
<div id ="replies"></div> が表示するテキストをデータテーブルに表示するにはどうしたらいいのでしょうか。また、別の方法がありましたら教えてください。よろしくお願いいたします。
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
### 補足情報(FW/ツールのバージョンなど)
|