MICUU
心情
所有
图集
登录
搜索
原创
alpineJS的todoList实战fetch、post/get方法
米醋儿
发布于:2022-11-14
``` {extend name="public:adminbase" /} {block name="content"} <script defer src="{:config('web.cdn')}/static/admin/js/alpine.min.js"></script> <div class="container-fluid p-t-15"> <div class="row" x-data="todoList()" x-init="todoListInit()"> <div class="col-md-12"> <div class="card"> <div class="card-header"><h4>TodoList</h4></div> <div class="card-body"> <div class="input-group m-b-10"> <input x-model="title" x-on:keydown.enter="createTodoList()" type="text" class="form-control" placeholder="请输入代办事项" aria-describedby="basic-addon2"> <span class="input-group-addon btn" id="basic-addon2" @click="createTodoList()">回车立即添加</span> </div> </div> </div> <template x-for="(item,index) in list" :key="index"> <div class="card"> <div class="card-header"> <h4 x-html="`添加于:`+ item.create_time + `|更新于:` + item.update_time"></h4> <template x-if="item.status == 1"> <button class="btn btn-xs btn-success" type="button">已完成</button> </template> <template x-if="item.status != 1"> <button class="btn btn-xs btn-danger" type="button">未完成</button> </template> </div> <div class="card-body"> <address @click="changeItem(item)"> <template x-if="item.status == 1"> <del><strong x-text="item.content"></strong></del> </template> <template x-if="item.status != 1"> <strong x-text="item.content"></strong> </template> </address> </div> </div> </template> <nav> <ul class="pager"> <li @click="prePage()"><a href="javascript:;">上一页</a></li> <li @click="nextPage()"><a href="javascript:;">下一页</a></li> </ul> </nav> </div> </div> </div> {include file="public/afooter"} <script> let url = "{:url('getList')}"; let createUrl = "{:url('create')}"; let changeItemUrl = "{:url('changeItem')}"; //定义初始化函数 myFunInit function todoList() { return { title: '', list: { "name": "chushihua" }, current_page:1, total:'', per_page:'', last_page:'', //定义 x-init 的初始化函数 todoListInit() { console.log('this.current_page',this.current_page) fetch(url+`?page=`+this.current_page, { method: 'GET', headers: { 'Content-Type': 'application/json' } } ) .then(response => response.json()) .then(data => { this.list = data.data this.current_page = data.current_page this.last_page = data.last_page }) }, createTodoList() { if (this.title.length == 0) { return false; } let data = { "title": this.title } fetch(createUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) } ) .then(response => response.json()) .then(data => { if (data.code == 200) { success(data.msg); this.list.unshift({ "content": this.title, "status" : 0 }) this.title = '' } else { error(data.msg) } }) }, /** * 修改事项的状态 * @param item */ changeItem(item){ let postData = { "id":item.id, "status": item.status == 0 ?1:0 } fetch(changeItemUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(postData) } ) .then(response => response.json()) .then(data => { if (data.code == 200) { item.status = postData.status } else { error(data.msg) } }) }, prePage(){ this.current_page-- if(this.current_page <= 1){ this.current_page = 1; } this.todoListInit() }, nextPage(){ this.current_page++ if(this.current_page > this.last_page){ this.current_page = this.last_page } this.todoListInit() } } } </script> {/block} ```
注:原创不易,转载请注明出处(
https://micuu.com/new/3100.html
),本站所有资源来源于网络收集,如有侵权请联系QQ245557979进行清除。
最后修改与 2022-11-15
上一篇:
湖南张家界桑植新娘婚礼前一天晚上出轨,还...
下一篇:
在自己的后台简单用alpineJS写了个...
留言反馈
请先登录
问题反馈渠道,如有软件无法下载或者其他问题可反馈。【由于某种原因,目前留言不展示】
用户需要登录后才能留言反馈
立即留言
珍藏视频
10分钟高效燃脂
30天高效瘦脸操
5分钟缓解颈椎操
友人
微博
全民K歌
唱吧
今日头条
悠悠网
科技小锅盖
彼岸桌面
阮一峰
laravel社区
V2ex
掘金
更多>