Blame view

src/views/table/complex-table.vue 15.6 KB
d7d9c38c2   Adam   auto commit the c...
1
2
3
  <template>
    <div class="app-container">
      <div class="filter-container">
a6e433928   Adam   auto commit the c...
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
        <el-input
          v-model="listQuery.title"
          :placeholder="$t('table.title')"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="handleFilter"
        />
        <el-select
          v-model="listQuery.importance"
          :placeholder="$t('table.importance')"
          clearable
          style="width: 90px"
          class="filter-item"
        >
          <el-option
            v-for="item in importanceOptions"
            :key="item"
            :label="item"
            :value="item"
          />
d7d9c38c2   Adam   auto commit the c...
24
        </el-select>
a6e433928   Adam   auto commit the c...
25
26
27
28
29
30
31
32
33
34
35
36
37
        <el-select
          v-model="listQuery.type"
          :placeholder="$t('table.type')"
          clearable
          class="filter-item"
          style="width: 130px"
        >
          <el-option
            v-for="item in calendarTypeOptions"
            :key="item.key"
            :label="item.display_name+'('+item.key+')'"
            :value="item.key"
          />
d7d9c38c2   Adam   auto commit the c...
38
        </el-select>
a6e433928   Adam   auto commit the c...
39
40
41
42
43
44
45
46
47
48
49
50
        <el-select
          v-model="listQuery.sort"
          style="width: 140px"
          class="filter-item"
          @change="handleFilter"
        >
          <el-option
            v-for="item in sortOptions"
            :key="item.key"
            :label="item.label"
            :value="item.key"
          />
d7d9c38c2   Adam   auto commit the c...
51
        </el-select>
a6e433928   Adam   auto commit the c...
52
53
54
55
56
57
58
        <el-button
          v-waves
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >
d7d9c38c2   Adam   auto commit the c...
59
60
          {{ $t('table.search') }}
        </el-button>
a6e433928   Adam   auto commit the c...
61
62
63
64
65
66
67
        <el-button
          class="filter-item"
          style="margin-left: 10px;"
          type="primary"
          icon="el-icon-edit"
          @click="handleCreate"
        >
d7d9c38c2   Adam   auto commit the c...
68
69
          {{ $t('table.add') }}
        </el-button>
a6e433928   Adam   auto commit the c...
70
71
72
73
74
75
76
77
        <el-button
          v-waves
          :loading="downloadLoading"
          class="filter-item"
          type="primary"
          icon="el-icon-download"
          @click="handleDownload"
        >
d7d9c38c2   Adam   auto commit the c...
78
79
          {{ $t('table.export') }}
        </el-button>
a6e433928   Adam   auto commit the c...
80
81
82
83
84
85
        <el-checkbox
          v-model="showReviewer"
          class="filter-item"
          style="margin-left:15px;"
          @change="tableKey=tableKey+1"
        >
d7d9c38c2   Adam   auto commit the c...
86
87
88
89
90
91
92
93
94
95
96
97
98
99
          {{ $t('table.reviewer') }}
        </el-checkbox>
      </div>
  
      <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        @sort-change="sortChange"
      >
a6e433928   Adam   auto commit the c...
100
101
102
103
104
105
106
107
        <el-table-column
          :label="$t('table.id')"
          prop="id"
          sortable="custom"
          align="center"
          width="80"
          :class-name="getSortClass('id')"
        >
d7d9c38c2   Adam   auto commit the c...
108
109
110
111
          <template slot-scope="{row}">
            <span>{{ row.id }}</span>
          </template>
        </el-table-column>
a6e433928   Adam   auto commit the c...
112
113
114
115
116
        <el-table-column
          :label="$t('table.date')"
          width="150px"
          align="center"
        >
d7d9c38c2   Adam   auto commit the c...
117
118
119
120
          <template slot-scope="{row}">
            <span>{{ row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
a6e433928   Adam   auto commit the c...
121
122
123
124
        <el-table-column
          :label="$t('table.title')"
          min-width="150px"
        >
d7d9c38c2   Adam   auto commit the c...
125
          <template slot-scope="{row}">
a6e433928   Adam   auto commit the c...
126
127
128
129
            <span
              class="link-type"
              @click="handleUpdate(row)"
            >{{ row.title }}</span>
d7d9c38c2   Adam   auto commit the c...
130
131
132
            <el-tag>{{ row.type | typeFilter }}</el-tag>
          </template>
        </el-table-column>
a6e433928   Adam   auto commit the c...
133
134
135
136
137
        <el-table-column
          :label="$t('table.author')"
          width="110px"
          align="center"
        >
d7d9c38c2   Adam   auto commit the c...
138
139
140
141
          <template slot-scope="{row}">
            <span>{{ row.author }}</span>
          </template>
        </el-table-column>
a6e433928   Adam   auto commit the c...
142
143
144
145
146
147
        <el-table-column
          v-if="showReviewer"
          :label="$t('table.reviewer')"
          width="110px"
          align="center"
        >
d7d9c38c2   Adam   auto commit the c...
148
149
150
151
          <template slot-scope="{row}">
            <span style="color:red;">{{ row.reviewer }}</span>
          </template>
        </el-table-column>
a6e433928   Adam   auto commit the c...
152
153
154
155
        <el-table-column
          :label="$t('table.importance')"
          width="80px"
        >
d7d9c38c2   Adam   auto commit the c...
156
          <template slot-scope="{row}">
a6e433928   Adam   auto commit the c...
157
158
159
160
161
162
            <svg-icon
              v-for="n in +row.importance"
              :key="n"
              icon-class="star"
              class="meta-item__icon"
            />
d7d9c38c2   Adam   auto commit the c...
163
164
          </template>
        </el-table-column>
a6e433928   Adam   auto commit the c...
165
166
167
168
169
        <el-table-column
          :label="$t('table.readings')"
          align="center"
          width="95"
        >
d7d9c38c2   Adam   auto commit the c...
170
          <template slot-scope="{row}">
a6e433928   Adam   auto commit the c...
171
172
173
174
175
            <span
              v-if="row.pageviews"
              class="link-type"
              @click="handleFetchPv(row.pageviews)"
            >{{ row.pageviews }}</span>
d7d9c38c2   Adam   auto commit the c...
176
177
178
            <span v-else>0</span>
          </template>
        </el-table-column>
a6e433928   Adam   auto commit the c...
179
180
181
182
183
        <el-table-column
          :label="$t('table.status')"
          class-name="status-col"
          width="100"
        >
d7d9c38c2   Adam   auto commit the c...
184
185
186
187
188
189
          <template slot-scope="{row}">
            <el-tag :type="row.status | statusFilter">
              {{ row.status }}
            </el-tag>
          </template>
        </el-table-column>
a6e433928   Adam   auto commit the c...
190
191
192
193
194
195
        <el-table-column
          :label="$t('table.actions')"
          align="center"
          width="230"
          class-name="small-padding fixed-width"
        >
d7d9c38c2   Adam   auto commit the c...
196
          <template slot-scope="{row,$index}">
a6e433928   Adam   auto commit the c...
197
198
199
200
201
            <el-button
              type="primary"
              size="mini"
              @click="handleUpdate(row)"
            >
d7d9c38c2   Adam   auto commit the c...
202
203
              {{ $t('table.edit') }}
            </el-button>
a6e433928   Adam   auto commit the c...
204
205
206
207
208
209
            <el-button
              v-if="row.status!='published'"
              size="mini"
              type="success"
              @click="handleModifyStatus(row,'published')"
            >
d7d9c38c2   Adam   auto commit the c...
210
211
              {{ $t('table.publish') }}
            </el-button>
a6e433928   Adam   auto commit the c...
212
213
214
215
216
            <el-button
              v-if="row.status!='draft'"
              size="mini"
              @click="handleModifyStatus(row,'draft')"
            >
d7d9c38c2   Adam   auto commit the c...
217
218
              {{ $t('table.draft') }}
            </el-button>
a6e433928   Adam   auto commit the c...
219
220
221
222
223
224
            <el-button
              v-if="row.status!='deleted'"
              size="mini"
              type="danger"
              @click="handleDelete(row,$index)"
            >
d7d9c38c2   Adam   auto commit the c...
225
226
227
228
229
              {{ $t('table.delete') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
a6e433928   Adam   auto commit the c...
230
231
232
233
234
235
236
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
d7d9c38c2   Adam   auto commit the c...
237

a6e433928   Adam   auto commit the c...
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
      <el-dialog
        :title="textMap[dialogStatus]"
        :visible.sync="dialogFormVisible"
      >
        <el-form
          ref="dataForm"
          :rules="rules"
          :model="temp"
          label-position="left"
          label-width="70px"
          style="width: 400px; margin-left:50px;"
        >
          <el-form-item
            :label="$t('table.type')"
            prop="type"
          >
            <el-select
              v-model="temp.type"
              class="filter-item"
              placeholder="Please select"
            >
              <el-option
                v-for="item in calendarTypeOptions"
                :key="item.key"
                :label="item.display_name"
                :value="item.key"
              />
d7d9c38c2   Adam   auto commit the c...
265
266
            </el-select>
          </el-form-item>
a6e433928   Adam   auto commit the c...
267
268
269
270
271
272
273
274
275
          <el-form-item
            :label="$t('table.date')"
            prop="timestamp"
          >
            <el-date-picker
              v-model="temp.timestamp"
              type="datetime"
              placeholder="Please pick a date"
            />
d7d9c38c2   Adam   auto commit the c...
276
          </el-form-item>
a6e433928   Adam   auto commit the c...
277
278
279
280
          <el-form-item
            :label="$t('table.title')"
            prop="title"
          >
d7d9c38c2   Adam   auto commit the c...
281
282
283
            <el-input v-model="temp.title" />
          </el-form-item>
          <el-form-item :label="$t('table.status')">
a6e433928   Adam   auto commit the c...
284
285
286
287
288
289
290
291
292
293
294
            <el-select
              v-model="temp.status"
              class="filter-item"
              placeholder="Please select"
            >
              <el-option
                v-for="item in statusOptions"
                :key="item"
                :label="item"
                :value="item"
              />
d7d9c38c2   Adam   auto commit the c...
295
296
297
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('table.importance')">
a6e433928   Adam   auto commit the c...
298
299
300
301
302
303
            <el-rate
              v-model="temp.importance"
              :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
              :max="3"
              style="margin-top:8px;"
            />
d7d9c38c2   Adam   auto commit the c...
304
305
          </el-form-item>
          <el-form-item :label="$t('table.remark')">
a6e433928   Adam   auto commit the c...
306
307
308
309
310
311
            <el-input
              v-model="temp.remark"
              :autosize="{ minRows: 2, maxRows: 4}"
              type="textarea"
              placeholder="Please input"
            />
d7d9c38c2   Adam   auto commit the c...
312
313
          </el-form-item>
        </el-form>
a6e433928   Adam   auto commit the c...
314
315
316
317
        <div
          slot="footer"
          class="dialog-footer"
        >
d7d9c38c2   Adam   auto commit the c...
318
319
320
          <el-button @click="dialogFormVisible = false">
            {{ $t('table.cancel') }}
          </el-button>
a6e433928   Adam   auto commit the c...
321
322
323
324
          <el-button
            type="primary"
            @click="dialogStatus==='create'?createData():updateData()"
          >
d7d9c38c2   Adam   auto commit the c...
325
326
327
328
            {{ $t('table.confirm') }}
          </el-button>
        </div>
      </el-dialog>
a6e433928   Adam   auto commit the c...
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
      <el-dialog
        :visible.sync="dialogPvVisible"
        title="Reading statistics"
      >
        <el-table
          :data="pvData"
          border
          fit
          highlight-current-row
          style="width: 100%"
        >
          <el-table-column
            prop="key"
            label="Channel"
          />
          <el-table-column
            prop="pv"
            label="Pv"
          />
d7d9c38c2   Adam   auto commit the c...
348
        </el-table>
a6e433928   Adam   auto commit the c...
349
350
351
352
353
354
355
356
        <span
          slot="footer"
          class="dialog-footer"
        >
          <el-button
            type="primary"
            @click="dialogPvVisible = false"
          >{{ $t('table.confirm') }}</el-button>
d7d9c38c2   Adam   auto commit the c...
357
358
359
360
361
362
        </span>
      </el-dialog>
    </div>
  </template>
  
  <script>
a6e433928   Adam   auto commit the c...
363
364
365
366
367
368
  import {
    fetchList,
    fetchPv,
    createArticle,
    updateArticle
  } from '@/api/article'
d7d9c38c2   Adam   auto commit the c...
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
  import waves from '@/directive/waves' // waves directive
  import { parseTime } from '@/utils'
  import Pagination from '@/components/Pagination' // secondary package based on el-pagination
  
  const calendarTypeOptions = [
    { key: 'CN', display_name: 'China' },
    { key: 'US', display_name: 'USA' },
    { key: 'JP', display_name: 'Japan' },
    { key: 'EU', display_name: 'Eurozone' }
  ]
  
  // arr to obj, such as { CN : "China", US : "USA" }
  const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
    acc[cur.key] = cur.display_name
    return acc
  }, {})
  
  export default {
    name: 'ComplexTable',
    components: { Pagination },
    directives: { waves },
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'info',
          deleted: 'danger'
        }
        return statusMap[status]
      },
      typeFilter(type) {
        return calendarTypeKeyValue[type]
      }
    },
    data() {
      return {
        tableKey: 0,
        list: null,
        total: 0,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          importance: undefined,
          title: undefined,
          type: undefined,
          sort: '+id'
        },
        importanceOptions: [1, 2, 3],
        calendarTypeOptions,
a6e433928   Adam   auto commit the c...
419
420
421
422
        sortOptions: [
          { label: 'ID Ascending', key: '+id' },
          { label: 'ID Descending', key: '-id' }
        ],
d7d9c38c2   Adam   auto commit the c...
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
        statusOptions: ['published', 'draft', 'deleted'],
        showReviewer: false,
        temp: {
          id: undefined,
          importance: 1,
          remark: '',
          timestamp: new Date(),
          title: '',
          type: '',
          status: 'published'
        },
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: 'Edit',
          create: 'Create'
        },
        dialogPvVisible: false,
        pvData: [],
        rules: {
a6e433928   Adam   auto commit the c...
443
444
445
446
447
448
449
450
451
452
453
454
455
456
          type: [
            { required: true, message: 'type is required', trigger: 'change' }
          ],
          timestamp: [
            {
              type: 'date',
              required: true,
              message: 'timestamp is required',
              trigger: 'change'
            }
          ],
          title: [
            { required: true, message: 'title is required', trigger: 'blur' }
          ]
d7d9c38c2   Adam   auto commit the c...
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
        },
        downloadLoading: false
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true
        fetchList(this.listQuery).then(response => {
          this.list = response.data.items
          this.total = response.data.total
  
          // Just to simulate the time of the request
          setTimeout(() => {
            this.listLoading = false
          }, 1.5 * 1000)
        })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      handleModifyStatus(row, status) {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        row.status = status
      },
      sortChange(data) {
        const { prop, order } = data
        if (prop === 'id') {
          this.sortByID(order)
        }
      },
      sortByID(order) {
        if (order === 'ascending') {
          this.listQuery.sort = '+id'
        } else {
          this.listQuery.sort = '-id'
        }
        this.handleFilter()
      },
      resetTemp() {
        this.temp = {
          id: undefined,
          importance: 1,
          remark: '',
          timestamp: new Date(),
          title: '',
          status: 'published',
          type: ''
        }
      },
      handleCreate() {
        this.resetTemp()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      createData() {
a6e433928   Adam   auto commit the c...
522
        this.$refs['dataForm'].validate(valid => {
d7d9c38c2   Adam   auto commit the c...
523
524
          if (valid) {
            this.temp.id = parseInt(Math.random() * 100) + 1024 // mock a id
50760eab9   Adam   auto commit the c...
525
            this.temp.author = '秀野堂主'
d7d9c38c2   Adam   auto commit the c...
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
            createArticle(this.temp).then(() => {
              this.list.unshift(this.temp)
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      },
      handleUpdate(row) {
        this.temp = Object.assign({}, row) // copy obj
        this.temp.timestamp = new Date(this.temp.timestamp)
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      updateData() {
a6e433928   Adam   auto commit the c...
549
        this.$refs['dataForm'].validate(valid => {
d7d9c38c2   Adam   auto commit the c...
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
          if (valid) {
            const tempData = Object.assign({}, this.temp)
            tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
            updateArticle(tempData).then(() => {
              const index = this.list.findIndex(v => v.id === this.temp.id)
              this.list.splice(index, 1, this.temp)
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      },
      handleDelete(row, index) {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        this.list.splice(index, 1)
      },
      handleFetchPv(pv) {
        fetchPv(pv).then(response => {
          this.pvData = response.data.pvData
          this.dialogPvVisible = true
        })
      },
      handleDownload() {
        this.downloadLoading = true
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['timestamp', 'title', 'type', 'importance', 'status']
a6e433928   Adam   auto commit the c...
586
587
588
589
590
591
592
          const filterVal = [
            'timestamp',
            'title',
            'type',
            'importance',
            'status'
          ]
d7d9c38c2   Adam   auto commit the c...
593
594
595
596
597
598
599
600
601
602
          const data = this.formatJson(filterVal)
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: 'table-list'
          })
          this.downloadLoading = false
        })
      },
      formatJson(filterVal) {
a6e433928   Adam   auto commit the c...
603
604
605
606
607
608
609
610
611
        return this.list.map(v =>
          filterVal.map(j => {
            if (j === 'timestamp') {
              return parseTime(v[j])
            } else {
              return v[j]
            }
          })
        )
d7d9c38c2   Adam   auto commit the c...
612
613
614
615
616
617
618
619
      },
      getSortClass: function(key) {
        const sort = this.listQuery.sort
        return sort === `+${key}` ? 'ascending' : 'descending'
      }
    }
  }
  </script>