Если вы будете использовать только ответ Видара Уолберга, вы получите сообщение об ошибке при открытии другой активности (например) и обратно на карту. Или в моем случае открыть другую активность, а затем снова открыть новую карту (без использования кнопки возврата). Но когда вы примете решение Vidar Wahlberg и решение Matt, у вас не будет исключений.
layout
<com.example.ui.layout.MapWrapperLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/map_relative_layout">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/root">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
</<com.example.ui.layout.MapWrapperLayout>
Фрагмент
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null){
parent.removeView(view);
}
}
try {
view = inflater.inflate(R.layout.map_view, null);
if(view!=null){
ViewGroup root = (ViewGroup) view.findViewById(R.id.root);
...
@Override
public void onDestroyView() {
super.onDestroyView();
Fragment fragment = this.getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.map);
if (fragment != null)
getFragmentManager().beginTransaction().remove(fragment).commit();
}
Я бы сохранил каждый выбранный объект в списке, а затем использовал бы его в качестве свойства dataSource:
И обновил бы свойство DataSource следующим образом:
this.dataSource = new MatTableDataSource(your_list)
HTML Code:
[ 111]Код TS:
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ICustomer } from '../models';
import { DataService } from '../data.service';
import { MatTableDataSource, MatDialog } from '@angular/material';
@Component({
selector: 'app-display',
templateUrl: './display.component.html',
styleUrls: ['./display.component.css']
})
export class DisplayComponent implements OnInit {
public contacts: ICustomer;
public selectedCustomers: any[] = [];
public displayForm: FormGroup;
public addCustomer: any;
dataSource = new MatTableDataSource([]);
list: any[] = []
public displayedColumns: string[] = ['name', 'email', 'phone', 'button'];
constructor(private fb: FormBuilder, public dataService: DataService) { }
public async ngOnInit(): Promise<void> {
this.displayForm = this.fb.group({
});
this.dataService.onSelectCustomer.subscribe(value => {
if (value) {
for (var i = 0; i < value.CustomerIds.length; i++) {
this.list.push(value.CustomerIds[i])
this.dataSource = new MatTableDataSource(this.list)
}
console.log(this.list)
}
});
}
public onSave(): void {
this.addCustomer = this.selectedCustomers;
//console.log(this.addCustomer)
var arr = this.addCustomer.CustomerIds.map(value => value.id);
console.log(arr);
}
}
Вам необходимо проверить, существует ли объект или нет, нажмите еще раз, как показано ниже -
this.dataService.onSelectCustomer.subscribe(value => {
if (value.CustomerIds && value.CustomerIds.length){
if (this.selectedCustomers && this.selectedCustomers.CustomerIds) {
value.CustomerIds.forEach(ele => {
this.selectedCustomers.CustomerIds.findIndex(obj => obj.id == ele.id) !== -1 ? "" : this.selectedCustomers.CustomerIds.push(ele);
});
} else {
this.selectedCustomers = value;
}
}
});