图书管理核心代码
渲染数据
axios({...}).then(result => {
const bookList = result.data.data
const htmlStr = bookList.map((item,index) =>{
return `<tr>
<td>${index +1}</td>
<td>${item.bookname}</td>
<td>${item.author}</td>
<td>${item.publisher}</td>
<td data-id=${item.id}>
<span class="del">删除</span>
<span class="edit">编辑</span>
</td>
</tr>`
}).join('')
document.querySelector('.list').innerHTML = htmlStr
})
新增数据
const addModalDom = document.querySelector('.add-modal')
const addModal = new bootstrap.Modal(addModalDom)
document.querySelector('add-btn').addEvenListener('click',() =>{
const addForm = document.querySelector('.add-form')
const bookObj = serialize(addForm,{hash:true,empty:true})
axios({...}).then(result =>{
getBoosList()
addForm.reset()
addModal.hide()
})
})
删除数据
document.querySelector('.list').addEvenListener('click',e =>{
if(e.target.classList.contains('del')){
const theId = e.target.parentNode.dataset.id
axios({...}).then(() =>{
getBookList()
})
}
})
编辑数据
const editDom = document.querySelector('.edit-modal')
const editModal = new bootstrap.Modal(editDom)
document.querySelector('.list').addEvenListener('click',e =>{
if if(e.target.classList.contains('edit')){
const theId = e.target.parentNode.dataset.id
axios({...}).then(result =>{
const bookObj = result.data.data
const keys = Object.keys(bookObj)
keys.forEach(key =>{
document.querySelector(`.edit-form .${key}`).value = bookObj[key]
})
})
editModal.show()
}
})