简体   繁体   English

删除在 ng-selectize 上选择的当前元素?

[英]Remove the current element selected on ng-selectize?

I am trying to delete the selected item but the select is deformed when the item is deleted.我正在尝试删除所选项目,但删除项目时选择变形。 I wrote some test code:我写了一些测试代码:

public val = null;
config = {
    labelField: 'label',
    valueField: 'value', 
    highlight: true,
    create: false,
  };

  data = [
    {
      label: '1', 
      value: '1'
    },
      {
      label: '2',
      value: '2'
    },
      {
      label: '3', 
      value: '3'
    } 
  ]

deleteFirst(){
    this.data.splice(0,1)
}

<ng-selectize [config]="config" [options] = "data" [(ngModel)]="val" 
(ngModelChange)="changed()"
></ng-selectize>
<button class="btn btn-primary" (click)="deleteFirst()">Delete First</button>

Just select the first option then delete.只需选择第一个选项然后删除。 I don't know how solve it, what am I doing wrong?我不知道如何解决它,我做错了什么?

Here is the result:结果如下:

在此处输入图片说明

Stackblitz link:- https://stackblitz.com/edit/angular-ng-selectize-xanydu?file=app/case/case.component.ts Stackblitz 链接:- https://stackblitz.com/edit/angular-ng-selectize-xanydu?file=app/case/case.component.ts

LInk:- https://www.npmjs.com/package/ng-selectize链接:- https://www.npmjs.com/package/ng-selectize

Add ng-selectize as:-添加 ng-selectize 为:-

npm i --save ng-selectize jquery selectize

Add the following to the styles array within .angular-cli.json:


"../node_modules/selectize/dist/css/selectize.css",
"../node_modules/selectize/dist/css/selectize.{your chosen theme}.css"
(the semantic-ui theme has been added to ng-selectize/selectize/selectize.semantic.css if needed)

Add the following to the scripts array within .angular-cli.json

"../node_modules/jquery/dist/jquery.min.js",
"../node_modules/ng-selectize/selectize/selectize.standalone.js" (or take from /node_modules/selectize/...)
Import module within applicable @NgModule:

import {NgSelectizeModule} from 'ng-selectize';
imports: [..., NgSelectizeModule, ...],
Use within template: <ng-selectize [config]="..." [options] = "..." {other-attributes}></ng-selectize>

.ts .ts

public val = null;
config = {
    labelField: 'label',
    valueField: 'value',
    maxItems: 10,
    highlight: true,
    create: false,
  };

  data = [
    {
      label: '1',
      value: '1'
    },
      {
      label: '2',
      value: '2'
    },
      {
      label: '3',
      value: '3'
    }
  ]

deleteFirst(){
    this.data.splice(0,1)
}

.html .html

<ng-selectize  name="mySelect"  *ngIf="data.length > 0"  [ngModelOptions]="{standalone: true}" 
 [config]="config" [options] = "data" [(ngModel)]="val"  ngDefaultControl
></ng-selectize>

<button class="btn btn-primary" (click)="deleteFirst()">Delete First</button>

Note: Add css and js files in angular.json as suggested in above link.注意:按照上面链接中的建议,在 angular.json 中添加 css 和 js 文件。

selectize.standalone.js  need to be inlcuded for single select

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM