您好,登錄后才能下訂單哦!
本文實(shí)例講述了jquery實(shí)現(xiàn)自定義樹形表格的方法。分享給大家供大家參考,具體如下:
話不多說,直接上代碼(本代碼僅提供思路):
function createTables(data, t, p) { $("#datatable" + t + ' tbody').empty(); var count = data.length; var html = ''; if (count > 0) { for (var i = 0; i < count; i++) { var btn = ''; if(data[i]._child && data[i]._child.length > 0){ btn = '<i class="layui-icon mt-icon" data-tid="'+ data[i].category_id +'-'+ data[i].id +'-'+ t +'" data-pid="'+ data[i].category_id +'-'+ data[i].pid +'-'+ t +'"></i> '; }else{ btn = '<i class="mt-icon" data-tid="'+ data[i].category_id +'-'+ data[i].id +'" data-pid="'+data[i].category_id +'-'+ data[i].pid+'"></i> '; } html += '<tr >' + '<td >'+btn + data[i].name + '(' + data[i].title + ')' + '</td>' + '<td>' + (data[i].source_name ? data[i].source_name : '') + '</td>' + '<td>' + data[i].show_time + '</td>' + '<td>' + dataFormat(data[i].current_record, data[i].unit) + '</td>' + '<td>' + dataFormat(data[i].prev_record, data[i].unit) + '</td>' + '<td>' + dataFormat(data[i].last_record, data[i].unit) + '</td>' + '<td>' + dataFormat(data[i].budget, data[i].unit) + '</td>' + '<td>' + dataFormat(data[i].budget_rate, '%') + '</td>' + '<td>' + dataFormat(data[i].record_rate) + '</td>' + '<td>' + dataFormat(data[i].mom, '%') + '</td>' + '<td>' + dataFormat(data[i].yoy, '%') + '</td>' + '<td><i class="layui-icon" onclick="showHiddenArea(this,' + data[i].category_id + ',' + data[i].data_structure_id + ',' + data[i].time_unit + ',' + t + ');"></i></td> ' + '</tr>'; if(data[i]._child && data[i]._child.length > 0){ html = createNewTable(data[i]._child,html,1,t); } } } else { html = '<tr><td colspan="12" > 暫無數(shù)據(jù) </td></tr>'; } $("#datatable" + t + ' tbody').html(html); // 插入的表格 initTreeAction(t); // 結(jié)構(gòu)初始化 } function createNewTable(data,html,y,t) { var counts = data.length; var num = ++y; for (var i = 0; i < counts; i++) { var btn = ''; var css = ''; css = 'text-indent:'+ (num * 20) +'px;'; if(data[i]._child && data[i]._child.length > 0){ css = 'text-indent:'+ (num==2?20:(num-1) * 20) +'px;'; btn = '<i class="layui-icon mt-icon" data-tid="'+ data[i].category_id +'-'+ data[i].id +'-'+ t +'" data-pid="'+data[i].category_id +'-'+ data[i].pid+'-'+ t +'" data-type="dir"></i> '; }else{ btn = '<i class="mt-icon" data-tid="'+ data[i].category_id +'-'+ data[i].id +'-'+ t +'" data-pid="'+data[i].category_id +'-'+ data[i].pid +'-'+ t +'" data-type="file"></i> '; } html += '<tr >' + '<td >'+ btn + data[i].name + '(' + data[i].title + ')' + '</td>' + '<td>' + (data[i].source_name ? data[i].source_name : '') + '</td>' + '<td>' + data[i].show_time + '</td>' + '<td>' + dataFormat(data[i].current_record, data[i].unit) + '</td>' + '<td>' + dataFormat(data[i].prev_record, data[i].unit) + '</td>' + '<td>' + dataFormat(data[i].last_record, data[i].unit) + '</td>' + '<td>' + dataFormat(data[i].budget, data[i].unit) + '</td>' + '<td>' + dataFormat(data[i].budget_rate, '%') + '</td>' + '<td>' + dataFormat(data[i].record_rate) + '</td>' + '<td>' + dataFormat(data[i].mom, '%') + '</td>' + '<td>' + dataFormat(data[i].yoy, '%') + '</td>' + '<td><i class="layui-icon" onclick="showHiddenArea(this,' + data[i].category_id + ',' + data[i].data_structure_id + ',' + data[i].time_unit + ',' + t + ');"></i></td> ' + '</tr>'; if(data[i]._child && data[i]._child.length > 0){ html = createNewTable(data[i]._child,html,num,t); } } return html; } function showLowData(obj) { var mId = $(obj).attr('data-tid'); var isOpen = $(obj).hasClass('open'); if (isOpen) { $(obj).removeClass('open'); if($(obj).hasClass('layui-icon')){ $(obj).html(''); } } else { $(obj).addClass('open'); if($(obj).hasClass('layui-icon')){ $(obj).html(''); } } $(obj).parents('tbody').find('tr').each(function () { var mt = $(this).find('.mt-icon'); var pid = mt.attr('data-pid'); var Open = mt.hasClass('open'); var type = mt.attr('data-type'); if (mId == pid) { if (isOpen) { $(this).hide(); if (type == 'dir' && Open == isOpen) { mt.trigger('click'); } } else { $(this).show(); } } }); }
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery表格(table)操作技巧匯總》、《jQuery操作xml技巧總結(jié)》、《jQuery form操作技巧匯總》、《jQuery常用插件及用法總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計有所幫助。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。