简体   繁体   English

下拉列表时如何在表格上显示名称

[英]How to show name on table when it is dropdown

I am creating my own custom inline editable table.我正在创建自己的自定义内联可编辑表。 I am not able to show the dropdown assigned name.我无法显示下拉指定的名称。 For example I have dropdown data like this:例如,我有这样的下拉数据:

statusDropdownData = [
    {
      statusId: 3,
      name: "test 1",
    },
    {
      statusId: 4,
      name: "test 2",
    },
  ];

On table I am able to see the ID, but I want to see the name, and based on id, when i click on edit button, that id should patch.在桌子上我可以看到 ID,但我想看到名称,并且基于 id,当我单击编辑按钮时,该 id 应该修补。 But I am not able to do it.但我做不到。 I tried this.我试过这个。

my custom component for table html我的表格 html 的自定义组件

<a (click)="addNew()" class="mb-1 ml-1">Add New</a>
<table class="row-border table-bordered-no-confilct border-1 table">
  <thead>
    <tr>
      <th *ngFor="let head of headers">
        {{ head.name }} <span class="text-danger" *ngIf="head.required">*</span>
      </th>
    </tr>
  </thead>

  <tr *ngFor="let tableData of data; let i = index">
    <td>
      <ng-container *ngIf="tableData.buttonStatus === true">
        <i
          class="ace-icon fa fa-pencil-square-o bigger-150 text-success"
          data-toggle="tooltip"
          title="Edit"
          *ngIf="!tableData.isEditable"
          (click)="onEdit(tableData)"
        ></i>
        <i
          class="ace-icon fa fa-trash-o bigger text-danger ml-1"
          data-toggle="tooltip"
          title="Delete"
          *ngIf="!tableData.isEditable"
          (click)="onDelete(tableData)"
        ></i>

     
      </ng-container>
      <i
        class="ace-icon fa fa-floppy-o bigger-150 text-success"
        data-toggle="tooltip"
        title="Save"
        *ngIf="tableData.isEditable"
        (click)="onSave(tableData)"
      ></i>
      <i
        class="ace-icon fa fa-times bigger-150 text-danger ml-1"
        data-toggle="tooltip"
        title="Cancel"
        *ngIf="tableData.isEditable"
        (click)="cancel(tableData, i)"
      ></i>
    </td>
    <td>{{ i + 1 }}</td>
    <ng-container *ngIf="tableData.isEditable; else viewable">
      <ng-container *ngFor="let head of headers">
        <ng-container *ngIf="head.mappedProperty">
          <td>
            <input
              *ngIf="
                head.dataType === 'text' ||
                head.dataType === 'number' ||
                head.dataType === 'checkbox'
              "
              [type]="head.dataType"
              [(ngModel)]="tableData[head.mappedProperty]"
              [checked]="tableData[head.mappedProperty]"
            />
       

            <my-custom-dropdown
              *ngIf="head.dataType === 'dropdown'"
              [data]="head.dropDownData"
              [config]="head.dropdownConfig"
              [(selectedModel)]="tableData[head.dropdownConfig.bindValue]"
            ></my-custom-dropdown>
          </td>
        </ng-container>
      </ng-container>
    </ng-container>

    <ng-template #viewable>
      <ng-container *ngFor="let head of headers">
        <ng-container *ngIf="head.mappedProperty">
          <td>{{ head.dataType ==='dropDown' ? tableData[head.mappedName]: tableData[head.mappedProperty] }}</td>
        </ng-container>
      </ng-container>
    </ng-template>
  </tr>
</table>

its ts它的 ts

 @Input() data;
  @Input() headers;
  @Output() modelData: EventEmitter<object> = new EventEmitter<object>();
  @Output() deleteData: EventEmitter<object> = new EventEmitter<object>();
  @Output() moveUpData: EventEmitter<object> = new EventEmitter<object>();
  @Output() moveDownData: EventEmitter<object> = new EventEmitter<object>();
  private newFieldCounter: number = 0;
  isNew = false;
  copyOfOriginalData;
  constructor(
    private alertService: ToasterService,
    private confirmationDialogService: ConfirmDialogService
  ) {}

  onEdit(data): void {
    for (let i = 0; i < this.data.length; i++) {
      if (i == data) {
        this.data[i].buttonStatus = true;
      } else {
        this.data[i].buttonStatus = false;
      }
    }
    this.isNew = false;
    this.copyOfOriginalData = { ...data };
    this.data.map((item) => {
      item.isEditable = data.id === item.id;
    });
    console.log('selected', data);
    console.log('data on edit', this.data )
  }

  onSave(data): void {
    let requiredColumns = [];
    this.headers.forEach((headers) => {
      if (headers.required === true) {
        requiredColumns.push(headers.mappedProperty);
      }
    });

    let isEmpty = false;
    requiredColumns.forEach((columnsData) => {
      if (data[columnsData] === "" || !data[columnsData]) {
        console.log("req", requiredColumns);
        this.alertService.error(columnsData + " can not be empty");
        isEmpty = true;
      }
    });
    if (!isEmpty) {
      this.modelData.emit(data);
      data.isEditable = !data.isEditable;
    }
  }

  onDelete(data): void {
    this.confirmationDialogService
      .confirm("", "Are you sure you want to remove this user?")
      .subscribe((action) => {
        if (action) {
          this.deleteData.emit(data);
        }
      });
  }

  moveDown(data): void {
    this.moveDownData.emit(data);
  }

  moveUp(data): void {
    this.moveUpData.emit(data);
  }

  cancel(data, index): void {
    if (this.isNew) this.data = this.data.filter((x) => !(x.id === data.id));
    else this.data[index] = { ...this.copyOfOriginalData };

    for (let i = 0; i < this.data.length; i++) {
      this.data[i].buttonStatus = true;
    }
    data.isEditable = !data.isEditable;
  }

  addNew(): void {
    this.isNew = true;
    this.data.push({
      id: "a" + this.newFieldCounter++,
      isEditable: true,
    });
  }

  addRequiredKeys(): void {
    this.data = this.data.map((item) => ({
      ...item,
      isEditable: false,
      buttonStatus: true,
    }));
    console.log("key added", this.data);
  }

  ngOnInit(): void {
    this.addRequiredKeys();
    console.log("header", this.headers);
  }

consuming component消费组件

 data = [
    {
      id: 1,
      name: "ashish",
      mobile: "8788888888",
      date: "11-10-2021",
      statusId: 3,
      jobReady: true,
    },
    {
      id: 2,
      name: "mishra",
      mobile: "9788888888",
      date: "11-11-2021",
      statusId: 4,
      jobReady: false,
    },
    // {
    //   id: 3,
    //   name: "demo",
    //   mobile: "9788888888",
    //   date: "11-11-2021",
    //   isEditable: false,
    // },
  ];

  statusDropdownData = [
    {
      statusId: 3,
      name: "test 1",
    },
    {
      statusId: 4,
      name: "test 2",
    },
  ];

  jobDropDownConfig: DropdownConfig = {
    bindLabel: "name",
    bindValue: "id",
    placeholder: "Select Job",
  };
  demoDropDownConfig: DropdownConfig = {
    bindLabel: "name",
    bindValue: "statusId",
    placeholder: "Select Status",
  };

  //datePickerConfig: DatePickerConfig ;

  datePickerConfig: DatePickerConfig = {
    propertyConfig: {
      datePicker: true,
      rangePicker: false,
      inlineDatePicker: false,
      inlineDateRangePicker: false,
      placeholder: "Date Picker",
      hideOnScroll: true,
    },
  };
  constructor() {
    this.datePickerConfig = new DatePickerConfig();
  }
  headers: Array<TableHeaders> = [
    { name: "Edit" },
    { name: "Sr.No" },
    { name: "name", dataType: "text", mappedProperty: "name", required: true },
    {
      name: "mobile",
      dataType: "number",
      mappedProperty: "mobile",
      required: true,
    },
    {
      name: "date",
      dataType: "date",
      mappedProperty: "date",
      datePickerConfig: this.datePickerConfig,
    },
    {
      name: "job",
      dataType: "dropdown",
      mappedProperty: "id",
      dropDownData: this.data,
      dropdownConfig: this.jobDropDownConfig,
      required: true,
    },
    {
      name: "Status",
      dataType: "dropdown",      
      mappedName:'name',
      mappedProperty: "statusId",
      dropDownData: this.statusDropdownData,
      dropdownConfig: this.demoDropDownConfig,
    },
    {
      name: "Job Ready",
      dataType: "checkbox",
      mappedProperty: "jobReady",
    },
  ];

  modelData(data) {
    console.log("data", data);
  }

  deleteData(data) {
    console.log("deleted data", data);
  }

The parameter of onEdit is of type: onEdit的参数类型为:

{
      id: number,
      name: string,
      mobile: string,
      date: string,
      statusId: number,
      jobReady: boolean,
},

So you should change your onEdit here:所以你应该在这里改变你的 onEdit :

onEdit(data): void {
    for (let i = 0; i < this.data.length; i++) {
      if (i == data.id) {                           // <--- change, add  .id
        this.data[i].buttonStatus = true;
      } else {
        this.data[i].buttonStatus = false;
      }
    }
    this.isNew = false;
    this.copyOfOriginalData = { ...data };
    this.data.map((item) => {
      item.isEditable = data.id === item.id;
    });
    console.log('selected', data);
    console.log('data on edit', this.data )
  }

暂无
暂无

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

相关问题 使用动态表时如何根据下拉列表使用jquery显示/隐藏列? - How to show/hide columns using jquery based on dropdown list when using a dynamic table? 选中复选框时如何显示/隐藏下拉菜单 - How to show/hide dropdown when checkbox is selected 单击时如何显示下拉菜单 - How to show dropdown menu when clicked 当用户在下拉列表中选择发言人姓名时,只有发言人的专业知识才会显示在下一个主题下拉列表中 - When a user pick a speaker name on a dropdown list only the speakers expertise will show in the next topic dropdown list 如何在知道此名称的表格中显示图像 - How to show image in the table having the knowing the name of this jQuery - 如何通过动态表中的文本框显示下拉选择的值 - jQuery - how to show dropdown selected values through textbox in dynamic table 根据下拉列表显示表格 - Show Table Based on Dropdown Selection 数据表:如何在表结构中显示0项的0到0中显示show Entries下拉列表 - Data Table: How to show the show Entries dropdown beside the Showing 0 to 0 of 0 entries in the table structure 如何在表中该行的其他可用下拉列表中显示从下拉列表中取消选择的选项-jQuery - how to show the deselected option from the dropdown list in the other available dropdown list of that row in a table - jquery 在另一个下拉列表中选择选项时显示下拉列表 - Show dropdown when option is selected in the another dropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM