common.js
3.3 KB
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
/*
* @Description: 省市组件
* @Author: huyue
* @Date: 2022-04-08 17:00:25
* @LastEditors: huyue
* @LastEditTime: 2022-04-11 17:01:39
*/
layui.define(['form', 'laydate', 'carousel', 'util'], function (exports) {
const { form, laydate, carousel, util, $, cache } = layui
exports('common', {
/**
* 生成下拉选
* @param config:参数
* id:dom的id
* string:单个dom
* Array:多个dom
* url:获取地址
* value:<option>的value名
* text:<option>text
* getValue:自定义传输值
* getText:自定义显示文本
* param:url请求的额外参数
* initData:默认option
* link:需要联动的下拉选的dom id
* select:选中事件
* @return <option/>文本
*/
getSelect(config) {
let f = {
id: undefined,
url: undefined,
name: '',
value: 'value',
text: 'text',
getValue: function (v) {
return v[this.value]
},
getText: function (t) {
return t[this.text]
},
param: null,
initData: [{ adcode: '', name: '请选择' }],
link: false,
select: false,
data: [],
isFirst: true,
...config
},
that = this,
isArray = Array.isArray(f.id)
if (f.isFirst) {
if (isArray) {
f.id.forEach(id => {
$('#' + id).attr('lay-filter', 'commonSelect' + id)
$('#' + id).attr('name', f.name)
})
} else {
$('#' + f.id).attr('lay-filter', 'commonSelect' + f.id)
$('#' + f.id).attr('name', f.name)
}
}
// getProvinceList()
//绑定选中事件
if (f.select) {
if (isArray) {
f.id.forEach(id => {
form.on('select(commonSelect' + id + ')', function (data) {
f.select(data)
})
})
} else {
form.on('select(commonSelect' + f.id + ')', function (data) {
f.select(data)
})
}
}
//生成option
function getOptions() {
// if (!f.link) {
// f.data = that.provinceInfoList
// }
let h = ''
$.each(f.initData, function (i, d) {
h += '<option value="' + d.adcode + '">' + d.name + '</option>'
})
$.each(f.data, function (i, d) {
h += '<option value="' + f.getValue(d) + '">' + f.getText(d) + '</option>'
})
if (isArray) {
f.id.forEach(d => {
let a = $('#' + d)
a.empty()
a.append(h)
})
} else {
let a = $('#' + f.id)
a.empty()
a.append(h)
}
form.render('select')
return h
}
function getProvinceList() {
var ajax = layui.$.ajax
ajax({
// url: 'https://oss-config.test.glsx.com.cn/mock/5be17454f31545347559d499/config/glsx-oss-account-config',
url: 'http://oss-config.dev.glsx.com.cn/mock/617ba1df1072f92988f05a14/account/provinceList',
async: false,
success: function (res) {
that.provinceInfoList = res.provinceInfoList
}
})
}
return getOptions()
}
})
})