Default Value for Dropdown

旧时模样 提交于 2020-01-24 22:16:40

问题


Can't seem to find an answer to this issue easily.

Default values for fields you want to display and allow editing, the best place seems to be to set the widget.value in a draft record, so default date widget.value = new Date(), etc.

How do I reference a dropdown where the dropdown is a related table lookup, e.g., a list of Properties where the bindings are @datasources.Property.Items for the options and @datasource.Item.Property for the value.

If I do console.log(widget.value); I get object Object, which is what I would expect because it is of type object. But how can I set (and retrieve) its value?

Help appreciated.


回答1:


I tested this for a similar situation in my own application. Do note that there is a minimal delay before the value in the dropdown actually populates. I'm not sure what your alternative solution could be due to the reason stated in my last comment above. I might suggest setting this in your form model onBeforeCreate event instead, but then you would loose the ability to change the relation in the form itself so that might not work for you.

Dropdown widget onAttach event:

var datasource = app.datasources.Property;
if(!datasource.loaded) {
  datasource.load(function() {
    var index = datasource.items.map(function(i){return i.Status;}).indexOf('Open');
    if(index !== -1) {
      widget.value = datasource.items[index];
    }
  });
} else {
  var index = datasource.items.map(function(i){return i.Status;}).indexOf('Open');
  if(index !== -1) {
    widget.value = datasource.items[index];
  }
}


来源:https://stackoverflow.com/questions/55881794/default-value-for-dropdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!