如何动态定义选择标签上的选项。 (使用模板文字)

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

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(&#39;table&#39;);
propertyTable.classList.add(&#39;expand&#39;,&#39;ignore&#39;);
let selected = [&quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;];
switch (this.type) {
case &quot;number&quot;:
selected[0] = &quot;selected&quot;;
break;
case &quot;dropDown&quot;:
selected[1] = &quot;selected&quot;;
break;
case &quot;autoSet&quot;:
selected[2] = &quot;selected&quot;;
break;
case &quot;plotly&quot;:
selected[3] = &quot;selected&quot;;
break;
case &quot;gradeGroup&quot;:
selected[4] = &quot;selected&quot;;
break;
case &quot;generate&quot;:
selected[5] = &quot;selected&quot;;
break;
}
/* starting the property table */
propertyTable.innerHTML = `&lt;tr class=&quot;ignore&quot;&gt;
&lt;th class=&quot;ignore&quot;&gt;ID&lt;/th&gt;
&lt;td class=&quot;ignore&quot;&gt;&lt;input class=&quot;tile ignore&quot; value=&quot;${this.id}&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&quot;ignore&quot;&gt;
&lt;th class=&quot;ignore&quot;&gt;Type&lt;/th&gt;
&lt;td class=&quot;ignore&quot;&gt;
&lt;select class=&quot;tile ignore&quot;&gt;
&lt;option value=&quot;number&quot; ${selected[0]} class=&quot;ignore&quot;&gt;number&lt;/option&gt;
&lt;option value=&quot;dropDown&quot; ${selected[1]} class=&quot;ignore&quot;&gt;dropDown&lt;/option&gt;
&lt;option value=&quot;autoSet&quot; ${selected[2]} class=&quot;ignore&quot;&gt;autoSet&lt;/option&gt;
&lt;option value=&quot;plotly&quot; ${selected[3]} class=&quot;ignore&quot;&gt;plotly&lt;/option&gt;
&lt;option value=&quot;gradeGroup&quot; c${selected[4]} lass=&quot;ignore&quot;&gt;gradeGroup&lt;/option&gt;
&lt;option value=&quot;generate&quot; ${selected[5]} class=&quot;ignore&quot;&gt;generate&lt;/option&gt;
&lt;/select&gt;
&lt;/td&gt;
&lt;/tr&gt;`;

答案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 = [&quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;];
let types = [&quot;number&quot;, &quot;dropDown&quot;, &quot;autoSet&quot;, &quot;plotly&quot;, &quot;gradeGroup&quot;, &quot;generate&quot;];
let index = types.indexOf(this.type);
if (index &gt;= 0) {
selected[index] = &quot;selected&quot;;
}

<!-- 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 = `

ID

Type

`;
```

但实际上,最好有一个用于构建每个选项的函数。类似于:

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
&quot;selected&quot;
else
&quot;&quot;
end
end

Then when you're creating the select options, call it:

/* creating the table */
let propertyTable = document.createElement(&#39;table&#39;);
propertyTable.classList.add(&#39;expand&#39;,&#39;ignore&#39;);
/* starting the property table */
propertyTable.innerHTML = `&lt;tr class=&quot;ignore&quot;&gt;
&lt;th class=&quot;ignore&quot;&gt;ID&lt;/th&gt;
&lt;td class=&quot;ignore&quot;&gt;&lt;input class=&quot;tile ignore&quot; value=&quot;${this.id}&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&quot;ignore&quot;&gt;
&lt;th class=&quot;ignore&quot;&gt;Type&lt;/th&gt;
&lt;td class=&quot;ignore&quot;&gt;
&lt;select class=&quot;tile ignore&quot;&gt;
&lt;option value=&quot;number&quot; ${select_class(&quot;number&quot;)} class=&quot;ignore&quot;&gt;number&lt;/option&gt;
&lt;option value=&quot;dropDown&quot; ${select_class(&quot;dropDown&quot;)} class=&quot;ignore&quot;&gt;dropDown&lt;/option&gt;
&lt;option value=&quot;autoSet&quot; ${select_class(&quot;autoSet&quot;)} class=&quot;ignore&quot;&gt;autoSet&lt;/option&gt;
&lt;option value=&quot;plotly&quot; ${select_class(&quot;plotly&quot;)} class=&quot;ignore&quot;&gt;plotly&lt;/option&gt;
&lt;option value=&quot;gradeGroup&quot; ${select_class(&quot;gradeGroup&quot;)} lass=&quot;ignore&quot;&gt;gradeGroup&lt;/option&gt;
&lt;option value=&quot;generate&quot; ${select_class(&quot;generate&quot;)} class=&quot;ignore&quot;&gt;generate&lt;/option&gt;
&lt;/select&gt;
&lt;/td&gt;
&lt;/tr&gt;`;

But really, having a function to build each option would be ideal. Something like:

function create_option(selector) do
`&lt;option value=&quot;${selector}&quot; ${select_class(selector)} class=&quot;ignore&quot;&gt;${selector}&lt;/option&gt;`
end

Which you'd use something like:

  /* starting the property table */
propertyTable.innerHTML = `&lt;tr class=&quot;ignore&quot;&gt;
&lt;th class=&quot;ignore&quot;&gt;ID&lt;/th&gt;
&lt;td class=&quot;ignore&quot;&gt;&lt;input class=&quot;tile ignore&quot; value=&quot;${this.id}&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&quot;ignore&quot;&gt;
&lt;th class=&quot;ignore&quot;&gt;Type&lt;/th&gt;
&lt;td class=&quot;ignore&quot;&gt;
&lt;select class=&quot;tile ignore&quot;&gt;
${create_option(&quot;number&quot;}
${create_option(&quot;dropDown&quot;}
${create_option(&quot;autoSet&quot;}
${create_option(&quot;plotly&quot;}
${create_option(&quot;gradeGroup&quot;}
${create_option(&quot;generate&quot;}
&lt;/select&gt;
&lt;/td&gt;
&lt;/tr&gt;`;

huangapple
  • 本文由 发表于 2023年3月7日 01:28:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653984.html
匿名

发表评论

匿名网友

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

确定