Hello to all, welcome to therichpost.com. In this post, I will tell you, Angular Material Autocomplete with Dynamic data Php Mysql.
I am doing this in Angular 6 and this is very interesting post.
Here is the working example picture:
Here is the complete working code and you need to add your Angular App:
1. First you need to run below command into your terminal to include angular material into your angular 6 app:
npm install --save @angular/material @angular/cdk @angular/animations
2. After that, please add below code into your app.module.ts file:
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { MatAutocompleteModule, MatNativeDateModule, MatBadgeModule, MatBottomSheetModule, MatButtonModule, MatButtonToggleModule, MatCardModule, MatCheckboxModule, MatChipsModule, MatDatepickerModule, MatDialogModule, MatDividerModule, MatExpansionModule, MatGridListModule, MatIconModule, MatInputModule, MatListModule, MatMenuModule, MatNativeDateModule, MatPaginatorModule, MatProgressBarModule, MatProgressSpinnerModule, MatRadioModule, MatRippleModule, MatSelectModule, MatSidenavModule, MatSliderModule, MatSlideToggleModule, MatSnackBarModule, MatSortModule, MatStepperModule, MatTableModule, MatTabsModule, MatToolbarModule, MatTooltipModule, MatTreeModule, MatFormFieldModule} from "@angular/material"; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { CdkTreeModule } from '@angular/cdk/tree'; import { CdkTableModule } from '@angular/cdk/table'; import { HttpClientModule } from '@angular/common/http'; import {platformBrowserDynamic} from '@angular/platform-browser-dynamic'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, BrowserAnimationsModule, FormsModule, HttpClientModule, ReactiveFormsModule, MatAutocompleteModule, MatFormFieldModule, MatInputModule, MatRippleModule, MatNativeDateModule ], exports: [ CdkTableModule, CdkTreeModule, MatAutocompleteModule, MatBadgeModule, MatBottomSheetModule, MatButtonModule, MatButtonToggleModule, MatCardModule, MatCheckboxModule, MatChipsModule, MatStepperModule, MatDatepickerModule, MatDialogModule, MatDividerModule, MatExpansionModule, MatGridListModule, MatIconModule, MatInputModule, MatListModule, MatMenuModule, MatNativeDateModule, MatPaginatorModule, MatProgressBarModule, MatProgressSpinnerModule, MatRadioModule, MatRippleModule, MatSelectModule, MatSidenavModule, MatSliderModule, MatSlideToggleModule, MatSnackBarModule, MatSortModule, MatTableModule, MatTabsModule, MatToolbarModule, MatTooltipModule, MatTreeModule, ] entryComponents: [AppComponent], declarations: [AppComponent], providers: [], bootstrap: [AppComponent] }) export class AppModule { } platformBrowserDynamic().bootstrapModule(AppModule);
3. After that, please add below code into your app.component.ts file:
import {Component } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import {FormControl} from '@angular/forms'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { myControl = new FormControl(); options: string[]; constructor(private http: HttpClient) { this.http.get('http://localhost/mypage.php').subscribe(data => { this.options = data; }, error => console.error(error)); } }
4. After that, please add below code into your app.component.html file:
<div class="jumbotron text-center"> <h3>Angular Material Autocomplete with Dynamic data Php Mysql</h3> </div> <form class="example-form"> <mat-form-field class="example-full-width" floatLabel="never"> <input type="text" matInput [formControl]="myControl" placeholder="Select Option" [matAutocomplete]="auto" disabled="true"> <mat-autocomplete #auto="matAutocomplete"> <mat-option *ngFor="let option of options" [value]="option.name"> {{ option.name }} </mat-option> </mat-autocomplete> </mat-form-field> </form>
5. Here is the mypage.php file code:
<?php header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Methods: PUT, GET, POST"); header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); $conn = new mysqli('localhost','root','','user'); $sql = "SELECT * FROM userdata"; $result = $conn->query($sql); $myArr = array(); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { $myArr[] = $row; } } else { echo "0 results"; } $myJSON = json_encode($myArr); echo $myJSON;
6. Here is database example code:
Now Here is done and If have any query related to this post, then please so ask questions or comment below.
Recent Comments