Ng-admin:如何根据用户的选择显示不同的字段?

时间:2016-03-11 07:00:11

标签: angularjs ng-admin

我正在使用ng-admin编写管理员管理。我遇到了下面的问题,有人可以帮助我吗?

在我的creationView中,我想根据“type”字段的选择显示不同的字段(文本/视频/图片)。我怎么能做到的?

var articles = nga.entity('articles');
articles.creationView().fields([
      nga.field('type','choice')
            .validation({ required: true })
            .choices([
                // 1: txt, 2: pic, 3: vid
                { value: 1, label: 'Text'},
                { value: 2, label: 'Picture'},
                { value: 3, label: 'Video'},
            ]),
      nga.field('txt','file')
          .attributes({ placeholder: 'Write something... !' }),
      nga.field('pic','file')
          .label('Picture(.jpg|.png)')
          .uploadInformation({ 'url': '/api/adminapi/uploadPicture', 'apifilename': 'pictures', 'accept': 'image/jpg,image/png'}),
      nga.field('vid','file')
         .label('Video(.mp4)')
         .uploadInformation({ 'url': '/api/adminapi/uploadVideo', 'apifilename': 'video', 'accept': 'video/mp4'}),

])

2 个答案:

答案 0 :(得分:5)

Field Configuration doc页面使用"模板"解释了如何执行此操作。字段配置:

  

template(String | Function,templateIncludesLabel = false)所有字段类型   支持template()方法,这样可以轻松自定义   特定领域的外观和感觉,而不牺牲原生   特征

     

...

     

强制模板替换   整行(包括标签),作为第二个参数传递给   template()调用。这对于有条件地隐藏字段非常有用   根据条目的属性:

     

post.editionView() .fields([ nga.field('category', 'choice') .choices([ { label: 'Tech', value: 'tech' }, { label: 'Lifestyle', value: 'lifestyle' } ]), nga.field('subcategory', 'choice') .choices(function(entry) { return subCategories.filter(function (c) { return c.category === entry.values.category; }); }) // display subcategory only if there is a category .template('<ma-field ng-if="entry.values.category" field="::field" value="entry.values[field.name()]" entry="entry" entity="::entity" form="formController.form" datastore="::formController.dataStore"></ma-field>', true), ]);

答案 1 :(得分:1)

我只有一个工作轮方法。那就是使用.attributes(onclick,“return updateButton();”)来表示nga.field(“type”)。在updateButton()方法中,它将有助于检查当前的“类型”字段值,并使用DOM方法更改按钮启用和禁用。

然而,我仍然真的很感激,如果这个要求将来可以得到支持。用户可以轻松地进行UI控件。