完整代碼部分 (僅供參考哈):
1 <template> 2 <div> 3 <label prop="name"> 姓名: </label> 4 <Input v-model="companyName" id="pp" style="width: 120px" placeholder="請輸入" /> 5 <Button @click="search" type="primary" icon="ios-search">查詢</Button> 6 <Button type="primary" @click="addBus" icon="ios-add-circle-outline">新增</Button> 7 <Table :columns="columns1" :data="data1" size="small" ref="table"> 8 <template slot-scope="{ row }" slot="name"> 9 <strong>{{ row.name }}</strong> 10 </template> 11 <template slot-scope="{ row, index }" slot="action"> 12 <Button type="primary" size="small" style="margin-right: 5px" @click="show(index)">詳情</Button> 13 <Button type="warning" style="margin-right: 5px" size="small" @click="editBus(row,index)">編輯</Button> 14 <!-- 前面的小圖標會居中擋住文字 設置一下樣式就好了 style="text-align:left" --> 15 <Poptip 16 style="text-align:left" 17 confirm 18 title="您確定要刪除該信息?" 19 placement="left-end" 20 @on-ok="remove(index)" 21 @on-cancel="cancel1" 22 > 23 <Button type="error" size="small">刪除</Button> 24 <!-- @click="remove(index)" --> 25 </Poptip> 26 </template> 27 </Table> 28 <Modal 29 v-model="handleModal" 30 title="新增一條數據" 31 @on-ok="ok" 32 @on-cancel="cancel" 33 :footer-hide="true" 34 :mask-closable="false" 35 width="820" 36 @on-visible-change="handleReset('formValidate')" 37 > 38 <Form inline ref="formValidate" :model="formValidate" :label-width="80" :rules="ruleValidate"> 39 <FormItem label="姓名" prop="name"> 40 <Input v-model="formValidate.name" placeholder="請輸入姓名"></Input> 41 </FormItem> 42 <FormItem label="年齡" prop="age"> 43 <Input v-model="formValidate.age" placeholder="請輸入年齡"></Input> 44 </FormItem> 45 <FormItem label="愛好" prop="address"> 46 <Input v-model="formValidate.address" placeholder="請輸入你的愛好"></Input> 47 </FormItem> 48 <FormItem label="性別" prop="sex"> 49 <Input v-model="formValidate.sex" placeholder="別不男不女"></Input> 50 </FormItem> 51 <FormItem> 52 <!-- 提交的單擊事件 在下面的方法里面寫好 --> 53 <Button type="primary" @click="handleSubmit('formValidate')">提交</Button> 54 <Button @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button> 55 </FormItem> 56 </Form> 57 </Modal> 58 <!-- 分頁控件 --> 59 <div style="margin: 10px;overflow: hidden"> 60 <div style="float:left"> 61 <Page 62 :total="dataCount" 63 :page-size="pageSize" 64 show-total 65 :current="1" 66 @on-change="changepage" 67 ></Page> 68 </div> 69 </div> 70 </div> 71 </template> 72 <script> 73 import { Script } from "vm"; 74 let testData = { 75 histories: [ 76 { 77 name: "老王", 78 age: 18, 79 sex: "男", 80 address: "我喜歡吃蘋果" 81 }, 82 { 83 name: "張三", 84 age: 18, 85 sex: "男", 86 address: "我喜歡吃炸雞" 87 }, 88 { 89 name: "張歐", 90 age: 18, 91 sex: "男", 92 address: "我喜歡吃西瓜" 93 }, 94 { 95 name: "張五", 96 age: 18, 97 sex: "男", 98 address: "我喜歡吃阿彌光" 99 }, 100 { 101 name: "國基", 102 age: 18, 103 sex: "男", 104 address: "我喜歡吃烤鴨" 105 }, 106 { 107 name: "老王", 108 age: 18, 109 sex: "女", 110 address: "我喜歡吃蘋果" 111 }, 112 { 113 name: "張三", 114 age: 18, 115 sex: "女", 116 address: "我喜歡吃炸雞" 117 }, 118 { 119 name: "張歐", 120 age: 18, 121 sex: "女", 122 address: "我喜歡吃西瓜" 123 }, 124 { 125 name: "張五", 126 age: 18, 127 sex: "女", 128 address: "我喜歡吃阿彌光" 129 }, 130 { 131 name: "國基", 132 age: 18, 133 sex: "女", 134 address: "我喜歡吃烤鴨" 135 } 136 137 ] 138 }; 139 export default { 140 data() { 141 return { 142 // 這里需要設置原數據為空 143 // 好像跟實例化一樣 不然會出錯的 144 data1: [], 145 companyName: "", 146 // modal開始為false 147 handleModal: false, 148 149 // 這個對應form里面的數據不能少 名字不規范我就不改了 150 // columns1 和formvalidate 里面的命名要一樣 別亂了 151 formValidate: { 152 name: "", 153 flight: "", 154 begin: "", 155 destination: "" 156 }, 157 // 分頁 158 159 ajaxHistoryData: [], 160 // 初始化信息總條數 161 dataCount: 0, 162 // 每頁顯示多少條 163 pageSize: 5, 164 // 設置table的表頭 165 columns1: [ 166 { 167 title: "姓名", 168 key: "name" 169 }, 170 { 171 title: "年齡", 172 key: "age" 173 }, 174 { 175 title: "愛好", 176 key: "address" 177 }, 178 { 179 title: "性別", 180 key: "sex" 181 }, 182 { 183 title: "操作", 184 slot: "action", 185 width: 200, 186 align: "center" 187 } 188 ], 189 // 設置表格的數據 190 tableData: [] 191 }; 192 }, 193 // 方法 194 methods: { 195 // 查找按鈕 196 search() { 197 // 獲取表格數據 198 var len = testData.histories; 199 // 設置一個空的數組 200 var arr = []; 201 // 循環遍歷 202 for (var i in len) { 203 // if判斷 如果文本框中的值等於表格中name的值 輸出 204 if (len[i].name == this.companyName) { 205 arr.push(len[i]); 206 // 如果等於空就給他全部數據 207 } else if (this.companyName == "") { 208 this.data1 = testData.histories; 209 return; 210 } 211 } 212 // 將查找出來的數據給表格 213 this.data1 = arr; 214 }, 215 // 新增按鈕的單擊事件 216 addBus() { 217 this.handleModal = true; 218 this.modalTitle = "新增"; 219 }, 220 // 新增數據 221 handleSubmit(name) { 222 var self = this; 223 self.$refs[name].validate(valid => { 224 if (valid) { 225 var params = JSON.parse(JSON.stringify(self.formValidate)); 226 227 if (self.modalTitle == "新增") { 228 // 獲取需要渲染到頁面中的數據 229 self.$Message.success("新增成功!"); 230 self.data1.push(params); 231 } else { 232 this.$set(self.data1, self.itemIndex, params); 233 self.$Message.success("修改成功!"); 234 } 235 self.handleModal = false; 236 } else { 237 if (self.modalTitle == "新增") { 238 self.$Message.error("新增失敗!"); 239 } else { 240 self.$Message.error("修改失敗!"); 241 } 242 } 243 }); 244 }, 245 // 修改方法 246 editBus(item, index) { 247 this.handleModal = true; 248 this.modalTitle = "修改"; 249 this.itemIndex = index; 250 this.formValidate = JSON.parse(JSON.stringify(item)); 251 }, 252 // 刪除一條數據 253 remove(index) { 254 this.data1.splice(index, 1); 255 // on-click 方法 冒泡提示確定 256 this.$Message.success("刪除成功"); 257 }, 258 cancel1() { 259 this.$Message.info("取消刪除"); 260 }, 261 // 清除文本框 重置 262 handleReset(name) { 263 this.$refs[name].resetFields(); 264 }, 265 // 詳情顯示 266 show(index) { 267 this.$Modal.info({ 268 title: "查看詳情", 269 content: `姓名:${this.data1[index].name}<br>年齡:${this.data1[index].age} 270 <br>愛好:${this.data1[index].address}<br>性別:${this.data1[index].sex} 271 ` 272 }); 273 }, 274 // 分頁 275 handleListApproveHistory() { 276 // 保存取到的所有數據 277 278 this.ajaxHistoryData = testData.histories; 279 this.dataCount = testData.histories.length; 280 // 初始化顯示,小於每頁顯示條數,全顯,大於每頁顯示條數,取前每頁條數顯示 281 if (testData.histories.length < this.pageSize) { 282 this.data1 = this.ajaxHistoryData; 283 } else { 284 this.data1 = this.ajaxHistoryData.slice(0, this.pageSize); 285 } 286 }, 287 changepage(index) { 288 this.page = index; 289 var _start = (index - 1) * this.pageSize; 290 var _end = index * this.pageSize; 291 this.data1 = this.ajaxHistoryData.slice(_start, _end); 292 } 293 294 }, 295 296 // 這個應該是加載事件 加載頁面的時候就調用 297 // mounted() { 298 // // 頁面一加載就將數據給出給表格 299 // this.data1 = testData.histories; 300 // }, 301 created() { 302 this.handleListApproveHistory(); 303 } 304 305 306 }; 307 </script> 308 309 <style> 310 </style>