Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
89 views
in Technique[技术] by (71.8m points)

How to count html rows that match criteria in two ore more columns with javascript?

I need to print a table with some total and then a second table with detailed events. Then I need to change the total of each total of the first table considering the number of rows of the second table.

I did it considering just one criteria of the second table. I need to consider 3 criteria (ID, MONTH and YEAR).

function get_conts() {
  var rows = document.getElementById('one').rows,
      len = rows.length,
      i,
      cellNum = 1,
      count = 0,
      cell;

  for (i = 1; i < len; i++) {
    cell = rows[i].cells[cellNum];
    get_workers(cell.innerHTML);
    console.log(cell.innerHTML);
  }
}

function get_workers(id_ent) {
  var rows = document.getElementById('two').rows,
      len = rows.length,
      i,
      cellNum = 1,
      count = 0,
      cell;

  for (i = 1; i < len; i++) {
    cell = rows[i].cells[cellNum];

    if (cell.innerHTML === id_ent.toString()) {
      count++;
    }
  }
  

  var total_workers = document.getElementById('total_' + id_ent).innerHTML;

  document.getElementById('total_' + id_ent).innerHTML = (total_workers - count).toString();
  console.log(total_workers + "-" + count);
}

get_conts();

https://jsfiddle.net/ke0u2nc9/

EDIT 1: I think concatenating ID, MONTH and YEAR it will work.

question from:https://stackoverflow.com/questions/65602796/how-to-count-html-rows-that-match-criteria-in-two-ore-more-columns-with-javascri

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I did it concatenating the id, month and year:

function get_conts() {
  var rows = document.getElementById('one').rows,
      len = rows.length,
      i,
      cell_id = 1,
      cell_month = 2,
      cell_year = 3,
      count = 0,
      cell;

  for (i = 1; i < len; i++) {
    cellvalue_id = rows[i].cells[cell_id];
    cellvalue_month = rows[i].cells[cell_month];
    cellvalue_year = rows[i].cells[cell_year];
    get_workers(cellvalue_id.innerHTML + "_" + cellvalue_month.innerHTML + "_" + cellvalue_year.innerHTML);
  }
}

function get_workers(id_ent) {
  var rows = document.getElementById('two').rows,
      len = rows.length,
      i,
      cell_id = 1,
      cell_month = 2,
      cell_year = 3,
      count = 0,
      cell;

  for (i = 1; i < len; i++) {
    cellvalue_id = rows[i].cells[cell_id];
    cellvalue_month = rows[i].cells[cell_month];
    cellvalue_year = rows[i].cells[cell_year];
    concatenated_text = cellvalue_id.innerHTML + "_" + cellvalue_month.innerHTML + "_" + cellvalue_year.innerHTML);

    if (concatenated_text === id_ent.toString()) {
      count++;
    }
  }
  

  var total_workers = document.getElementById('total_' + id_ent).innerHTML;

  document.getElementById('total_' + id_ent).innerHTML = (total_workers - count).toString();
  console.log(total_workers + "-" + count);
}

get_conts();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...