mbui.define(['tool','layer'], function (exports) { const tool = mbui.tool,layer = mbui.layer; let searchTimer; const dataTypes = { 'property':{ title:'选择固定资产', url:'/adm/api/get_property', searchbar:'', template:function (item,types){ return '' } }, 'car':{ title:'选择车辆信息', url:'/adm/api/get_car', searchbar:'', template:function (item,types){ return '' } }, 'room':{ title:'选择会议室', url:'/adm/api/get_meeting_room', searchbar:'', template:function (item,types){ return '' } }, 'loan':{ title:'选择抵消借支', url:'/finance/api/get_loan', searchbar:'', template:function (item,types){ return '' } }, 'customer':{ title:'选择客户', url:'/customer/api/get_customer', searchbar:'', template:function (item,types){ return '' } }, 'supplier':{ title:'选择供应商', url:'/contract/api/get_supplier', searchbar:'', template:function (item,types){ return '' } }, 'contract':{ title:'选择销售合同', url:'/contract/api/get_contract', searchbar:'', template:function (item,types){ return '' } }, 'product':{ title:'选择产品', url:'/contract/api/get_product', searchbar:'', template:function (item,types){ return '' } }, 'purchase':{ title:'选择采购合同', url:'/contract/api/get_purchase', searchbar:'', template:function (item,types){ return '' } }, 'purchased':{ title:'选择采购物品', url:'/contract/api/get_purchased', searchbar:'', template:function (item,types){ return '' } }, 'project':{ title:'选择项目', url:'/project/api/get_project', searchbar:'', template:function (item,types){ return '' } }, 'task':{ title:'选择任务', url:'/project/api/get_task', searchbar:'', template:function (item,types){ return '' } } }; //html转义,防止XSS function escapeHtml(text) { var map = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }; return text.replace(/[&<>"']/g, function(m) { return map[m]; }); } var LoadData= function () { this.config = { title: "请选择内容", url: "", searchbar:"", type: "radio", where: {}, limit: 20, data:[], selectData:[], template: function (data) { return JSON.parse(data); }, callback: function(){} }; this.index = 0; this.loaded = 0; this.page = 1; this.count = 0; this.total = 0; }; // 初始化 LoadData.prototype.init = function (options,types) { var that = this; let opts={}; if (types != "undefined") { opts = dataTypes[types]; } $.extend(true,that.config,opts,options); //console.log(that.config); that.index = new Date().getTime(); var $container = $('
关闭确认

'+that.config.title+'

'); $container.append(that.config.searchbar); $container.append('

暂无数据
努力加载中
—— ● ——
'); $('body').append($container); $('#root').hide(); $container.find('.left').click(function () { $container.fadeOut(function () { $container.remove(); $('#root').show(); }); }); $container.on('input','.search-input',function() { const key = $(this).val().trim(); // 清除上一次的定时器 clearTimeout(searchTimer); // 设置新的延迟请求 searchTimer = setTimeout(() => { that.config.where['keywords']=key; that.page=1; that.ajax(); }, 400); }); $container.on('click','.tags-search',function() { const key = $(this).data('key'); const val = $(this).data('val'); $(this).addClass('active').siblings().removeClass('active'); // 清除上一次的定时器 clearTimeout(searchTimer); // 设置新的延迟请求 searchTimer = setTimeout(() => { that.config.where[key]=val; that.page=1; that.ajax(); }, 400); }); $container.find('.right').click(function () { let selected = $container.find('input:checked'); if (selected.length == 0) { layer.msg('请选择数据'); return false; } that.config.selectData.length=0; let ids=[]; for (var m = 0; m < selected.length; m++) { let selected_item = $(selected[m]).parent(); let id=selected_item.data('id'); ids.push(id); } that.config.selectData = that.config.data.filter(item => ids.includes(item.id)); that.config.callback(that.config.selectData); $container.fadeOut(function () { $container.remove(); $('#root').show(); }); }); $('#lists_'+that.index).scroll(function(){ if ($(this).scrollTop() + $('body').height() >= $('#container_'+that.index).height()-60) { console.log($(this).scrollTop()); console.log('
'); console.log($('body').height()); console.log('
'); console.log($('#container_'+that.index).height()); // 滚动到页面底部时加载更多数据 if (that.total < that.count && that.loaded == 0){ that.ajax(); } } }); that.ajax(); }; LoadData.prototype.ajax = function () { var that = this; var elem = $('#container_'+that.index); var container = elem.parent(); //console.log(container.html()); // 发送请求获取数据 $.ajax({ url: that.config.url + '?page=' + that.page + '&limit=' + that.config.limit, type: 'GET', data: that.config.where, beforeSend: function () { // 显示加载按钮 that.loaded = 1; container.find('.load-data-loading').show(); container.find('.load-data-end').hide(); }, success: function (res) { that.count=res.count; that.total+=res.data.length; container.find('.load-data-none').attr('class','load-data-none load-data-'+that.count); if(that.page==1){ that.config.data.length=0; elem.html(''); } if (res.count > 0) { that.page++; that.config.data.push(...res.data); $.each(res.data, function (index, item) { // 转义JSON对象中的字符串值,防止XSS for (var key in item) { if (typeof item[key] === 'string') { item[key] = escapeHtml(item[key]); } } // 创建列表项并添加到列表中 var listItem = that.config.template(item,that.config.type); elem.append(listItem); }); container.find('.load-data-end').show(); } else{ that.config.data.length=0; that.page=1; } }, complete: function () { that.loaded = 0; container.find('.load-data-loading').hide(); container.find('.load-data-end').show(); } }); } //选择员工弹窗 $('body').on('click','.picker-data',function () { let that = $(this); let types = that.data('types'); let type = that.data('type'); let where = that.data('where'); if (typeof(type) == "undefined" || type == '') { type = 1; } type = type == 2 ? 'checkbox' : 'radio'; if (typeof(types) == "undefined" || types == '') { layer.msg('请设置【picker】的类型'); return false; } if (typeof(where) == "undefined" || where == '') { map = {}; } else{ map = JSON.parse(where); } let picker = new LoadData(); picker.init({ type:type, where: map, callback:function(selectData){ let ids=[],titles=[]; for ( var i = 0; i