B
B
Bogdan2015-11-08 21:01:40
JavaScript
Bogdan, 2015-11-08 21:01:40

How to do a search on the columns of an HTML table?

Hello! There is a site on ModX and a table with data in HTML format on one of the pages. Tell me how to search through the columns of this table so that when you enter it in a text field, the results of matching the query are displayed immediately on the page?
I tried to do it in AngularJS, but there must be a simpler solution.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vivers, 2019-08-12
@Vivers

function tableSearch() {
    var phrase = document.getElementById('**сюда id поисковой строки**');
    var table = document.getElementById('**сюда id столбца**');
    var regPhrase = new RegExp(phrase.value, 'i');
    var flag = false;
    for (var i = 1; i < table.rows.length; i++) {
        flag = false;
        for (var j = table.rows[i].cells.length - 1; j >= 0; j--) {
            flag = regPhrase.test(table.rows[i].cells[j].innerHTML);
            if (flag) break;
        }
        if (flag) {
            table.rows[i].style.display = "";
        } else {
            table.rows[i].style.display = "none";
        }

    }
}

Sorry it's over 3 years old

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question