| | |
| | | table = layui.table, |
| | | form = layui.form; |
| | | var tableSelect = function () { |
| | | this.v = '1.2.0'; |
| | | this.v = '1.3.0'; |
| | | }; |
| | | |
| | | /** |
| | |
| | | |
| | | //é»è®¤è®¾ç½® |
| | | opt.searchKey = opt.searchKey || 'keyword'; |
| | | opt.checkedKey = opt.checkedKey; |
| | | opt.table.page = opt.table.page || false; |
| | | opt.table.height = opt.table.height || 290; |
| | | elem.off('click').on('click', function(e) { |
| | |
| | | tableBox += '</div>'; |
| | | tableBox = $(tableBox); |
| | | $('body').append(tableBox); |
| | | //æ°æ®ç¼å |
| | | var checkedData = []; |
| | | //渲æTABLE |
| | | opt.table.elem = "#"+tableName; |
| | | opt.table.id = tableName; |
| | | opt.table.where['sWhere'] = opt.sWhere |
| | | //è¥æ²¡æwhere表示为å端å页 |
| | | if (opt.table.url != undefined) { |
| | | opt.table.where['sWhere'] = opt.sWhere |
| | | } else { |
| | | opt.table.data = opt.ajaxSelect(opt.sWhere); |
| | | } |
| | | opt.table.request={//è®¾ç½®é¡µåæ° |
| | | pageName: 'page', // 页ç çåæ°åç§°ï¼é»è®¤ï¼page |
| | | limitName: 'size' // æ¯é¡µæ°æ®æ¡æ°çåæ°åï¼é»è®¤ï¼limit |
| | |
| | | "data": res.data // è§£ææ°æ®å表 |
| | | }; |
| | | } |
| | | opt.table.done = function(res, curr, count){ |
| | | defaultChecked(res, curr, count); |
| | | setChecked(res, curr, count); |
| | | }; |
| | | //ç¨jumpæ¿ä»£åæ¥è¡¨æ ¼pagejumpç¨äºæ´æ°æé®é䏿°éæ¤åè½åç°æ ç¨å é¤ |
| | | //opt.table.done = function (res, curr, count, origin) { |
| | | // this.page.jump = jump; |
| | | //} |
| | | |
| | | var tableSelect_table = table.render(opt.table); |
| | | |
| | | //å页éä¸ä¿åæ°ç» |
| | | table.on('radio('+tableName+')', function(obj){ |
| | | if(opt.checkedKey){ |
| | | checkedData = table.checkStatus(tableName).data |
| | | } |
| | | updataButton(table.checkStatus(tableName).data.length) |
| | | }) |
| | | table.on('checkbox(' + tableName + ')', function (obj) { |
| | | if(opt.checkedKey){ |
| | | if(obj.checked){ |
| | | for (var i=0;i<table.checkStatus(tableName).data.length;i++){ |
| | | checkedData.push(table.checkStatus(tableName).data[i]) |
| | | } |
| | | }else{ |
| | | if(obj.type=='all'){ |
| | | for (var j=0;j<table.cache[tableName].length;j++) { |
| | | for (var i=0;i<checkedData.length;i++){ |
| | | if(checkedData[i][opt.checkedKey] == table.cache[tableName][j][opt.checkedKey]){ |
| | | checkedData.splice(i,1) |
| | | } |
| | | } |
| | | } |
| | | }else{ |
| | | //å 为LAYUIé®é¢ï¼æä½å°ååå
¨éç¶ææ¶è·åå°çobj为空ï¼è¿éç¨å½æ°è·åæªéä¸ç项ã |
| | | function nu (){ |
| | | var noCheckedKey = ''; |
| | | for (var i=0;i<table.cache[tableName].length;i++){ |
| | | if(!table.cache[tableName][i].LAY_CHECKED){ |
| | | noCheckedKey = table.cache[tableName][i][opt.checkedKey]; |
| | | } |
| | | } |
| | | return noCheckedKey |
| | | } |
| | | var noCheckedKey = obj.data[opt.checkedKey] || nu(); |
| | | for (var i=0;i<checkedData.length;i++){ |
| | | if(checkedData[i][opt.checkedKey] == noCheckedKey){ |
| | | checkedData.splice(i,1); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | checkedData = uniqueObjArray(checkedData, opt.checkedKey); |
| | | updataButton(checkedData.length) |
| | | }else{ |
| | | updataButton(table.checkStatus(tableName).data.length) |
| | | } |
| | | }); |
| | | |
| | | //渲æè¡¨æ ¼åéä¸ |
| | | function setChecked (res, curr, count) { |
| | | for(var i=0;i<res.data.length;i++){ |
| | | for (var j=0;j<checkedData.length;j++) { |
| | | if(res.data[i][opt.checkedKey] == checkedData[j][opt.checkedKey]){ |
| | | res.data[i].LAY_CHECKED = true; |
| | | var index= res.data[i]['LAY_TABLE_INDEX']; |
| | | var checkbox = $('#'+tableName+'').next().find('tr[data-index=' + index + '] input[type="checkbox"]'); |
| | | checkbox.prop('checked', true).next().addClass('layui-form-checked'); |
| | | var radio = $('#'+tableName+'').next().find('tr[data-index=' + index + '] input[type="radio"]'); |
| | | radio.prop('checked', true).next().addClass('layui-form-radioed').find("i").html(''); |
| | | radio.closest('tr').addClass('layui-table-click'); |
| | | } |
| | | } |
| | | } |
| | | var checkStatus = table.checkStatus(tableName); |
| | | if(checkStatus.isAll){ |
| | | $('#'+tableName+'').next().find('.layui-table-header th[data-field="0"] input[type="checkbox"]').prop('checked', true); |
| | | $('#'+tableName+'').next().find('.layui-table-header th[data-field="0"] input[type="checkbox"]').next().addClass('layui-form-checked'); |
| | | } |
| | | updataButton(checkedData.length) |
| | | } |
| | | |
| | | //åå
¥é»è®¤éä¸å¼(puash checkedData) |
| | | function defaultChecked (res, curr, count){ |
| | | if(opt.checkedKey && elem.attr('ts-selected')){ |
| | | var selected = elem.attr('ts-selected').split(","); |
| | | for(var i=0;i<res.data.length;i++){ |
| | | for(var j=0;j<selected.length;j++){ |
| | | if(res.data[i][opt.checkedKey] == selected[j]){ |
| | | checkedData.push(res.data[i]) |
| | | } |
| | | } |
| | | } |
| | | checkedData = uniqueObjArray(checkedData, opt.checkedKey); |
| | | } |
| | | } |
| | | //jumpå页触å彿° |
| | | //function jump(obj, first) { |
| | | // table.reload(tableName); |
| | | // //æ´æ°æé® |
| | | // updataButton(table.checkStatus(tableName).data.length) |
| | | //} |
| | | |
| | | //æ´æ°é䏿°é |
| | | function updataButton (n) { |
| | |
| | | elem.off('input').on('input', function (e) { |
| | | // 黿¢è¡¨åæäº¤ï¼å¦æè¾å
¥æ¡å¨è¡¨åå
ï¼ |
| | | e.preventDefault(); |
| | | opt.table.where['sWhere'] = opt.sWhere + " and " + opt.searchKey + " like '%" + elem.val() + "%' "; |
| | | tableSelect_table.reload({ |
| | | where: opt.table.where, |
| | | page: { |
| | | curr: 1 |
| | | } |
| | | }); |
| | | //å端å页æç´¢ |
| | | if (opt.table.where != undefined) { |
| | | opt.table.where['sWhere'] = opt.sWhere + " and " + opt.searchKey + " like '%" + elem.val() + "%' "; |
| | | tableSelect_table.reload({ |
| | | where: opt.table.where, |
| | | page: { |
| | | curr: 1 |
| | | } |
| | | }); |
| | | } else { |
| | | var sWhere = opt.sWhere + " and " + opt.searchKey + " like '%" + elem.val() + "%' "; |
| | | tableSelect_table.reload({ |
| | | data: opt.ajaxSelect(sWhere) |
| | | }); |
| | | } |
| | | |
| | | }) |
| | | // //å
³é®è¯æç´¢ |
| | | // form.on('submit(tableSelect_btn_search)', function(data){ |
| | | // // console.log(" and "+opt.searchKey +"='" + data.opt.searchKey +"'") |
| | | // console.log(data); |
| | | // opt.table.where['sWhere'] = " and "+opt.searchKey +" like '%" + data.field[opt.searchKey] +"%' "; |
| | | // tableSelect_table.reload({ |
| | | // where: opt.table.where, |
| | | // page: { |
| | | // curr: 1 |
| | | // } |
| | | // }); |
| | | // return false; |
| | | // }); |
| | | |
| | | //åå»è¡éä¸ |
| | | table.on('rowDouble('+tableName+')', function(obj){ |
| | |
| | | l.LAY_CHECKED = flag; |
| | | } |
| | | }); |
| | | //æ´æ°æé® |
| | | updataButton(table.checkStatus(tableName).data.length) |
| | | //æ´æ°æé®ï¼æ ç¨å é¤ï¼ |
| | | //updataButton(table.checkStatus(tableName).data.length) |
| | | } |
| | | }) |
| | | //æé®éä¸ |
| | | tableBox.find('.tableSelect_btn_select').on('click', function() { |
| | | var checkStatus = table.checkStatus(tableName); |
| | | if(checkedData.length > 1){ |
| | | checkStatus.data = checkedData; |
| | | } |
| | | selectDone(checkStatus); |
| | | }) |
| | | |
| | | //åå¼åè°åå
³é |
| | | function selectDone (checkStatus){ |
| | | if(opt.checkedKey){ |
| | | var selected = []; |
| | | for(var i=0;i<checkStatus.data.length;i++){ |
| | | selected.push(checkStatus.data[i][opt.checkedKey]) |
| | | } |
| | | elem.attr("ts-selected",selected.join(",")); |
| | | } |
| | | opt.done(elem, checkStatus); |
| | | tableBox.remove(); |
| | | delete table.cache[tableName]; |
| | | checkedData = []; |
| | | } |
| | | |
| | | //ç¹å»å
¶ä»åºåå
³é |
| | |
| | | if(!userSet_con.is(e.target) && userSet_con.has(e.target).length === 0){ |
| | | tableBox.remove(); |
| | | delete table.cache[tableName]; |
| | | checkedData = []; |
| | | } |
| | | }); |
| | | }) |