vue booklist

寫一個books接口,帶按價格排序,前端頁面創(chuàng)建完成,向后端發(fā)送請求,獲取圖書屬性,表格顯示在前端
前端使用監(jiān)聽屬性實現點擊價格正序和倒序排列
解決跨域問題

#setting.py
INSTALLED_APPS = [
    .......
    'corsheaders',
]
MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    ......
]
CORS_ORIGIN_ALLOW_ALL=True


image.png

前端代碼

    <table class="table table-striped">
    <thead><tr><th>title</th>
        <th>price
            <span class="btn btn-xs " @click="OrdingUpDown">
                <svg t="1686060486584" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2935" width="16" height="16">
                    <path d="M512 51.2L153.6 460.8h716.8L512 51.2z m0 921.6l358.4-409.6H153.6L512 972.8z" fill="#999999" p-id="2936"></path>
                </svg>
            </span>
        </th><th>publish</th> <th>author</th></tr></thead>
    <tbody><tr v-for="book in bookList"><td>{{book.title}}</td><td>{{book.price}}</td><td>{{book.publish}}</td> <td>{{book.authors}}</td></tr></tbody>
</table>
//根組件
let vm = new Vue({
    el: "#app",
    data: {
        bookList: [],
        ordering:true
    },
    methods: {
        OrdingUpDown(){
            this.ordering=!this.ordering
          },
    computed:{computed_sumprice(){var sum=0;this.checkGroup.forEach(function (good) {sum+=good.price*good.number});return sum;}},
    watch:{
        ordering(bool){
            console.log(bool)
            if(bool){
                this.bookList.sort(function(a, b){return a.price - b.price});
                console.log(this.bookList)
            }
            else {this.bookList.sort(function(b, a){return a.price - b.price});
                console.log(this.bookList)
            }
        }
    },
    created() {
        axios.get('http://127.0.0.1:8000/book/').then(res => {
                // console.log(res.data);
                this.bookList=res.data
            })
})
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容