待編輯
<script type="module">
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.9.2/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.9.2/firebase-analytics.js";
import {getDatabase ,ref,set,onValue,remove} from "https://www.gstatic.com/firebasejs/9.9.2/firebase-database.js"
const firebaseConfig = {
apiKey: "AIzaSyBzRAbWytJdZTZlU99rz9sTO1teBdZrVJw",
authDomain: "arduino-da53c.firebaseapp.com",
databaseURL: "https://arduino-da53c-default-rtdb.firebaseio.com",
projectId: "arduino-da53c",
storageBucket: "arduino-da53c.appspot.com",
messagingSenderId: "332226206687",
appId: "1:332226206687:web:1794175a601b7c3189426a",
measurementId: "G-587C0KPN7H"
};
// // Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getDatabase();
get_list();
function get_list(){ //取得realtime database內的資料
const starCountRef = ref(db,'webtest');
onValue(starCountRef, (snapshot) => {
var data = snapshot.val();
for(let i in data){
console.log(data[i]);
}
});
}
$(document).on('click','.delete',function(){ //var0是一個文字方塊,點擊刪除後刪除資料庫內的資料
alert($("#var0").html());
remove(ref(db, 'webtest/'+$("#var0").html()), {
});
get_list();
});
$("#submit").on('click',function(){ //點了submit之後,新增一筆資料
if($("#txt0").val()!=""){
set(ref(db, 'webtest/'+$("#txt0").val()), {
name: $("#txt0").val(),
temp1: $("#txt1").val(),
temp2: $("#txt2").val(),
temp3: $("#txt3").val()
});
}
$("#txt0").val(""); //清空文字方塊的內容
$("#txt1").val("");
$("#txt2").val("");
$("#txt3").val("");
get_list();
});
</script>