我在Angular项目中遇到麻烦。我正在使用ID为number和name为any的JSON对象的项目列表。单击另一个列表中的设置类型(例如“请求类型”)后,该列表就会出现。我要添加到列表中。但是,我也想从列表中删除项目,当我添加一个新项目时,我需要它转到使用的上一个ID号之后的下一个数字,而不是行数。后端。现在,当我尝试添加项目时,出现以下错误:“'请求类型'实例无效。详细信息:'id'不能为空(值:未定义);'name'不能为空为空白(值:未定义)。”我已经对Angular中的可观察对象进行了一些研究,但我只是无法指出问题所在。任何帮助将不胜感激。
代码如下:
export class SettingsPage {
items: any[];
selectedItem: any;
addTable: boolean = false;
rows: {id:number, name:any}[] = [];
editing = {};
nameOld;
editRow = {};
rowCount: number;
nameNew;
wasClicked = false;
@ViewChild("name") name: ElementRef;
constructor(public navCtrl: NavController, public navParams: NavParams,
public alertCtrl: AlertController, private requestTypeApi :
RequestTypeApi, private requestSizeApi : RequestSizeApi, private
requestStatusApi : RequestStatusApi, private businessAreaApi :
BusinessAreaApi, private businessValueApi : BusinessValueApi, private
projectTypeApi : ProjectTypeApi, private projectTeamApi : ProjectTeamApi)
{
this.items = [{label:"Request Types", service: this.requestTypeApi},
{label: "Request Sizes", service: this.requestSizeApi}, {label: "Request
Status", service: this.requestStatusApi}, {label: "Business Areas",
service: this.businessAreaApi}, {label: "Business Values", service:
this.businessValueApi}, {label: "Project Types", service:
this.projectTypeApi}, {label: "Project Teams", service:
this.projectTeamApi}];
this.selectedItem = navParams.get('items');
}
ionViewDidLoad() {
}
itemTapped(item) {
this.selectedItem = item;
this.selectedItem.wasClicked = true;
console.log(this.selectedItem);
this.addTable = true;
this.selectedItem.service.find()
.subscribe(data => {
this.rows = data;
this.rowCount = this.rows.length;
});
}
cancelTapped() {
this.addTable = false;
}
addTapped(event, cell, rowIndex) {
const prompt = this.alertCtrl.create({
title: 'Add Detail',
inputs: [
{
name: 'name'
}
],
buttons: [
{
text: 'Save',
handler: data => {
var detail = data.name;
this.rowCount = this.rowCount + 1;
this.selectedItem.service.create().subscribe(result => {
this.rows.push({id: this.rowCount, name: detail});
});
this.rows = this.rows.slice();
}
},
{
text: 'Cancel'
}
]
});
prompt.present();
}
save(event) {
this.nameNew = '';
this.nameNew = event.target.value;
this.selectedItem.service.updateAttributes(this.rowCount,
this.name).subscribe(result => {
let rows = this.rows;
rows[this.rowCount] = Object.assign({}, { id:
this.rows[this.rowCount].id, name: this.nameNew != "" ? this.nameNew :
this.nameOld }),
this.rows = [...this.rows];
this.rows.push({id: this.rowCount, name: this.name});
})
}
storeOldValues(rowIndex) {
this.nameOld = this.rows[rowIndex].name;
this.editRow = rowIndex;
setTimeout(() => this.name.nativeElement.focus(), 50);
}
doneEditing(rowIndex, returnOld) {
if (returnOld) {
this.rows[rowIndex].name = this.nameOld;
} else {
this.rows[rowIndex].name = this.nameNew;
}
this.editRow = null;
}
deleteRow(rowIndex) {
let temp = [...this.rows]
temp.splice(rowIndex, 1);
this.rows = temp;
}
}