liuxiaolong
2019-05-06 3e0536f508aad49f743e7bfabca34e3980a1b6e2
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<template>
  <select-king
    ref="selectKing"
    v-model="selectVal"
    :disabled="isDataBase"
    optionsWidth="300px"
    placeholder="请选择分析"
  >
    <div class="type-library">
      <!-- 多选状态显示 start -->
      <div v-if="isCheckBox">
        <div class="library-list clearfix">
          <div
            class="float-left pl-1 alarm-item"
            v-for="(item,index) in SDKDicCheck"
            :key="index"
            v-show="item.value"
          >
            <span class="border-left px-1"></span>
            <el-checkbox class="alarm-checkBox mb-0" v-model="item.status"></el-checkbox>
            <a
              :title="item.name"
              :class="['alarm-name',item && item.status ?'active':'']"
              @click="selectCheck({ index, val:!!item.status})"
            >{{item.name}}</a>
          </div>
        </div>
        <div class="py-1 text-center">
          <b-btn variant="primary" size="sm" @click="submitCheck" class="mr10">确定</b-btn>
          <b-btn variant="secondary" size="sm" class="mr10" @click="cancel">取消</b-btn>
        </div>
      </div>
      <!-- 多选状态显示 end -->
      <!-- 单选状态显示 start -->
      <div v-else>
        <div class="library-list clearfix">
          <div class="float-left pl-1 alarm-item" v-for="(item,index) in SDKDic" :key="index">
            <span class="border-left px-1"></span>
            <a
              :title="item.name"
              :class="['alarm-name',item && item.status ?'active':'']"
              @click="selectCheck({ index, val:!!item.status},'radio')"
            >{{item.name}}</a>
          </div>
        </div>
        <div class="py-1 text-right" v-if="SDKDic.length>1">
          <b-btn variant="primary" class="mr10" @click="isCheckBox=!isCheckBox">多选</b-btn>
        </div>
      </div>
      <!-- 单选状态显示 end -->
    </div>
  </select-king>
</template>
<script>
import selectKing from '@/components/selectKing'
import { FormItem, Form, CheckboxGroup, Checkbox } from 'element-ui'
import { getAllSDKDic } from '@/server/common.js'
export default {
  name: 'keyword',
  components: {
    elFormItem: FormItem,
    elCheckboxGroup: CheckboxGroup,
    elCheckbox: Checkbox,
    elForm: Form,
    selectKing
  },
  props: ['isDataBase'],
  data() {
    return {
      selectVal: '全部分析',
      isCheckBox: false,
      sdkType: [], // 分析val
      SDKDic: [
        {
          name: '全部分析',
          value: '',
          status: true
        }
      ],
      SDKDicCheck: [
        {
          name: '全部分析',
          value: '',
          status: true
        }
      ]
    }
  },
  computed: {},
  methods: {
    // 获取数据
    async initData() {
      const res = await getAllSDKDic()
      if (res && res.success && res.data) {
        let list = res.data && res.data.length ? res.data : []
        list = list.map(item => {
          return {
            name: item,
            value: item,
            status: false
          }
        })
 
        this.SDKDic = [
          {
            name: '全部分析',
            value: '',
            status: true
          },
          ...JSON.parse(JSON.stringify(list))
        ]
        this.SDKDicCheck = [
          {
            name: '全部分析',
            value: '',
            status: true
          },
          ...JSON.parse(JSON.stringify(list))
        ]
      }
    },
    commonCheck({ index, val, type = 'checkBox' }) {
      const dataList = JSON.parse(JSON.stringify(this.SDKDicCheck))
      if (type === 'radio') {
        dataList.map((iteam, index) => {
          dataList[index].status = false
        })
        dataList[index].status = true
      } else {
        dataList[index].status = !val
        dataList[0].status = false
      }
      return dataList
    },
    selectCheck({ index, val }, type) {
      const jsonArr = this.commonCheck({ index, val, type })
      if (type === 'radio') {
        // 单选选择库
        this.SDKDic = [...jsonArr]
        this.SDKDicCheck = [...jsonArr]
        // 提交数据
        this.emitData()
      } else {
        this.SDKDicCheck = [...jsonArr]
      }
    },
    submitCheck() {
      const jsonArr = JSON.parse(JSON.stringify(this.SDKDicCheck))
      this.SDKDic = [...jsonArr]
      this.isCheckBox = false
 
      // 提交数据
      this.emitData()
    },
    cancel() {
      const jsonArr = JSON.parse(JSON.stringify(this.SDKDic))
      this.SDKDicCheck = [...jsonArr]
      this.isCheckBox = false
    },
    /* 多选视图操作 end */
    emitData() {
      // 根据记录选择值
      let valNames = []
      let sdkType = []
 
      this.SDKDic.filter(item => item && item.status).map(item => {
        item.name && valNames.push(item.name)
        item.value && sdkType.push(item.value)
      })
 
      // 设置selcet val显示
      this.selectVal = valNames.join('/')
      // 关闭下拉
      this.$refs.selectKing.close()
      // 拼接实际value
      const json = {
        valNames,
        sdkType,
        data: this.SDKDic
      }
      this.$emit('submitData', json)
    },
    init() {
      /* 进行清空 */
      const SDKArr = this.SDKDic.map(item => {
        item.status = item.value === ''
        return item
      })
      this.SDKDic = [...JSON.parse(JSON.stringify(SDKArr))]
      this.SDKDicCheck = [...JSON.parse(JSON.stringify(SDKArr))]
      this.emitData()
    }
  },
  created() {
    this.initData()
  },
  watch: {}
}
</script>
<style lang="scss" scoped>
.type-library {
  .select-liby {
    padding: 2px 20px 2px 0;
    a {
      cursor: pointer;
    }
    a.active {
      font-weight: 600;
      text-decoration: underline;
    }
    a:hover {
      color: #ccc;
    }
    a.active:hover {
      color: #333;
    }
  }
}
.library-list {
  h6 {
    height: 30px;
    line-height: 30px;
    cursor: pointer;
  }
  .alarm-item {
    line-height: 30px;
 
    .alarm-icon {
      display: inline-block;
      vertical-align: middle;
      width: 15px;
      height: 15px;
      line-height: 15px;
      background: #d9534f;
      border-radius: 50%;
      overflow: hidden;
      text-align: center;
      color: #fff;
    }
    .alarm-name {
      display: inline-block;
      vertical-align: middle;
      line-height: 15px;
      padding: 2px 4px;
      max-width: 100px;
      cursor: pointer;
    }
    .alarm-name.active {
      background: #35bde7;
      color: #fff;
    }
  }
}
</style>