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 data-tid="'+ data[i].category_id +'-'+ data[i].id +'-'+ t +'" data-pid="'+ data[i].category_id +'-'+ data[i].pid +'-'+ t +'">&#xe623;</i> '; }else{ btn = '<i 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>&#xe62c;</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 data-tid="'+ data[i].category_id +'-'+ data[i].id +'-'+ t +'" data-pid="'+data[i].category_id +'-'+ data[i].pid+'-'+ t +'" data-type="dir">&#xe623;</i> '; }else{ btn = '<i 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>&#xe62c;</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('&#xe623;'); } } else { $(obj).addClass('open'); if($(obj).hasClass('layui-icon')){ $(obj).html('&#xe625;'); } } $(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选择器用法总结

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/2ece877bfb20831331a50f35af3f5b83.html