Convert HTML table to ul li using jquery
Asked Answered
T

5

5

I want to convert HTML table to ul and li elements. my table structure is

<table>
    <tbody>
        <tr>
            <th>1974</th>
            <td>UK</td>
        </tr>
        <tr>
                <th>1976</th>
            <td>Street</td>
        </tr>
        <tr>
            <th>1976-2002</th>
            <td>visitors.</td>
        </tr>

    </tbody> 
 </table>

I tried to convert this to

        <ul>
            <li>
                <p>1974</p>
                <p>UK</p>
            </li>
            <li>
                    <p>1976</p>
                <p>Street</p>
            </li>
            <li>
                <p>1976-2002</p>
                <p>visitors.</p>
            </li>

        </ul> 

using jquery replaceWith function as below. Here I firstly going to convert <tbody> element.

$(document).ready(function() {
    $("tbody").each(function(){
    var html = $(this).html();
    $(this).replaceWith("<ul>" + html + "</ul>");
    });
)};

but this not give any positive answer. Anybody have any idea how to do this.

Thanks.

Thoracotomy answered 12/2, 2013 at 9:5 Comment(2)
<table> <ul> isn't a valid markup, it should be <table> <tr> <td> <ul></ul></td></tr></table>.Charity
Sorry, It is mistake. actually I want completely change table to list. i have updated above code.Thoracotomy
I
10

I would do it like this:

$(document).ready(function() {
    var ul = $("<ul>");
    $("table tr").each(function(){
        var li = $("<li>")
        $("th, td", this).each(function(){
            var p = $("<p>").html(this.innerHTML);
            li.append(p);
        });
        ul.append(li);
    })    
    $("table").replaceWith(ul);    
});

UL first is created, and than table is replaced with UL.

Demo: http://jsfiddle.net/yXyCk/

Ingot answered 12/2, 2013 at 9:20 Comment(0)
F
6

Try

$('table').replaceWith( $('table').html()
   .replace(/<tbody/gi, "<ul id='table'")
   .replace(/<tr/gi, "<li")
   .replace(/<\/tr>/gi, "</li>")
   .replace(/<td/gi, "<p")
   .replace(/<\/td>/gi, "</p>")
   .replace(/<th/gi, "<p")
   .replace(/<\/th>/gi, "</p>")
   .replace(/<\/tbody/gi, "<\/ul")
);

Above code is tested and worked for me..
Cheers

Fante answered 12/2, 2013 at 9:16 Comment(1)
You are missing th. Only td's are replaced with pIngot
H
0

use this code

function convertToList(element) {
    var list = $("<ul/>");

    $(element).find("tr").each(function() {
        var p = $(this).children().map(function() {
            return "<p>" + $(this).html() + "</p>";
        });

        list.append("<li>" + $.makeArray(p).join("") + "</li>");
    });

    $(element).replaceWith(list);
}
Helainehelali answered 12/2, 2013 at 9:37 Comment(0)
M
0

The problem with the other answers here is that all of the styles are lost when converting the table to a list. This is what I used to accomplish it and maintain the table look:

function convert_table_to_list($table) {
    var $list = $("<ul></ul>");
    $list.css("list-style-type", "none");
    $list.css("padding", 0);

    $table.find("tr").each(function() {
        var $li = $("<li></li>");

        $li.height($(this).height());
        $(this).find("td").each(function() {
            var $div = $("<div></div>");

            // Add inline and external styling
            var style = css($(this));
            $div.css(style);

            // Add some browser default styles
            $div.css("display", "table-cell");
            $div.html($(this).html());
            $div.width($(this).width());
            $div.height($(this).height());
            $div.css("vertical-align", $(this).css("vertical-align"));
            $div.css("padding", $(this).css("padding"));

            // Add class(es)
            $div.addClass($(this).attr("class"));

            // Append to li
            $li.append($div);
        });
        // Append li to ul
        $list.append($li);
    });

    $list.insertAfter($table);
    $table.remove();

    return $list;
}

// The following are taken from https://mcmap.net/q/99387/-can-jquery-get-all-css-styles-associated-with-an-element
function css(a) {
    var sheets = document.styleSheets, o = {};
    for (var i in sheets) {
        var rules = sheets[i].rules || sheets[i].cssRules;
        for (var r in rules) {
            if (a.is(rules[r].selectorText)) {
                o = $.extend(o, css2json(rules[r].style), css2json(a.attr('style')));
            }
        }
    }
    return o;
}
function css2json(css) {
    var s = {};
    if (!css) return s;
    if (css instanceof CSSStyleDeclaration) {
        for (var i in css) {
            if ((css[i]).toLowerCase) {
                s[(css[i]).toLowerCase()] = (css[css[i]]);
            }
        }
    } else if (typeof css == "string") {
        css = css.split("; ");
        for (var i in css) {
            var l = css[i].split(": ");
            s[l[0].toLowerCase()] = (l[1]);
        }
    }
    return s;
}

See it on JSFiddle

Mcmahon answered 28/6, 2014 at 18:44 Comment(0)
M
-1

Convert <table> structure menu to <UL> using jQuery

I have used for loop instead if each for better speed/performance

   for(var i=0; i<$anchorTarget.length; i++){
            var $eachAnchor = $anchorTarget.eq(i);
            //get anchor markup from table
            var getAnchor = $eachAnchor.parent().html();
            //append anchor on created ul
            $("."+convertedMenuCls).append("<li>"+getAnchor+"</li>");
            //hide old icon - if you've icon as a image in old table markup
            $eachAnchor.parent().parent().find("img").hide();
            //Add new icon
            $eachAnchor.parent().parent().find("img").after("<span>+</span>");
        }

Table structure

enter image description here

UL Structure After conversion enter image description here

Here is working code: Convert Table structure menu to UL using jQuery

Melodics answered 29/12, 2016 at 6:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.