schangxiang@126.com
2025-09-10 3d43ffa3152110b7823f9fa6320c08a6ae02358a
1
2
3
4
5
6
7
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<template>
  <div class="manager-form-container">
    <div class="button-container-title">按钮设计区</div>
    <!--按钮设置-->
    <manager-form-button-group :buttons.sync="data.dataListOptions.buttons" :config-type.sync="currentConfigType" :select.sync="selectWidget"></manager-form-button-group>
 
    <!--表格设置-->
    <div class="button-container-title margin-top-5">
      字段设计区
      <el-button type="text" size="mini" class="btn-add-all" @click="btnAddAll">全部加入</el-button>
      <el-button type="text" size="mini" class="btn-add-all" @click="btnClearAll">清空</el-button>
    </div>
    <draggable v-model="data.dataListOptions.fields" :options="{group:'people', ghostClass: 'ghost', animation:300, handle:'.handle', chosenClass:'chosen-item'}" tag="ul" class="draggable-main" @end="handleMoveEnd" @add="handleWidgetAdd" @move="handleWidgetStart" @clone="handleWidgetClone">
      <template v-for="(element, index) in data.dataListOptions.fields">
        <manager-form-item v-if="element && element.key" :key="element.key" :element="element" :select.sync="selectWidget" :index="index" :fields="data.dataListOptions.fields" :config-type.sync="currentConfigType"></manager-form-item>
      </template>
    </draggable>
  </div>
</template>
 
<script>
import Draggable from "vuedraggable";
import ManagerFormItem from "./ManagerFormItem";
import ManagerFormButtonGroup from "./ManagerFormButtonGroup";
 
export default {
  components: {
    Draggable,
    ManagerFormItem,
    ManagerFormButtonGroup
  },
  props: {
    data: {
      type: Object,
      default: () => {
        return {};
      }
    },
    select: {
      type: Object,
      default: () => {
        return {};
      }
    },
    configType: {
      type: String,
      default: null
    },
    // 主表字段
    fieldComponents: {
      type: Array,
      default: () => {
        return [];
      }
    }
  },
  data() {
    return {
      selectWidget: this.select
    };
  },
  computed: {
    currentConfigType: {
      get: function() {
        return this.configType;
      },
      set: function(val) {
        this.$emit("update:configType", val);
      }
    }
  },
  watch: {
    select(val) {
      this.selectWidget = val;
    },
    "data.dataListOptions.buttons": {
      handler(val) {
        // console.log(JSON.stringify(this.data.dataListOptions.buttons));
      },
      deep: true
    },
    selectWidget: {
      handler(val) {
        this.$emit("update:select", val);
      },
      deep: true
    }
  },
  methods: {
    handleMoveEnd({ newIndex, oldIndex }) {
      // console.log('index', newIndex, oldIndex)
    },
    handleWidgetStart(index) {
      // console.log(index, '#####')
    },
    handleWidgetClone(index) {
      // console.log(index, '#####')
    },
    handleSelectWidget(index) {
      this.selectWidget = this.data.dataListOptions.fields[index];
    },
    handleWidgetAdd(evt) {
      const newIndex = evt.newIndex;
      // 为拖拽到容器的元素添加唯一 key
      const key = Date.parse(new Date()) + "_" + Math.ceil(Math.random() * 99999);
      var field = this.data.dataListOptions.fields[newIndex];
      this.$set(this.data.dataListOptions.fields, newIndex, {
        prop: field.options.prop,
        label: field.label,
        searchRowNo: field.searchRowNo,
        dataType: field.options.dataType,
        sortable: false,
        type: "input",
        hidden: false,
        isQuickSearch: false,
        key
      });
 
      this.selectWidget = this.data.dataListOptions.fields[newIndex];
    },
    // 添加全部列表字段
    btnAddAll() {
      this.fieldComponents
        .filter(field => {
          return (
            !field.options.prop.endsWith("Id") &&
            !field.options.prop.endsWith("ID") &&
            !this.data.dataListOptions.fields.some(item => {
              return item.prop === field.options.prop;
            })
          );
        })
        .forEach(field => {
          const newIndex = this.data.dataListOptions.fields.length;
          // 为拖拽到容器的元素添加唯一 key
          const key = Date.parse(new Date()) + "_" + Math.ceil(Math.random() * 99999);
          this.$set(this.data.dataListOptions.fields, newIndex, {
            prop: field.options.prop,
            label: field.label,
            searchRowNo: field.searchRowNo,
            dataType: field.options.dataType,
            sortable: false,
            type: "input",
            hidden: false,
            isQuickSearch: false,
            key
          });
          var newField = this.data.dataListOptions.fields[newIndex];
          switch (newField.dataType) {
            case "datetime":
              newField.width = 120;
              break;
          }
          if (newField.prop.endsWith("Code")) {
            newField.width = 120;
          }
        });
 
      // 选中第一个
      this.currentConfigType = "ManagerConfig";
      this.selectWidget = this.data.dataListOptions.fields[0];
    },
    // 清空全部列表字段
    btnClearAll() {
      this.$confirm("确定要全部列表字段, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.data.dataListOptions.fields = [];
          this.selectWidget = null;
          this.$message.error("清空完毕");
        })
        .catch(() => {
          this.$message.info("已取消");
        });
    }
  }
};
</script>
 
<style lang="scss" scoped>
.btn-add-all {
  padding: 2px 0px 2px 10px;
}
</style>