forked from ringocx/layui_complete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautocomplete.js
149 lines (148 loc) · 6.59 KB
/
autocomplete.js
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
layui.define(['jquery', 'laytpl', 'layer'], function (e) {
"use strict";
layui.link('/resource/css/autocomplete.css');
var hint = layui.hint(),
$ = layui.jquery,
laytpl = layui.laytpl,
layer = layui.layer,
module = 'autocomplete',
filter = 'layui-autocomplete',
container = 'layui-form-autocomplete',
container_focus = 'layui-form-autocomplete-focus',
system = {
config: {
template: ['<div class="layui-form-autocomplete">', '<dl class="layui-anim layui-anim-upbit">', '</dl>', '</div>'].join(''),
layout: ['<dd data-index="{{d.index}}">{{d.text}}</dd>'].join(''),
template_txt: '{{d.text}}',
template_val: '{{d.value}}',
cache: false
},
index: layui.autocomplete ? layui.autocomplete.index + 1e4: 0,
data: {},
},
callback = function() {
var _self = this,
_config = _self.config,
_id = _config.id;
return _id && (callback.config[_id] = _config), {
config: _self.config
}
},
job = function(e) {
var _self = this;
_self.index = ++system.index,
_self.config = $.extend({}, _self.config, system.config, e),
_self.render()
};
job.prototype.config = {
text: {
none: "无数据",
loading: "加载中"
},
response: {
code: 'code',
data: 'content'
},
ajax: [],
data: [],
filter: ''
},
job.prototype.render = function() {
var _self = this, _config = _self.config;
if (_config.elem = $(_config.elem), _config.where = _config.where || {}, !_config.elem[0]) return _self;
var _elem = _config.elem,
_container = _elem.next('.' + container),
_html = _self.elem = $(laytpl(_config.template).render({}));
_config.id = _self.id, _container[0] && _container.remove(), _elem.attr('autocomplete', 'off'), _elem.after(_html);
_self.events()
},
job.prototype.pullData = function () {
var _self = this,
_config = _self.config,
_elem = _config.elem,
_container = _elem.next('.' + container),
_dom = _container.find('dl');
if (!_config.filter) return _self.renderData([]);
if (_config.cache && _config.data[_self.index]) return _self.renderData(_config.data[_self.index]);
if (_config.cache && _config.ajax[_self.index] != undefined) return;
(!_config.cache && _config.ajax[_self.index] != undefined) && _config.ajax[_self.index].abort(), _config.ajax[_self.index] = $.ajax({
type: _config.method || "get",
url: _config.url,
data: {keywords: _config.filter},
dataType: "json",
beforeSend: function () {
_container.addClass(container_focus), _dom.html(['<dd style="text-align: center" autocomplete-load>', _config.text.loading, '</dd>'].join(''))
},
success: function (resp) {
return 0 != eval('resp.' + _config.response.code) ? layer.msg(eval('resp.' + _config.response.data)) : _config.data[_self.index] = eval('resp.' + _config.response.data), _self.renderData(_config.data[_self.index])
},
error: function () {
hint.error("请求失败")
},
complete: function () {
delete _config.ajax[_self.index]
}
})
},
job.prototype.renderData = function (resp) {
var _self = this,
_config = _self.config,
_elem = _config.elem,
_container = _elem.next('.' + container),
_dom = _container.find('dl'),
_list = [];
layui.each(resp, function (i, e) {
(typeof e === "object" || typeof e === "array") ?
$.each(e, function (_i, _e) {
if(_e.indexOf(_config.filter) > -1) {
_list.push(laytpl(_config.layout).render({index: i, text: laytpl(_config.template_txt).render(e)}));
return false;
}
}) : (e.indexOf(_config.filter) > -1 && _list.push(laytpl(_config.layout).render({index: i, text: laytpl(_config.template_txt).render(e)})))
});
_dom.html(_list.join('')), _list.length > 0 ? _container.addClass(container_focus) : _container.removeClass(container_focus)
},
job.prototype.events = function () {
var _self = this,
_config = _self.config,
_elem = _config.elem,
_container = _elem.next('.' + container),
_dom = _container.find('dl');
_elem.on('focus', function () {
_config.filter = this.value, _config.cache ? _self.pullData() : _self.renderData(_config.data[_self.index])
}).on('input propertychange', function () {
_config.filter = this.value, _self.pullData()
}),
$(document).on('click', function (e) {
var _target = e.target, _item = _dom.find(_target), _e = _item.length > 0 ? _item.closest('dd') : undefined;
if (_target === _elem[0]) return false;
if (_e !== undefined) {
if (_e.attr('autocomplete-load') !== undefined) return false;
_elem.val(laytpl(_config.template_val).render(_config.data[_self.index][_e.index()])), _config.onselect == undefined || _config.onselect(_config.data[_self.index][_e.index()])
}
_container.removeClass(container_focus);
})
};
callback.config = {},
callback.job = {},
system.init = function (e, c) {
var c = c || {}, _self = this, _elems = $(e ? 'input[lay-filter="' + e + '"]': 'input[' + filter + ']');
_elems.each(function (_i, _e) {
var _elem = $(_e),
_lay_data = _elem.attr('lay-data');
try {
_lay_data = new Function("return " + _lay_data)()
} catch (ex) {
return hint.error("autocomplete元素属性lay-data配置项存在语法错误:" + _lay_data)
}
var _config = $.extend({elem: this}, system.config, c, _lay_data);
_config.url == undefined && (_config.data == undefined || _config.length === 0) && hint.error("autocomplete配置有误,缺少获取数据方式");
system.render(_config);
})
},
system.render = function (e) {
var j = new job(e);
return callback.call(j)
}
system.init(), e(module, system);
})