您好,登錄后才能下訂單哦!
本文實例講述了jquery實現自定義樹形表格的方法。分享給大家供大家參考,具體如下:
話不多說,直接上代碼(本代碼僅提供思路):
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" > 暫無數據 </td></tr>'; } $("#datatable" + t + ' tbody').html(html); // 插入的表格 initTreeAction(t); // 結構初始化 } 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(); } } }); }
更多關于jQuery相關內容感興趣的讀者可查看本站專題:《jQuery表格(table)操作技巧匯總》、《jQuery操作xml技巧總結》、《jQuery form操作技巧匯總》、《jQuery常用插件及用法總結》、《jQuery擴展技巧總結》及《jquery選擇器用法總結》
希望本文所述對大家jQuery程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。