Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Exclude a specific td from querySelectorAll

hello there I have an HTML table which I want to exclude a specific td from it and i dont know how.

HTML :

 <table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap"
                           cellspacing="0" width="100%">
                        <thead>
                        <tr>
                            <th>row</th>
                            <th>title</th>
                            <th>User</th>
                            <th> Type </th>
                            <th> date</th>
                            <th>func</th>
                        </tr>
                        </thead>
                        <tbody>                            
                            {% for item in knowledges %}
                                <tr>
                                    <td>{{ forloop.counter }}</td>
                                    <td>{{item.KnowledgeTitle}}</td>
                                    <td>{{ item.CreatorUserID.get_full_name }}</td>
                                    <td>                             
                                        {% if item.Type == 1 %}
                                         Show Something...
                                        {% endif %}               
                                    </td>
                                    <td id="{{ item.CreateDate }}{{ forloop.counter }}"></td>
                                    <td id="excluded">
                                        <a class="btn btn-success" href="/KnowledgeView/{{ item.KnowledgeCode }}"><i
                                            class="fa fa-eye">Show</i> </a>
                                        <button title="show" type="button" onclick="getClickID(this.id)" id="{{item.KnowledgeCode}}" class="btn btn-info" data-toggle="modal" data-target="#exampleModalLong" >Click</button>                                       
                                    </td>
                                </tr>
                            {% endfor %}

So basicly i want to exclude the second td <td>{{item.KnowledgeTitle}}</td> which is Title.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function htmlToCSV(html, filename) {
    var data = [];
    var rows = document.querySelectorAll("table tr");
            
    for (var i = 0; i < rows.length; i++) {
        var row = [], cols = rows[i].querySelectorAll("td, th");
                
        for (var j = 0; j < cols.length; j++) {
                row.push(cols[j].innerText);
        }
                
        data.push(row.join(","));       
    }

    downloadCSVFile(data.join("\n"), filename);

Please dont get confused with some Django-template-tags

>Solution :

To do what you require you can place a class on the cells to be excluded and then target them with :not() in your querySelectorAll() selector.

Also note that your code can be made more succinct by using map(). Try this:

function htmlToCSV(html, filename) {
  var rows = document.querySelectorAll("table tr");
  let csv = Array.from(rows).map(row => {
    return Array.from(row.querySelectorAll('td:not(.export-exclude), th:not(.export-exclude)')).map(cell => cell.innerText).join(',');
  });
  console.log(csv);
  
  //downloadCSVFile(data.join("\n"), filename);
}

htmlToCSV();
<table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th>row</th>
      <th class="export-exclude">title</th>
      <th>User</th>
      <th> Type </th>
      <th> date</th>
      <th>func</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td class="export-exclude">item.KnowledgeTitle 1</td>
      <td>item.CreatorUserID.get_full_name 1</td>
      <td>
        Show Something 1...
      </td>
      <td id=""></td>
      <td id="excluded">
        <a class="btn btn-success" href="/KnowledgeView/{{ item.KnowledgeCode }}">
          <i class="fa fa-eye">Show</i>
        </a>
        <button title="show" type="button" onclick="getClickID(this.id)" id="{{item.KnowledgeCode}}" class="btn btn-info" data-toggle="modal" data-target="#exampleModalLong">Click</button>
      </td>
    </tr>
    <tr>
      <td>2</td>
      <td class="export-exclude">item.KnowledgeTitle 2</td>
      <td>item.CreatorUserID.get_full_name 2</td>
      <td>
        Show Something 2...
      </td>
      <td id=""></td>
      <td id="excluded">
        <a class="btn btn-success" href="/KnowledgeView/{{ item.KnowledgeCode }}">
          <i class="fa fa-eye">Show</i>
        </a>
        <button title="show" type="button" onclick="getClickID(this.id)" id="{{item.KnowledgeCode}}" class="btn btn-info" data-toggle="modal" data-target="#exampleModalLong">Click</button>
      </td>
    </tr>
  </tbody>
</table>

Finally, the html argument of your function does nothing and can be removed.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading