如何在AppScript中将对象键和值映射到选项HTML标签中?

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

How can I map the object key and value in option HTML tags in appscript

问题

我从Code.gs中的一个函数中检索到了以下对象。我想将键值对传递到我的HTML下拉按钮中。

如何将对象的键和值映射到选项HTML标签中?

HTML

<div class="form-group">
    <? var data = listAccounts() ?>
    <optgroup label="Ad Accounts">
        <? for (i=0; i<data.length; i++) { ?>
        <option value="<?= data[i] ?>"><?= data[i] ?></option>
        <? } ?>
    </optgroup>
</div>

从函数listAccounts()返回的示例对象

{ 
  'abc': 'accountSummaries/123',
  'def': 'accountSummaries/124',
  'ghi': 'accountSummaries/125',
  'jkl Accounts': 'accountSummaries/1405'
}
英文:

I am retrieving this below object from a function in Code. gs. I would like to pass the key-value pair into my HTML drop-down button.

How can I map the object key and value in option HTML tags?

HTML

&lt;div class=&quot;form-group&quot;&gt;
        &lt;? var data = listAccounts() ?&gt;
    &lt;optgroup label=&quot;Ad Accounts&quot;&gt;
        &lt;? for (i=0; i&lt;data.length; i++) { ?&gt;
        &lt;option value=&quot;&lt;?= data[i] ?&gt;&quot;&gt; &lt;?= data[i] ?&gt;) &lt;/option&gt;
    &lt;/optgroup&gt;
&lt;/div&gt;

Example Object Returned from a function listAccounts()

{ 
  &#39;abc&#39;: &#39;accountSummaries/123&#39;,
  &#39;def&#39;: &#39;accountSummaries/124&#39;,
  &#39;ghi&#39;: &#39;accountSummaries/125&#39;,
  &#39;jkl Accounts&#39;: &#39;accountSummaries/1405&#39;
}

答案1

得分: 2

以下是翻译好的部分:

嗯,这是Javascript部分,所以我猜...这应该可以完成任务?
https://jsfiddle.net/MarinHTML/z29o5kaj/11/

var abc = { 'abc': 'accountSummaries/123',
  'def': 'accountSummaries/124',
  'ghi': 'accountSummaries/125',
  'jkl Accounts': 'accountSummaries/1405'
  };

for(let i=0;i&lt;Object.keys(abc).length;i++) {
    $('#gg').append('<option data-key="'+Object.keys(abc)[i]+'" data-value="'+Object.values(abc)[i]+'">'+Object.values(abc)[i]+'</option>')
}

^ 使用jQuery的示例映射。或者你想用PHP来做吗?这也可以工作,但你必须指出你想要键和值的位置。

英文:

Hmm, it's the Javascript section, so I guess... this should do the job?
https://jsfiddle.net/MarinHTML/z29o5kaj/11/

var abc = { &#39;abc&#39;: &#39;accountSummaries/123&#39;,
  &#39;def&#39;: &#39;accountSummaries/124&#39;,
  &#39;ghi&#39;: &#39;accountSummaries/125&#39;,
  &#39;jkl Accounts&#39;: &#39;accountSummaries/1405&#39;
  };
  

for(let i=0;i&lt;Object.keys(abc).length;i++) {
	$(&#39;#gg&#39;).append(&#39;&lt;option data-key=&quot;&#39;+Object.keys(abc)[i]+&#39;&quot; data-value=&quot;&#39;+Object.values(abc)[i]+&#39;&quot;&gt;&#39;+Object.values(abc)[i]+&#39;&lt;/option&gt;&#39;)
}

^Example mapping using jQuery. Or do you want to do it with PHP? This can also work, but you must point out where you want the key and where you want the value.

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

发表评论

匿名网友

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

确定