最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to preselect a paper-item in a paper-dropdown-menu from model data? - Stack Overflow

programmeradmin1浏览0评论

I have an iron-form to do CRUD in my project. This works well, apart of one problem.

How can I get the paper-dropdown-menu to show the value from the model data ? (That is: preselect paper-item corresponding to the [[o.id]] value from my model data)

The selected attribute of paper-menu does not help because it's index-based and not based on the value of a paper-item.

Here is the code of the form:

<form is="iron-form" id="itemForm" method="post" action="/api/item/edit">

    <input is="iron-input" name="id" type="hidden" value="{{item.id}}">

    <paper-dropdown-menu label="Title" selected-item="{{selectedTitle}}">
        <paper-menu class="dropdown-content">
            <template is="dom-repeat" items="{{titles}}" as="o">
                <paper-item value="[[o.id]]">[[o.name]]</paper-item>
            </template>
        </paper-menu>
    </paper-dropdown-menu>
    <input is="iron-input" name="title" type="hidden" value$="[[selectedTitle.value]]">

    <paper-input name="firstName" label="First name" value="{{item.firstName}}"></paper-input>

    <paper-button raised onclick="document.getElementById('itemForm').submit()">Save</paper-button>
</form>

I have an iron-form to do CRUD in my project. This works well, apart of one problem.

How can I get the paper-dropdown-menu to show the value from the model data ? (That is: preselect paper-item corresponding to the [[o.id]] value from my model data)

The selected attribute of paper-menu does not help because it's index-based and not based on the value of a paper-item.

Here is the code of the form:

<form is="iron-form" id="itemForm" method="post" action="/api/item/edit">

    <input is="iron-input" name="id" type="hidden" value="{{item.id}}">

    <paper-dropdown-menu label="Title" selected-item="{{selectedTitle}}">
        <paper-menu class="dropdown-content">
            <template is="dom-repeat" items="{{titles}}" as="o">
                <paper-item value="[[o.id]]">[[o.name]]</paper-item>
            </template>
        </paper-menu>
    </paper-dropdown-menu>
    <input is="iron-input" name="title" type="hidden" value$="[[selectedTitle.value]]">

    <paper-input name="firstName" label="First name" value="{{item.firstName}}"></paper-input>

    <paper-button raised onclick="document.getElementById('itemForm').submit()">Save</paper-button>
</form>
Share Improve this question asked Sep 4, 2015 at 20:44 yglodtyglodt 14.6k15 gold badges101 silver badges136 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

First, you cannot use selected-item for two-way data binding 'cause it's read-only.

Instead, you should be using selected on the paper-menu like this -

<paper-menu id="menu" class="dropdown-content" selected="{{selectedValue}}" attr-for-selected="value">

Note that you need to specify the attr-for-selected to tell the selected property which attribute to use for selection. You also need to create a selectedValue property in your element to update the selected property via binding.

Initially, I specified the selectedValue property in Polymer's ready function and it didn't work. I suspect this might be because the selected property was set too early, before the repeating templates were rendered.

So instead of setting it inside ready, I set it in attached -

attached: function () {
  this.async(function () {
    this.selectedValue = 1;
  });
}

And this time it works.

To pre-select items in the paper-dropdown-menu you can add the properties selected and attr-for-selected in the element used as dropdown-content (e.g. paper-menu or paper-listbox).

<paper-dropdown-menu>
  <paper-listbox attr-for-selected="data-order" selected="1" class="dropdown-content">
    <paper-item data-order="0">First</paper-item>
    <paper-item data-order="1">Second</paper-item>
    <paper-item data-order="2">Third</paper-item>
  </paper-listbox>
</paper-dropdown-menu>
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>