vue自定义指令获取焦点及过滤器修改时间

 1 <template id="comp3">
 2     <div id="app">
 3         <model :list="selectedlist" :isactive="isActive" v-cloak @change="changeOverlay" @modify="add" @msearch="search"></model>
 4         <div class="panel panel-primary">
 5             <div class="panel-heading">
 6                 <h3 class="panel-title">网课管理</h3>
 7             </div>
 8             <div class="panel-body form-inline">
 9                 <label>
10                     搜索名称关键字:
11                     <!-- 自定义获取焦点 指令    -->
12                     <input type="text" class="form-control" v-focus v-model="keywords" id="search">&nbsp;&nbsp;
13                     <input type="button" value="搜索" class="btn btn-primary" />
14                 </label>&nbsp;&nbsp;&nbsp;&nbsp;
15                 <label>
16                     ID:
17                     <input type="text" class="form-control" v-model="id">
18                 </label>
19                 <label>
20                     网课名称:
21                     <input type="text" class="form-control" v-model="subClassifyName" @keyup.enter="add">
22                 </label>
23                 <input type="button" value="添加" class="btn btn-primary" @click="add()" />
24             </div>
25         </div>
26         <table class="table table-bordered table-hover table-striped">
27             <thead>
28                 <tr>
29                     <th class="text-center">网课ID</th>
30                     <th class="text-center">网课名称</th>
31                     <th class="text-center">创建时间</th>
32                     <th class="text-center">更新时间</th>
33                     <th class="text-center">操作</th>
34                 </tr>
35             </thead>
36             <tbody>
37                 <tr v-for="(item, index) of search(keywords)" :key="item.id">
38                     <td class="text-center" v-text="item.id"></td>
39                     <td class="text-center" v-text="item.subClassifyName"></td>
40                     <td class="text-center" v-text="">{{item.createTime | dateFormat}}</td>
41                     <td class="text-center" v-text="">{{item.updateTime | dateFormat}}</td>
42                     <td class="text-center">
43                         <!--data-toggle="modal" data-target="#update"-->
44                         <a href="#" @click="showOverlay(index)">
45                             <input type="button" data-toggle="modal" data-target="#compmodify_dialog" class="btn btn-primary" value="修改" />
46                         </a>
47                         <a href="#" @click.prevent="del(item.id)">
48                             <input type="button" class="btn btn-danger" value="删除" />
49                         </a>
50                     </td>
51                 </tr>
52             </tbody>
53         </table>
54         <!-- 底部索引 -->
55         <div class="box-pagination">
56             <ul class="pagination">
57                 <li><a href="#" aria-label="Previous" @click="getpageinfo(1)">首页</a></li>
58             </ul>
59             <ul class="pagination">
60                 <li><a href="#" @click="getprepageinfo()">&laquo;</a></li>
61             </ul>
62             <ul v-for="i in page" class="pagination">
63                 <li><a href="#" @click="getpageinfo(i)">{{i}}</a></li>
64             </ul>
65             <ul class="pagination">
66                 <li><a href="#" @click="getaftpageinfo()">&raquo;</a></li>
67             </ul>
68             <ul class="pagination">
69                 <li><a href="#" aria-label="Previous" @click="getpageinfo(page)">尾页</a></li>
70             </ul>
71         </div>
72     </div>
73 </template>
template
  1 // 网课管理 (comp3)
  2 var register = {
  3     template: '#comp3',
  4     data() {
  5         return {
  6             isActive: false,
  7             selected: -1,
  8             selectedlist: {},
  9             classifyName: '',
 10             keywords: '',
 11             id: '',
 12             subClassifyName: '',
 13             creatTime: '',
 14             updateTime: '',
 15 
 16             pagenum: '',
 17             page: '',
 18 
 19             list: [],
 20         }
 21     },
 22     props: ['clist'],
 23     methods: {
 24         // 修改数据
 25         showOverlay(index) {
 26             this.selected = index;
 27             this.selectedlist = this.list[index];
 28             this.changeOverlay();
 29         },
 30         // 点击保存按钮
 31         modify(arr) {
 32             if (this.selected > -1) {
 33                 Vue.set(this.list, this.selected, arr);
 34                 this.selected = -1;
 35             } else {
 36                 this.list.push(arr);
 37             }
 38             this.setSlist(this.list);
 39             this.changeOverlay();
 40         },
 41         // 添加 
 42         add: function() {
 43             //获取id subClassifyName 加入到数组
 44             var car = {
 45                 id: this.id,
 46                 subClassifyName: this.subClassifyName,
 47                 createTime: new Date()
 48             }
 49             this.list.push(car)
 50             /*重置id subClassifyName 让对话栏里值空*/
 51             this.id = this.subClassifyName = ''
 52         },
 53         // 发起get请求后通过.then获取服务器返回的内容
 54         getpageinfo(pagenum) {
 55             this.pagenum = pagenum;
 56             this.$http.get('http://10.0.41.100:8096/course/list/' + pagenum).then(function(result) {
 57                 this.list = result.body.list
 58                 this.page = Math.ceil(result.body.total / 5)
 59                 /*alert(Math.ceil(result.body.total/5))*/
 60             })
 61         },
 62         getprepageinfo() {
 63             if (this.pagenum > 0) {
 64                 this.pagenum = this.pagenum - 1;
 65             }
 66             /*alert(pagenum)*/
 67             this.$http.get('http://10.0.41.100:8096/course/list/' + this.pagenum).then(function(result) {
 68                 this.list = result.body.list
 69                 this.page = Math.ceil(result.body.total / 5)
 70             })
 71         },
 72         getaftpageinfo() {
 73             if (this.pagenum < this.page) {
 74                 this.pagenum = this.pagenum + 1;
 75             }
 76             /*alert(pagenum)*/
 77             this.$http.get('http://10.0.41.100:8096/course/list/' + this.pagenum).then(function(result) {
 78                 this.list = result.body.list
 79                 this.page = Math.ceil(result.body.total / 5)
 80             })
 81 
 82         },
 83         getinfo() {
 84             this.$http.get('http://10.0.41.100:8096/course/list/' + '1').then(function(result) {
 85                 this.list = result.body.list
 86                 this.page = Math.ceil(result.body.total / 5)
 87                 /*alert(Math.ceil(result.body.total/5))*/
 88             })
 89 
 90         },
 91         /*根据id找到数据用list的 splice方法删除*/
 92         del(id) {
 93             this.list.some((item, i) => {
 94                 if (item.id == id) {
 95                     this.list.splice(i, 1)
 96                     return true;
 97                 }
 98             })
 99         },
100         /*更改覆盖*/
101         changeOverlay() {
102             // 重置控件状态
103             this.selected = -1;
104             this.isActive = !this.isActive;
105             this.createTime = new Date();
106         },
107         /*查询*/
108         search(keywords) {
109             /*includes() 检索该字符串中是否包含指定字符  是  返回*/
110             return this.list.filter(item => {
111                 if (item.subClassifyName.includes(keywords)) {
112                     return item
113                 }
114             })
115         }
116     },
117     created() { // 模板加载完成后立刻请求数据
118         this.getinfo();
119     }
120 }
121 // 网课管理 的模态框
122 Vue.component('model', {
123     props: ['list', 'isactive', 'selectedlist', ],
124     template: "#compmodify",
125     computed: {
126         modifylist() {
127             return this.list;
128         }
129     },
130     methods: {
131         changeActive() {
132             this.$emit('change');
133         },
134         modify() {
135             this.$emit('add', this.modifylist);
136         }
137     }
138 });
vue
 1 /*1、日期过虑器--全局*/
 2 Vue.filter('dateFormat', function(dateStr, pattern = "") {
 3     // 根据给定字符串 得到特定时间  yyyy-mm-dd
 4     var dt = new Date(dateStr)
 5     var y = dt.getFullYear()
 6     var m = dt.getMonth() + 1 //month从0开始
 7     var d = dt.getDate()
 8         
 9     if(pattern.toLowerCase() === 'yyyy-mm-dd') { //toLowerCase() 转换成小写
10         return `${y}-${m}-${d}`
11     } else {
12         var hh = dt.getHours()
13         var mm = dt.getMinutes()
14         var ss = dt.getSeconds()
15         return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
16     }
17 })
18 /*2、自动获取光标--用Vue.directive()定义全局指令*/
19 Vue.directive('focus', {
20     inserted: function(el) { // 表示元素插入到DOM中的时候,会执行inserted函数
21         el.focus()
22     },
23 })
Js

 

 

posted @ 2019-11-08 18:10  鸣人卷~~  阅读(231)  评论(0编辑  收藏  举报