Blame view

ant-design-vue-jeecg/src/views/examples/form/advancedForm/AdvancedForm.vue 5.59 KB
肖超群 authored
1
2
3
<template>
  <div>
    <a-card class="card" title="仓库管理" :bordered="false">
肖超群 authored
4
      <repository-form ref="repository" :showSubmit="false"/>
肖超群 authored
5
6
    </a-card>
    <a-card class="card" title="任务管理" :bordered="false">
肖超群 authored
7
      <task-form ref="task" :showSubmit="false"/>
肖超群 authored
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    </a-card>

    <!-- table -->
    <a-card>
      <form :autoFormCreate="(form) => this.form = form">
        <a-table
          :columns="columns"
          :dataSource="data"
          :pagination="false"
        >
          <template v-for="(col, i) in ['name', 'workId', 'department']" :slot="col" slot-scope="text, record, index">
            <a-input
              :key="col"
              v-if="record.editable"
              style="margin: -5px 0"
              :value="text"
              :placeholder="columns[i].title"
              @change="e => handleChange(e.target.value, record.key, col)"
            />
            <template v-else>{{ text }}</template>
          </template>
          <template slot="operation" slot-scope="text, record, index">
            <template v-if="record.editable">
              <span v-if="record.isNew">
                <a @click="saveRow(record.key)">添加</a>
肖超群 authored
33
                <a-divider type="vertical"/>
肖超群 authored
34
35
36
37
38
39
                <a-popconfirm title="是否要删除此行?" @confirm="remove(record.key)">
                  <a>删除</a>
                </a-popconfirm>
              </span>
              <span v-else>
                <a @click="saveRow(record.key)">保存</a>
肖超群 authored
40
                <a-divider type="vertical"/>
肖超群 authored
41
42
43
44
45
                <a @click="cancel(record.key)">取消</a>
              </span>
            </template>
            <span v-else>
              <a @click="toggle(record.key)">编辑</a>
肖超群 authored
46
              <a-divider type="vertical"/>
肖超群 authored
47
48
49
50
51
52
              <a-popconfirm title="是否要删除此行?" @confirm="remove(record.key)">
                <a>删除</a>
              </a-popconfirm>
            </span>
          </template>
        </a-table>
肖超群 authored
53
54
55
        <a-button style="width: 100%; margin-top: 16px; margin-bottom: 8px" type="dashed" icon="plus"
                  @click="newMember">新增成员
        </a-button>
肖超群 authored
56
57
58
59
60
61
62
63
64
65
66
      </form>
    </a-card>

    <!-- fixed footer toolbar -->
    <footer-tool-bar>
      <a-button type="primary" @click="validate" :loading="loading">提交</a-button>
    </footer-tool-bar>
  </div>
</template>

<script>
肖超群 authored
67
68
69
import RepositoryForm from './RepositoryForm'
import TaskForm from './TaskForm'
import FooterToolBar from '@/components/tools/FooterToolBar'
肖超群 authored
70
肖超群 authored
71
72
73
74
75
76
77
78
79
80
81
export default {
  name: "AdvancedForm",
  components: {
    FooterToolBar,
    RepositoryForm,
    TaskForm
  },
  data() {
    return {
      description: '高级表单常见于一次性输入和提交大批量数据的场景。',
      loading: false,
肖超群 authored
82
肖超群 authored
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
      // table
      columns: [
        {
          title: '成员姓名',
          dataIndex: 'name',
          key: 'name',
          width: '20%',
          scopedSlots: {customRender: 'name'}
        },
        {
          title: '工号',
          dataIndex: 'workId',
          key: 'workId',
          width: '20%',
          scopedSlots: {customRender: 'workId'}
        },
        {
          title: '所属部门',
          dataIndex: 'department',
          key: 'department',
          width: '40%',
          scopedSlots: {customRender: 'department'}
        },
        {
          title: '操作',
          key: 'action',
          scopedSlots: {customRender: 'operation'}
        }
      ],
      data: [
        {
          key: '1',
          name: '小明',
          workId: '001',
          editable: false,
          department: '行政部'
        },
        {
          key: '2',
          name: '李莉',
          workId: '002',
          editable: false,
          department: 'IT部'
        },
        {
          key: '3',
          name: '王小帅',
          workId: '003',
          editable: false,
          department: '财务部'
        }
      ]
    }
  },
  methods: {
    handleSubmit(e) {
      e.preventDefault()
    },
    newMember() {
      this.data.push({
        key: '-1',
        name: '',
        workId: '',
        department: '',
        editable: true,
        isNew: true
      })
    },
    remove(key) {
      const newData = this.data.filter(item => item.key !== key)
      this.data = newData
    },
    saveRow(key) {
      let target = this.data.filter(item => item.key === key)[0]
      target.editable = false
      target.isNew = false
    },
    toggle(key) {
      let target = this.data.filter(item => item.key === key)[0]
      target.editable = !target.editable
肖超群 authored
163
    },
肖超群 authored
164
165
166
167
168
169
170
171
172
173
174
175
176
    getRowByKey(key, newData) {
      const data = this.data
      return (newData || data).filter(item => item.key === key)[0]
    },
    cancel(key) {
      let target = this.data.filter(item => item.key === key)[0]
      target.editable = false
    },
    handleChange(value, key, column) {
      const newData = [...this.data]
      const target = newData.filter(item => key === item.key)[0]
      if (target) {
        target[column] = value
肖超群 authored
177
178
        this.data = newData
      }
肖超群 authored
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
    },

    // 最终全页面提交
    validate() {
      this.$refs.repository.form.validateFields((err, values) => {
        if (!err) {
          this.$notification['error']({
            message: 'Received values of form:',
            description: values
          })
        }
      })
      this.$refs.task.form.validateFields((err, values) => {
        if (!err) {
          this.$notification['error']({
            message: 'Received values of form:',
            description: values
          })
        }
      })
肖超群 authored
199
200
    }
  }
肖超群 authored
201
}
肖超群 authored
202
203
204
</script>

<style lang="less" scoped>
肖超群 authored
205
206
207
.card {
  margin-bottom: 24px;
}
肖超群 authored
208
</style>