There’s quite a few examples around, but I couldn’t find one covering a table. The intent is to have the Comments column’s input field shown as mandatory, if value Rejected or Discuss is selected in an adjacent column.
Here’s a Fiddle
I’ve seen that there should be an onchange added to each row of the table, which would call a function, but there should be an id for each row?
This is how I’m building the table, using GAS:
JavaScript
x
38
38
1
function createTable(dataArray) {
2
if (dataArray && dataArray !== undefined && dataArray.length != 0) {
3
var option = "<select name='D1'>" +
4
"<option value='empty'></option>" +
5
"<option value='Approved'>Approved</option>" +
6
"<option value='Discuss'>Discuss</option>" +
7
"<option value='Rejected'>Rejected</option>" +
8
"</select>";
9
var textBox = "<input type='text' name='comments'/>"
10
11
var result = "<div class='card'><table class='table table-borderless table-hover' id='dtable'>" +
12
"<thead style='white-space: nowrap'>" +
13
"<tr>" + //Change table headings to match witht he Google Sheet
14
"<th class='text-center'>Notes</th>" +
15
"<th class='text-center'>Approval</th>" +
16
"<th class='text-center'>Comments</th>" +
17
"</tr>" +
18
"</thead>" +
19
"<tbody>";
20
for (var i = 0; i < dataArray.length; i++) {
21
result += "<tr>";
22
for (var j = 0; j < dataArray[i].length; j++) { //Checks if Link columns has data and pushes it as a link into the table
23
result += "<td class='align-middle' style='word-wrap: break-word;max-width: 160px;text-align:center'>" + dataArray[i][j] + "</td>";
24
}
25
result += "<td class='align-middle' style='text-align:center'>" + option + "</td>";
26
result += "<td class='align-middle' style='text-align:center'>" + textBox + "</td>";
27
result += "</tr>";
28
}
29
result += "</tbody></table></div><br>";
30
var div = document.getElementById('search-results');
31
div.innerHTML = result;
32
} else {
33
var div = document.getElementById('search-results');
34
div.innerHTML = "Data not found!";
35
return;
36
}
37
}
38
Appreciate your help!
Advertisement
Answer
Add onChange event for select box like selectValueChanged and then inside the method pass the current object. Using current object , find the selected value and then perform your operations.
JavaScript
1
10
10
1
function selectValueChanged(cur){
2
if(cur.value == "Rejected" || cur.value == "Discuss"){
3
cur.parentElement.nextElementSibling.children[0].required = true;
4
cur.parentElement.nextElementSibling.children[0].placeholder = "this is requred";
5
}else{
6
cur.parentElement.nextElementSibling.children[0].required = false;
7
cur.parentElement.nextElementSibling.children[0].placeholder = "Optional";
8
}
9
}
10