英文:
How to dynamically define the selected option on the select tag. (Using template literals)
问题
I am trying to create some html elements with template literals. I know I could have a for loop and create the options one by one but I find that to be messy code. This is the best way that I could get working. Is there an easier way that I am completely missing?
/* creating the table */
let propertyTable = document.createElement('table');
propertyTable.classList.add('expand', 'ignore');
let selected = ["", "", "", "", "", ""];
switch (this.type) {
case "number":
selected[0] = "selected";
break;
case "dropDown":
selected[1] = "selected";
break;
case "autoSet":
selected[2] = "selected";
break;
case "plotly":
selected[3] = "selected";
break;
case "gradeGroup":
selected[4] = "selected";
break;
case "generate":
selected[5] = "selected";
break;
}
/* starting the property table */
propertyTable.innerHTML = `<tr class="ignore">
<th class="ignore">ID</th>
<td class="ignore"><input class="tile ignore" value="${this.id}"></td>
</tr>
<tr class="ignore">
<th class="ignore">Type</th>
<td class="ignore">
<select class="tile ignore">
<option value="number" ${selected[0]} class="ignore">number</option>
<option value="dropDown" ${selected[1]} class="ignore">dropDown</option>
<option value="autoSet" ${selected[2]} class="ignore">autoSet</option>
<option value="plotly" ${selected[3]} class="ignore">plotly</option>
<option value="gradeGroup" ${selected[4]} class="ignore">gradeGroup</option>
<option value="generate" ${selected[5]} class="ignore">generate</option>
</select>
</td>
</tr>`;
英文:
I am trying to create some html elements with template literals. I know I could have a for loop and create the options one by one but I find that to be messy code. This is the best way that I could get working. Is there an easier way that I am completely missing?
/* creating the table */
let propertyTable = document.createElement('table');
propertyTable.classList.add('expand','ignore');
let selected = ["", "", "", "", "", ""];
switch (this.type) {
case "number":
selected[0] = "selected";
break;
case "dropDown":
selected[1] = "selected";
break;
case "autoSet":
selected[2] = "selected";
break;
case "plotly":
selected[3] = "selected";
break;
case "gradeGroup":
selected[4] = "selected";
break;
case "generate":
selected[5] = "selected";
break;
}
/* starting the property table */
propertyTable.innerHTML = `<tr class="ignore">
<th class="ignore">ID</th>
<td class="ignore"><input class="tile ignore" value="${this.id}"></td>
</tr>
<tr class="ignore">
<th class="ignore">Type</th>
<td class="ignore">
<select class="tile ignore">
<option value="number" ${selected[0]} class="ignore">number</option>
<option value="dropDown" ${selected[1]} class="ignore">dropDown</option>
<option value="autoSet" ${selected[2]} class="ignore">autoSet</option>
<option value="plotly" ${selected[3]} class="ignore">plotly</option>
<option value="gradeGroup" c${selected[4]} lass="ignore">gradeGroup</option>
<option value="generate" ${selected[5]} class="ignore">generate</option>
</select>
</td>
</tr>`;
答案1
得分: 1
// 替代大型 `switch` 语句,使用类型名称的数组。
let selected = ["", "", "", "", "", ""];
let types = ["number", "dropDown", "autoSet", "plotly", "gradeGroup", "generate"];
let index = types.indexOf(this.type);
if (index >= 0) {
selected[index] = "selected";
}
英文:
Instead of the big switch
statement, use an array of type names.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let selected = ["", "", "", "", "", ""];
let types = ["number", "dropDown", "autoSet", "plotly", "gradeGroup", "generate"];
let index = types.indexOf(this.type);
if (index >= 0) {
selected[index] = "selected";
}
<!-- end snippet -->
答案2
得分: 1
以下是您要翻译的内容:
您是否可以尝试一个更简单的函数,您可以使用相关类型调用它,类似于:
(请注意,这是伪代码,根据需要进行调整)
function select_class(selector) do
if (this.type == selector) do
"selected"
else
""
end
end
然后在创建选择选项时调用它:
/* 创建表格 */
let propertyTable = document.createElement('table');
propertyTable.classList.add('expand','ignore');
/* 开始属性表格 */
propertyTable.innerHTML = `
`;
```
但实际上,最好有一个用于构建每个选项的函数。类似于:
function create_option(selector) do
`<option value="${selector}" ${select_class(selector)} class="ignore">${selector}</option>`
end
您可以像这样使用它:
/* 开始属性表格 */
propertyTable.innerHTML = `<tr class="ignore">
<th class="ignore">ID</th>
<td class="ignore"><input class="tile ignore" value="${this.id}"></td>
</tr>
<tr class="ignore">
<th class="ignore">Type</th>
<td class="ignore">
<select class="tile ignore">
${create_option("number")}
${create_option("dropDown")}
${create_option("autoSet")}
${create_option("plotly")}
${create_option("gradeGroup")}
${create_option("generate")}
</select>
</td>
</tr>`;
英文:
Could you try a simpler function that you call with the relevant type,
something like:
(Note this is pseudocode, adjust as necessary)
function select_class(selector) do
if (this.type == selector) do
"selected"
else
""
end
end
Then when you're creating the select options, call it:
/* creating the table */
let propertyTable = document.createElement('table');
propertyTable.classList.add('expand','ignore');
/* starting the property table */
propertyTable.innerHTML = `<tr class="ignore">
<th class="ignore">ID</th>
<td class="ignore"><input class="tile ignore" value="${this.id}"></td>
</tr>
<tr class="ignore">
<th class="ignore">Type</th>
<td class="ignore">
<select class="tile ignore">
<option value="number" ${select_class("number")} class="ignore">number</option>
<option value="dropDown" ${select_class("dropDown")} class="ignore">dropDown</option>
<option value="autoSet" ${select_class("autoSet")} class="ignore">autoSet</option>
<option value="plotly" ${select_class("plotly")} class="ignore">plotly</option>
<option value="gradeGroup" ${select_class("gradeGroup")} lass="ignore">gradeGroup</option>
<option value="generate" ${select_class("generate")} class="ignore">generate</option>
</select>
</td>
</tr>`;
But really, having a function to build each option would be ideal. Something like:
function create_option(selector) do
`<option value="${selector}" ${select_class(selector)} class="ignore">${selector}</option>`
end
Which you'd use something like:
/* starting the property table */
propertyTable.innerHTML = `<tr class="ignore">
<th class="ignore">ID</th>
<td class="ignore"><input class="tile ignore" value="${this.id}"></td>
</tr>
<tr class="ignore">
<th class="ignore">Type</th>
<td class="ignore">
<select class="tile ignore">
${create_option("number"}
${create_option("dropDown"}
${create_option("autoSet"}
${create_option("plotly"}
${create_option("gradeGroup"}
${create_option("generate"}
</select>
</td>
</tr>`;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论