Home > Enterprise >  Hide table row if cells with input are empty or zero
Hide table row if cells with input are empty or zero

Time:07-15

i'm trying to delete rows in a table that are empty but not in the full table only from "tr (" x " 5)" and from "td (" i " 7) input".

I was trying to sum the values from the row from the inputs and if the result is "0", "null" or "undefined". But i can't get it to work.

The table has right now 150 tr and 15 td but this should be set dynamicaly. The maximum to the bottom of the table from the 5th for "tr" and the maximum to the right for the "td" from the 7th.

$(".produkttabelle_alt tr:nth-child(n 5)").each(function(i, obj){ 

var sum = 0;

$(this).children("td:nth-child(n 7)").children().each(function(){ 

var input_val_sum = $(this).val(); 

sum  = parseInt(input_val_sum); 
}); 


if ((sum != null) || (sum != "") || (sum != "undefined") || (sum != 0) ) {

$(this).closest(".old_row_tr").hide(); 

}});

CodePudding user response:

Consider the following.

$(".produkttabelle_alt tr:nth-child(5n)").each(function(i, row) {
  var sum = 0;
  $("td:nth-child(7n)", row).each(function(j, cell) {
    sum  = parseInt($("input", cell).val());
  });
  if ((sum != null) || (sum != "") || (sum != "undefined") || (sum != 0)) {
    console.log("Hide Row "   ($(row).index()   1));
    $(row).hide();
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table >
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
</table>

Please see: https://api.jquery.com/nth-child-selector/

Because jQuery's implementation of :nth- selectors is strictly derived from the CSS specification, the value of n is "1-indexed", meaning that the counting starts at 1.

If you want to to target every 5th row, use 5n. If you want to target every 7th cell, use 7n.

Update

The Value of an input will always be a String, so it is unlikely to be null or undefined. It may be letter or some other chracter. So you may consider using isNaN() function.

if(isNaN(sum) || sum == 0){
  row.hide();
}

This will catch Empty or 0 value rows. Here is an example:

$(".produkttabelle_alt tr:nth-child(5n)").each(function(i, row) {
  var sum = 0;
  $("td:nth-child(7n)", row).each(function(j, cell) {
    sum  = parseInt($("input", cell).val());
  });
  if (isNaN(sum) || (sum == 0)) {
    console.log("Hide Row "   ($(row).index()   1));
    $(row).hide();
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table >
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <!-- Test Row -->
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="0" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="0" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="0" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
  <!-- Test Row -->
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="" /></td>
    <td>22</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td><input type="text" value="7" /></td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td><input type="text" value="14" /></td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td><input type="text" value="21" /></td>
    <td>22</td>
  </tr>
</table>

CodePudding user response:

Thanks you @Twisty i could figuere out the problem. Thanks you very much! This snippet now works for adding a class to rows that are empty or the sum of the children 7n is 0.

 $(".produkttabelle_alt tr:nth-child(n 5)").each(function(i, obj){
        var sum = 0;

        $(this).children("td:nth-child(n 7)").children().each(function(){
          var input_val_sum = $(this).val();
          sum  = parseInt(input_val_sum);
            
        });
        
        if (isNaN(sum) || (sum == 0)) {
            
            $(this).closest(".old_row_tr").addClass("not_ordered");
        }   
});
  • Related