简体   繁体   English

Angular mat-chip-list 大小不正确,芯片位于输入区域

[英]Angular mat-chip-list size is incorrect, chips are sitting in input area

Apologies for the bad title, I have a myriad of issues that need work.为糟糕的标题道歉,我有无数的问题需要解决。

I am not a web dev, this is the first time working with Angular, and Typescript, so I am not sure even how to articulate the problems correctly, or even if I am giving you the right information.我不是 web 开发人员,这是第一次使用 Angular 和 Typescript,所以我什至不确定如何正确阐明问题,或者即使我给你的信息是正确的。

So, for starters, my input area should be 15 px by 150px, but it is showing as 108px by 150px.所以,对于初学者来说,我的输入区域应该是 15 像素 x 150 像素,但它显示为 108 像素 x 150 像素。 红框显示输入区域大小 Here is my css.这是我的 css。

.manual-work-entry {
margin-top: 1em;

.entry-row {
    height: 40px;
}

.input-small {
    width: 100px;
    margin: 0;
    padding: 1px;

    mat-chip {
        height: 22px;
        font-size: 11px;
        width: 85px;

        div {
            width: 100%;
        }
    }
}

::ng-deep .lower .mat-form-field-underline {
    bottom: 0 !important;
}

::ng-deep .mat-chip-input {
    .mat-form-field {
        .mat-input-infix,
        .mat-form-field-infix,
        .mat-chip-input {
            width: auto;
            height: 15px !important;
        }

        .mat-chip-input {
            flex: 1 1 auto;
        }
    }
}
}

Second, when I input a value for a chip, it shows above the input box, pushing it down.其次,当我为筹码输入一个值时,它会显示在输入框上方,将其向下推。 I want them to show below the box.我希望它们显示在框下方。

输入框被推下。

Third, when I press enter, I want the value I just typed in to vanish from the input box.第三,当我按下回车键时,我希望我刚刚输入的值从输入框中消失。 I ho idea how to do that.我不知道该怎么做。

Fourth, when I hit backspace, I can keep going past the input values, and delete chips.第四,当我按下退格键时,我可以继续跳过输入值并删除筹码。 Is this supposed to happen?这应该发生吗? I don't think it is, but I am not proficient enough in Angular to know.我觉得不是,但我对Angular还不够熟练,不知道。

Here is a chunk of the afflicted HTML code.这是受影响的 HTML 代码的一部分。

<mat-form-field class="input-small">
                                                    <mat-chip-list #additionalPartNumberCL
                                                                   class="mat-chip-list-stacked">
                                                        <mat-chip *ngFor="let number of additionalPartNumbers"
                                                                  [selectable]="true"
                                                                  [removable]="true"
                                                                  (removed)="removeMatChip($event, number, 'additionalPartNumbers')">
                                                            <div>
                                                                {{number}}
                                                            </div>
                                                            <mat-icon matChipRemove>cancel</mat-icon>
                                                        </mat-chip>
                                                        <input placeholder="Additional Part Numbers (optional)"
                                                               [matChipInputFor]="additionalPartNumberCL"
                                                               (matChipInputTokenEnd)="addMatChip($event, 'additionalPartNumbers')">
                                                    </mat-chip-list>
                                                </mat-form-field>

And the corresponding typescript for addMatChip而addMatChip对应的typescript

    public addMatChip(event: any, varName: string): void {
    const value = (event.value || "").trim();

    // this allows us to input the same value multiple times
    switch (varName) {
        case "additionalPartNumbers": {
            this.additionalPartNumbers.push(value);
            break;
        }
//removing extra code
    }

    // reset the input value
    if (event.value) {
        event.value = "";
    }
}

and the removeMatChip code和 removeMatChip 代码

public removeMatChip(event: any, partNumber: string, varName: string): void {
    const value = partNumber;

    switch (varName) {
        case "additionalPartNumbers": {
            const index = this.additionalPartNumbers.indexOf(value);
            if (index >= 0) {
                this.additionalPartNumbers.splice(index, 1);
            }
            break;
//remove useless code
        }
    }
}

Any suggestions for improving my code?有什么改进我的代码的建议吗?

I fixed issue 1 and 2 by moving the input to above the mat-chip-list.我通过将输入移到 mat-chip-list 上方来修复问题 1 和 2。

Issue 3 was solved by adding an event.input.value = "";通过添加event.input.value = "";解决了问题 3 to the if(event.value) in the add function.if(event.value)中添加 function。

I am still working on issue 4.我仍在处理问题 4。

You can add custom css in the file style.css.您可以在文件 style.css 中添加自定义 css。 first you should try to find out the class from dev tools then you can apply the css to that list.首先,您应该尝试从开发工具中找出 class,然后您可以将 css 应用到该列表。

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

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