select-own.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. var selectName="",
  2. selectCode="";//选择数据
  3. function add_selected_fun(data,$id,name,code){
  4. var $body = $('body'),
  5. allData = data;
  6. selectName = $id.val().trim()
  7. selectCode = $id.attr('data-id')
  8. //取消
  9. function cancel(){
  10. $('.sel-bg').hide()
  11. $('.sel-main').hide()
  12. $('.sel-search').hide()
  13. $('#keyInput').val('')
  14. $body.off('click','.sel-ok')
  15. }
  16. function showData(data){
  17. var tmp=""
  18. $.map(data,function(item,index){
  19. if(item[code] == selectCode){
  20. tmp +='<li class="active" data-id="'+item[code]+'">'+item[name]+'</li>'
  21. }else{
  22. tmp +='<li data-id="'+item[code]+'">'+item[name]+'</li>'
  23. }
  24. })
  25. tmp +='<li></li><li></li>'
  26. return tmp
  27. }
  28. function initData(){
  29. showAllData()
  30. $('.sel-bg').show()
  31. $('.sel-search').show()
  32. $('.sel-main').show()
  33. }
  34. if($('ul.sel-list').length==0){
  35. var tmp1 = showData(allData)
  36. var html = '<div class="sel-bg"></div>\
  37. <div class="sel-search"><input id="keyInput" type="text" placeholder="关键字过滤"/></div>\
  38. <div class="sel-main">\
  39. <div class="sel-head"><div class="sel-cancel">取消</div><div class="sel-ok">确定</div></div>\
  40. <div class="sel-contain"><ul class="sel-list">'+tmp1+'</ul></div>\
  41. </div>'
  42. $body.prepend(html)
  43. $body.on('click','.sel-cancel',function(){
  44. cancel()
  45. })
  46. $body.on('click','.sel-bg',function(){
  47. cancel()
  48. })
  49. $body.on('click','.sel-list li',function(){
  50. var $li = $(this),
  51. $text = $li.text().trim(),
  52. $ul = $('.sel-list')
  53. if(!$text){
  54. return
  55. }
  56. if(!$li.hasClass('active')){
  57. $ul.find('li').removeClass('active')
  58. $li.addClass('active')
  59. selectName = $li.text()
  60. selectCode = $li.attr('data-id')
  61. }
  62. })
  63. $body.on('input','#keyInput',function(){
  64. var $data = $('#keyInput').val().trim()
  65. if(!$data){
  66. showAllData()
  67. }
  68. }).on('keydown',function(e) {
  69. var $data = $('#keyInput').val().trim()
  70. if($data && e.which === 13) {
  71. $('#keyInput').blur()
  72. filteData($data)
  73. }
  74. })
  75. $body.on('focus','#keyInput',function(){
  76. $('.sel-main').hide()
  77. })
  78. $body.on('blur','#keyInput',function(){
  79. setTimeout(function(){
  80. if($('.sel-bg').is(':visible')){
  81. $('.sel-main').show()
  82. }
  83. },500)
  84. })
  85. }
  86. //初始化
  87. initData()
  88. //过滤数据
  89. function filteData(key){
  90. var newArr = []
  91. $.map(allData,function(item,index){
  92. if(item[name].indexOf(key) > -1){
  93. newArr.push(item)
  94. }
  95. })
  96. var html = showData(newArr)
  97. $('.sel-list').empty().html(html)
  98. }
  99. //展示所有
  100. function showAllData(){
  101. var html = showData(allData)
  102. $('.sel-list').empty().html(html)
  103. }
  104. //重新绑定点击
  105. $body.on('click','.sel-ok',function(){
  106. $id.val(selectName)
  107. $id.attr('data-id',selectCode)
  108. cancel()
  109. })
  110. }