最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript - Search Table Column for String - Stack Overflow

programmeradmin2浏览0评论

I am searching my Table Column for the string "None". It does this but I am unable to get the row number after. I am attempting to use the "rowIndex" attribute. Not sure why it is pulling "Not a Number" (NaN). Table is 50 rows 10 cols. I am assuming it may have to do with that I am pulling from a column instead of Row.

function F0416()                                                                   
{

var tab = document.getElementById('part1Table');
var l = tab.rows.length;
var s = '';
for ( var i = 0; i < l; i++ )
{var tr = tab.rows[i];
var cll = tr.cells[2];                                                              
s += ' ' + cll.innerText;
}

var y = (s.indexOf('None') != -1)                                                   
document.write(this.rowIndex + 1)

I am searching my Table Column for the string "None". It does this but I am unable to get the row number after. I am attempting to use the "rowIndex" attribute. Not sure why it is pulling "Not a Number" (NaN). Table is 50 rows 10 cols. I am assuming it may have to do with that I am pulling from a column instead of Row.

function F0416()                                                                   
{

var tab = document.getElementById('part1Table');
var l = tab.rows.length;
var s = '';
for ( var i = 0; i < l; i++ )
{var tr = tab.rows[i];
var cll = tr.cells[2];                                                              
s += ' ' + cll.innerText;
}

var y = (s.indexOf('None') != -1)                                                   
document.write(this.rowIndex + 1)
Share Improve this question edited Jul 31, 2017 at 21:29 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 5, 2014 at 21:36 JohnBJohnB 1511 gold badge2 silver badges13 bronze badges 1
  • 1 What is the value of "this"? Make sure it is the current row. – krisk Commented Mar 5, 2014 at 21:44
Add a ment  | 

2 Answers 2

Reset to default 2

Instead of concatenating all the values of the column into a string and searching the string, you could instead test the value of each cell in the column for the value 'None'. Then you would know the row number from the loop counter, and you could halt the loop if you find it instead of iterating over every row.

It would look more like this:

for ( var i = 0; i < l; i++ ) {
    var tr = tab.rows[i];
    var cll = tr.cells[2];                                                              
    if(cll.innerText.indexOf('None') != -1) {
        document.write(i + 1);
        break;
    }
}

You could also return the value of the row instead of outputting it.

I would remend using a rich javascript library like JQuery.

Then given the following HTML:

<table>
    <tr><td>Row 1- Column 1</td><td>Row 1 - Column 2</td>
    <tr><td>none</td><td>Row 2 - Column 2</td>
    <tr><td>Row 3- Column 1</td><td>Row 3 - Column 2</td>
</table>

You can use the following to get all the rows containing none:

var rows = $('table tr').filter(":contains('none')");

Have a look at this Fiddle example.

发布评论

评论列表(0)

  1. 暂无评论