var selectName="",
selectCode="";//选择数据
function add_selected_fun(data,$id,name,code){
var $body = $('body'),
allData = data;
selectName = $id.val().trim()
selectCode = $id.attr('data-id')
//取消
function cancel(){
$('.sel-bg').hide()
$('.sel-main').hide()
$('.sel-search').hide()
$('#keyInput').val('')
$body.off('click','.sel-ok')
}
function showData(data){
var tmp=""
$.map(data,function(item,index){
if(item[code] == selectCode){
tmp +='
'+item[name]+''
}else{
tmp +=''+item[name]+''
}
})
tmp +=''
return tmp
}
function initData(){
showAllData()
$('.sel-bg').show()
$('.sel-search').show()
$('.sel-main').show()
}
if($('ul.sel-list').length==0){
var tmp1 = showData(allData)
var html = '\
\
'
$body.prepend(html)
$body.on('click','.sel-cancel',function(){
cancel()
})
$body.on('click','.sel-bg',function(){
cancel()
})
$body.on('click','.sel-list li',function(){
var $li = $(this),
$text = $li.text().trim(),
$ul = $('.sel-list')
if(!$text){
return
}
if(!$li.hasClass('active')){
$ul.find('li').removeClass('active')
$li.addClass('active')
selectName = $li.text()
selectCode = $li.attr('data-id')
}
})
$body.on('input','#keyInput',function(){
var $data = $('#keyInput').val().trim()
if(!$data){
showAllData()
}
}).on('keydown',function(e) {
var $data = $('#keyInput').val().trim()
if($data && e.which === 13) {
$('#keyInput').blur()
filteData($data)
}
})
$body.on('focus','#keyInput',function(){
$('.sel-main').hide()
})
$body.on('blur','#keyInput',function(){
setTimeout(function(){
if($('.sel-bg').is(':visible')){
$('.sel-main').show()
}
},500)
})
}
//初始化
initData()
//过滤数据
function filteData(key){
var newArr = []
$.map(allData,function(item,index){
if(item[name].indexOf(key) > -1){
newArr.push(item)
}
})
var html = showData(newArr)
$('.sel-list').empty().html(html)
}
//展示所有
function showAllData(){
var html = showData(allData)
$('.sel-list').empty().html(html)
}
//重新绑定点击
$body.on('click','.sel-ok',function(){
$id.val(selectName)
$id.attr('data-id',selectCode)
cancel()
})
}