Looping through multi tables in jQuery and adding data-labels to each td.

huangapple go评论67阅读模式
英文:

Looping through multi tables in jQuery and adding data-labels to each td

问题

I have this code that takes the th values and adds them to data-label's for each td. I then convert them in CSS to show a mobile view of the table. It all works fine but when you have multiple tables with different headers it takes the last table on the page and adds these data-label's to all the tables.

I cant seem to see what I am doing wrong and wondered if anyone could help?

$('table').each(function() { 
  if ($(this).find('th').length > 1) { 
    thcount = 0
    $(this).find('th').each(function() {
      eachth = $(this)	
      thcount=thcount+1		
      $('tr td:nth-child('+thcount+')').each(function(a) {
        if (eachth.html() == '') {
          $(this).css('padding-left', '6px');
        } else {
          $(this).attr('data-label', eachth.html() + '\u00A0');	
        }
      });
    });
  }
});	
英文:

I have this code that takes the th values and adds them to data-label's for each td. I then convert them in CSS to show a mobile view of the table. It all works fine but when you have multiple tables with different headers it takes the last table on the page and adds these data-label's to all the tables.

I cant seem to see what I am doing wrong and wondered if anyone could help?

$('table').each(function() { 
  if ($(this).find('th').length > 1) { 
    thcount = 0
    $(this).find('th').each(function() {
	  eachth = $(this)	
	  thcount=thcount+1		
	  $('tr td:nth-child('+thcount+')').each(function(a) {
		if (eachth.html() == '') {
		  $(this).css('padding-left', '6px');
		} else {
		  $(this).attr('data-label', eachth.html() + '\u00A0');	
		}
	  });
	});
  }
});	

答案1

得分: 0

以下是您要翻译的内容:

"一次处理一个 th 更好。例如,像这样:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;table&#39;).each(function(){
  const table = $(this);
  table.find(&#39;th&#39;).each(function(index) {
    table.find(`td:nth-child(${index + 1})`).attr( &#39;data-label&#39;, $(this).text() + &#39; &#39; );
  })
})

<!-- language: lang-css -->

table th,
table td {
  padding: 4px;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;1&lt;/th&gt;
    &lt;th&gt;2&lt;/th&gt;
    &lt;th&gt;3&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;2&lt;/td&gt;
    &lt;td&gt;3&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;2&lt;/td&gt;
    &lt;td&gt;3&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;br&gt;
&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;4&lt;/th&gt;
    &lt;th&gt;5&lt;/th&gt;
    &lt;th&gt;6&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;2&lt;/td&gt;
    &lt;td&gt;3&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;2&lt;/td&gt;
    &lt;td&gt;3&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

<!-- end snippet -->
"

英文:

It is better to do it in one th each. For example, like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;table&#39;).each(function(){
  const table = $(this);
  table.find(&#39;th&#39;).each(function(index) {
    table.find(`td:nth-child(${index + 1})`).attr( &#39;data-label&#39;, $(this).text() + &#39; &#39; );
  })
})

<!-- language: lang-css -->

table th,
table td {
  padding: 4px;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;1&lt;/th&gt;
    &lt;th&gt;2&lt;/th&gt;
    &lt;th&gt;3&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;2&lt;/td&gt;
    &lt;td&gt;3&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;2&lt;/td&gt;
    &lt;td&gt;3&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;br&gt;
&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;4&lt;/th&gt;
    &lt;th&gt;5&lt;/th&gt;
    &lt;th&gt;6&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;2&lt;/td&gt;
    &lt;td&gt;3&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;2&lt;/td&gt;
    &lt;td&gt;3&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月13日 19:11:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定