更改HTML表格单元格中的文本取决于数值。

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

changing the text in a html table cell depending on value

问题

$(function(){
  $("tr").each(function(){
    var col_val = $(this).find("td:eq(1)").text();
    if (col_val == "open"){
      $(this).addClass('table-primary');  //the selected class colors the row green//
    } else if (col_val == "in progress"){
      $(this).addClass('table-success');  //the selected class colors the row green//
    } else {
      $(this).addClass('table-secondary');
      var col_edit = $(this).find("td:eq(0)");
      $(col_edit).text('');
      // You can remove the text in the cell by setting it to an empty string as above.
    }
  });
});

This code is used to manipulate the content of table cells based on the value in another cell. When the status value is "closed," it removes the text in the corresponding cell so that you can't click the link anymore.

英文:

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

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

$(function(){
  $(&quot;tr&quot;).each(function(){
	
    var col_val = $(this).find(&quot;td:eq(1)&quot;).text();
    if (col_val == &quot;open&quot;){
      $(this).addClass(&#39;table-primary&#39;);  //the selected class colors the row green//
    } else if (col_val == &quot;in progress&quot;){
      $(this).addClass(&#39;table-success&#39;);  //the selected class colors the row green//
    } else {
      $(this).addClass(&#39;table-secondary&#39;);
      var col_edit = $(this).find(&quot;td:eq(0)&quot;);
     // console.log(&quot;jes&quot;);
     //console.log(col_edit);
      
      // addClass(&#39;h1&#39;) is just for testing, if I selected the right cell -&gt; this seems to work!!
      $(col_edit).addClass(&#39;h1&#39;);
    // PLEASE HELP HERE: Instead of a big h1 &#39;Edit&#39; in cell 3 i want to see nothing (&#39;&#39;)
    // these versions I tried:
      $(col_edit).text = &quot;&quot;;
      $(col_edit).innerHTML = &quot;&quot;;
      $(col_edit).value = &quot;&quot;;
      $(col_edit).text = &#39;&#39;;
      $(col_edit).innerHTML = &#39;&#39;;
      $(col_edit).value = &#39;&#39;;
      $(col_edit).addClass(&#39;hidden&#39;);
    }
  });
});

<!-- 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;!DOCTYPE HTML&gt;
&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
&lt;!-- UNIMPORTANT HEAD --&gt;
&lt;head th:fragment=&quot;header&quot;&gt;
&lt;meta charset=&quot;UTF-8&quot; /&gt;

&lt;title th:text=&quot;${title}&quot;&gt;DiKuKa&lt;/title&gt;
&lt;link rel=&quot;shortcut icon&quot; type=&quot;image/x-icon&quot; href=&quot;images/dikuka.ico&quot;&gt;
&lt;link rel=&quot;stylesheet&quot;
	href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css&quot;&gt;
&lt;link rel=&quot;stylesheet&quot;
	href=&quot;https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css&quot;
	integrity=&quot;sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh&quot;
	crossorigin=&quot;anonymous&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; th:href=&quot;@{/css/style.css}&quot; /&gt;
&lt;script
	src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script
	src=&quot;https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js&quot;&gt;&lt;/script&gt;
&lt;script
	src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
&lt;script th:src=&quot;@{/js/custom.js}&quot;&gt;&lt;/script&gt;

&lt;/head&gt;

	&lt;!-- THE IMPORTANT TABLE: --&gt;
	&lt;div class=&quot;table-responsive&quot;&gt; 
	&lt;table class=&quot;table table-hover&quot;&gt;
		&lt;thead class=&quot;thead-light&quot;&gt;
			&lt;tr&gt;
				&lt;th scope=&quot;col&quot;&gt;&lt;/th&gt;
				&lt;th scope=&quot;col&quot;&gt;status&lt;/th&gt;
			&lt;/tr&gt;
			&lt;/thead&gt;
			&lt;tbody&gt;
			&lt;tr&gt;  
				&lt;td&gt;&lt;a href=&quot;#someLink&quot;&gt;Edit&lt;/a&gt;&lt;/td&gt;
				&lt;td&gt;open&lt;/td&gt; 
			&lt;/tr&gt;
      	&lt;tr&gt;  
				&lt;td&gt;&lt;a href=&quot;#someLink&quot;&gt;Edit&lt;/a&gt;&lt;/td&gt;
				&lt;td&gt;in progress&lt;/td&gt; 
			&lt;/tr&gt;
      	&lt;tr&gt;  
				&lt;td&gt;&lt;a href=&quot;#someLink&quot;&gt;Edit&lt;/a&gt;&lt;/td&gt;
				&lt;td&gt;closed&lt;/td&gt; 
			&lt;/tr&gt;
			&lt;/tbody&gt;
	&lt;/table&gt;
	&lt;br&gt;
	&lt;hr&gt;
	&lt;/div&gt;
	&lt;br&gt;

<!-- end snippet -->

https://jsfiddle.net/q5hn6d72/1/

I'm trying to delete the text in a table cell using javascript depending on the value in another cell.
The purpose is that you should not be able to click the link of the text in that cell anymore when the status value is equal to "closed".
Please try the fiddle, at the bottom of the Javascript I marked the different approached that I tried so far. I managed to select the right cell with col_edit, otherwise the cell wouldn't be shown as a h1.

Any tips are welcome!

screenshot of the actual project

答案1

得分: 0

以下是您要翻译的内容:

"Simply you can check 1 more condition for closed as
else if (col_val == &quot;closed&quot;){ $(this).find(&quot;td:eq(0)&quot;).html(&#39;&#39;);

When closed then remove text of td


$(function(){
$("tr").each(function(){

var col_val = $(this).find("td:eq(1)").text();
//console.log(col_val);
if (col_val == "open"){
  $(this).addClass('table-primary');  //the selected class colors the row green//
} else if (col_val == "in progress"){
  $(this).addClass('table-success');  //the selected class colors the row green//
}  else if (col_val == "closed"){
   $(this).find("td:eq(0)").html('');
} else {
  $(this).addClass('table-secondary');
  var col_edit = $(this).find("td:eq(0)");
  console.log("jes");
 // console.log(col_edit);
  
  // addClass('h1') is just for testing, if I selected the right cell -> this seems to work!!
  $(col_edit).addClass('h1');
// PLEASE HELP HERE: Instead of a big h1 'Edit' in cell 3 i want to see nothing ('')
// these versions I tried:
  $(col_edit).text = "";
  $(col_edit).innerHTML = "";
  $(col_edit).value = "";
  $(col_edit).text = '';
  $(col_edit).innerHTML = '';
  $(col_edit).value = '';
  $(col_edit).addClass('hidden');
}

});
});







DiKuKa




status
Edit open
Edit in progress
Edit closed


"

英文:

Simply you can check 1 more condition for closed as
else if (col_val == &quot;closed&quot;){
$(this).find(&quot;td:eq(0)&quot;).html(&#39;&#39;);

When closed then remove text of td

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

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

$(function(){
  $(&quot;tr&quot;).each(function(){
	
    var col_val = $(this).find(&quot;td:eq(1)&quot;).text();
    //console.log(col_val);
    if (col_val == &quot;open&quot;){
      $(this).addClass(&#39;table-primary&#39;);  //the selected class colors the row green//
    } else if (col_val == &quot;in progress&quot;){
      $(this).addClass(&#39;table-success&#39;);  //the selected class colors the row green//
    }  else if (col_val == &quot;closed&quot;){
       $(this).find(&quot;td:eq(0)&quot;).html(&#39;&#39;);
    } else {
      $(this).addClass(&#39;table-secondary&#39;);
      var col_edit = $(this).find(&quot;td:eq(0)&quot;);
      console.log(&quot;jes&quot;);
     // console.log(col_edit);
      
      // addClass(&#39;h1&#39;) is just for testing, if I selected the right cell -&gt; this seems to work!!
      $(col_edit).addClass(&#39;h1&#39;);
    // PLEASE HELP HERE: Instead of a big h1 &#39;Edit&#39; in cell 3 i want to see nothing (&#39;&#39;)
    // these versions I tried:
      $(col_edit).text = &quot;&quot;;
      $(col_edit).innerHTML = &quot;&quot;;
      $(col_edit).value = &quot;&quot;;
      $(col_edit).text = &#39;&#39;;
      $(col_edit).innerHTML = &#39;&#39;;
      $(col_edit).value = &#39;&#39;;
      $(col_edit).addClass(&#39;hidden&#39;);
    }
  });
});

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

&lt;!DOCTYPE HTML&gt;
&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
&lt;!-- UNIMPORTANT HEAD --&gt;
&lt;head th:fragment=&quot;header&quot;&gt;
&lt;meta charset=&quot;UTF-8&quot; /&gt;

&lt;title th:text=&quot;${title}&quot;&gt;DiKuKa&lt;/title&gt;
&lt;link rel=&quot;shortcut icon&quot; type=&quot;image/x-icon&quot; href=&quot;images/dikuka.ico&quot;&gt;
&lt;link rel=&quot;stylesheet&quot;
	href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css&quot;&gt;
&lt;link rel=&quot;stylesheet&quot;
	href=&quot;https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css&quot;
	integrity=&quot;sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh&quot;
	crossorigin=&quot;anonymous&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; th:href=&quot;@{/css/style.css}&quot; /&gt;
&lt;script
	src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script
	src=&quot;https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js&quot;&gt;&lt;/script&gt;
&lt;script
	src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
&lt;script th:src=&quot;@{/js/custom.js}&quot;&gt;&lt;/script&gt;

&lt;/head&gt;

	&lt;!-- THE IMPORTANT TABLE: --&gt;
	&lt;div class=&quot;table-responsive&quot;&gt; 
	&lt;table class=&quot;table table-hover&quot;&gt;
		&lt;thead class=&quot;thead-light&quot;&gt;
			&lt;tr&gt;
				&lt;th scope=&quot;col&quot;&gt;&lt;/th&gt;
				&lt;th scope=&quot;col&quot;&gt;status&lt;/th&gt;
			&lt;/tr&gt;
			&lt;/thead&gt;
			&lt;tbody&gt;
			&lt;tr&gt;  
				&lt;td&gt;&lt;a href=&quot;#someLink&quot;&gt;Edit&lt;/a&gt;&lt;/td&gt;
				&lt;td&gt;open&lt;/td&gt; 
			&lt;/tr&gt;
      	&lt;tr&gt;  
				&lt;td&gt;&lt;a href=&quot;#someLink&quot;&gt;Edit&lt;/a&gt;&lt;/td&gt;
				&lt;td&gt;in progress&lt;/td&gt; 
			&lt;/tr&gt;
      	&lt;tr&gt;  
				&lt;td&gt;&lt;a href=&quot;#someLink&quot;&gt;Edit&lt;/a&gt;&lt;/td&gt;
				&lt;td&gt;closed&lt;/td&gt; 
			&lt;/tr&gt;
			&lt;/tbody&gt;
	&lt;/table&gt;
	&lt;br&gt;
	&lt;hr&gt;
	&lt;/div&gt;
	&lt;br&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 18:41:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577105.html
匿名

发表评论

匿名网友

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

确定