18 Temmuz 2018 Çarşamba

Spring REST ile Angular ng-select component kullanımı


Merhabalar. Bu yazımda sizlere bir Angular componenti olan ng-select ve daha önceden yazdığım bir Spring REST service kullanarak autocomplete olayını göstereceğim.

ng-select componentinin kurulumu, temel kullanımı ve özellikleri hakkında bilgi almak için buraya 

bakabilirsiniz.

Burada gireceğimiz kullanıcı adı değeri sistemde aranıp filtrelenerek bulunduğu takdirde kullanıcı adının yanı sıra bu kullanıcıya ait tüm bilgileri tutan bir array dolacaktır.

Öncelikle Java tarafında ki servis metodum aşağıda ki gibidir.



@RequestMapping(value = "/filtrele" , method = RequestMethod.GET , produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<List<KullaniciModel>> filterKullanici(@RequestParam String username){
List<KullaniciModel> filteredKullanici = kullaniciService.kullaniciFiltrele(username);
if(filteredKullanici != null && filteredKullanici.size()>0){
return new ResponseEntity<List<KullaniciModel>>(filteredKullanici,HttpStatus.OK);
}
return new ResponseEntity<List<KullaniciModel>>(HttpStatus.NOT_FOUND);
}



ng-select’in html tarafında kullanımı aşağıda ki gibidir.



<div class="form-group">
<label>Kullanıcı Seçiniz</label>
<ng-select [items]="kullanici | async"
 bindLabel="username"
 placeholder="Kullanıcı Ara"
 [hideSelected]="true"
 [multiple]="true"
 typeToSearchText="Kullanıcı adını yazınız"
 notFoundText="Kullanıcı Bulunamadı"
 [loading]="kullaniciLoading"
 [typeahead]="kullaniciInput"
 [(ngModel)]="selectedUser">
</ng-select>
</div>



Component tarafında ise aşağıda ki gibi kullanılır.



import { Component, OnInit } from '@angular/core';

import { AuthService } from '../../auth/auth.service';
import { Observable, of, Subject,concat } from 'rxjs';
import { catchError, debounceTime, distinctUntilChanged, map, tap, switchMap } from 'rxjs/operators';

@Component({
selector: 'app-rol',
templateUrl: './rol.component.html',
styleUrls: ['./rol.component.css']
})
export class RolComponent implements OnInit {

kullanici: Observable<any>;
kullaniciLoading = false;
kullaniciInput = new Subject<string>();
selectedUser: any[];



constructor(private auhtService: AuthService { }

ngOnInit() {
 this.loadKullanici();
}

private loadKullanici() {
 this.kullanici = concat(
  of([]), // default items
    this.kullaniciInput.pipe(
    debounceTime(200),
    distinctUntilChanged(),
    tap(() => this.kullaniciLoading = true),
       switchMap(term => this.auhtService.getKullaniciFiltrele(term).pipe(
          catchError(() => of([])), // empty list on error
          tap(() => this.kullaniciLoading = false)
       ))
     )
   );
}
}



Angular tarafında Java servisini çağırışım ise aşağıda ki gibidir.

getKullaniciFiltrele(username:string):Observable<any>{

   return this.http.get(this.apiUrl + 'api/kullanici/filtrele?username='+username);
}


Componentin autocomplete kullanımına dair ekran görüntüleri aşağıda ki gibidir.










26 Mayıs 2018 Cumartesi

Angular HTTP Servislerinde Exception Handling


Merhabalar. Bu yazımda  Angular Http servislerini kullanırken dönen hatanın nasıl yönetilebileceğine dair bir kaç satır kod yazacağım.

Hatanın yönetimi için öncelikle aşağıda ki import ları kesinlikle servisinize eklemeniz gerekir.

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';


Daha sonra ise yine önceden yazdığımız ve response olarak text dönen bir restful servisi service tarafında aşağıda ki gibi çağırırız.

kitapKaydet(kitap:Kitap):Observable<any>{
return this.http.post(this.apiUrl+'admin/kitapislem/saveKitap',kitap)
.map((response:Response)=><any>response)
.catch(res=>Observable.throw(res.text()));
}


Component tarafında ise bu service metodunu aşağıda ki gibi çağırırız.

this.kitapService.kitapKaydet(kitapModel).subscribe(res=>{
this.notificationService.success('Başarılı',res.text());
},
err=>{
this.notificationService.error('Hata',err);
}
);


Başka bir yazıda görüşmek üzere. Hoşçakalın...






12 Mayıs 2018 Cumartesi

ANGULAR 4 İLE JQUERY DATATABLE KULLANIMI



Merhabalar. Bu yazımda sizlere Angular 4 ile jQuery datatable kullanımından bahsedeceğim. Görünüm olarak jQuery’nin default datatable css i yerine Bootstrap datatable görünümünü kullanacağım. Veri kaynağı olarak daha önceden yazdığım bir RESTful web servisini kullanacağım. Bu servis bana database’imde ki kitap listesini dönmektedir.

Angular 4 ile jQuery datatable kullanmak için evvela aşağıda ki dependencyleri npm ile kurmamız gerekmektedir.

npm install jquery –save
npm install datatables.net –save
npm install datatables.net-dt –save
npm install angular-datatables –save
npm install @types/jquery –save-dev
npm install @types/datatables.net –save-dev
npm install bootstrap@3
npm install data-table-angular-4-bootstrap-3 --save


Bütün bu dependencyleri kurduktan sonra projemizde ki angular.cli.json dosyasının içerisinde css ve javascript dosyalarını import etmemiz gerekir. Bu işlem aşağıda ki gibi olur.

.
.
.
"styles": [
 "../node_modules/bootstrap/dist/css/bootstrap.min.css",
 "../node_modules/datatables/media/css/dataTables.bootstrap.min.css",
 "styles.css"
],
"scripts": [
 "../node_modules/jquery/dist/jquery.js",
 "../node_modules/datatables.net/js/jquery.dataTables.js",
 "../node_modules/datatables/media/js/dataTables.bootstrap.min.js"
],

.
.
.


Artık DatatableModule’ü app.module.ts dosyamıza import ederek kodlamaya başlayabiliriz.

import { DataTablesModule } from 'angular-datatables';

.
.
.
imports: [
 BrowserModule,
 FormsModule,
 DataTablesModule,
 BrowserAnimationsModule,
 RouterModule.forRoot(appRoute),
]

.
.
.

data-table.component.ts

import { Component, OnInit } from '@angular/core';
import { Subject } from 'rxjs';
import { Kitap } from '../kitap/kitap';
import { KitapService } from '../kitap/kitap.service';

@Component({
selector: 'app-data-table',
templateUrl: './data-table.component.html',
styleUrls: ['./data-table.component.css'],
providers : [KitapService]
})
export class DataTableComponent implements OnInit {

dtOptions: DataTables.Settings = {};
dtTrigger: Subject<any> = new Subject();
kitaplar:Kitap[] = [];
constructor(private kitapService:KitapService) { }

ngOnInit() {
this.dtOptions = {
 pagingType: 'full_numbers',
 pageLength: 5,
 processing: true,
 language:{
 paginate:{
 first:"İlk",
 last:"Son",
 next:"Sonraki",
 previous:"Önceki"
},
 search:"Ara",
 info:"_TOTAL_ kayıttan _START_ - _END_ arasındaki kayıtlar gösteriliyor"
},
 lengthChange:false
};

this.getKitaplar();
}

getKitaplar(){
 this.kitapService.getKitapListe(undefined).subscribe(p=>
{
 this.kitaplar=p;
 this.dtTrigger.next();
}
);
}
}


data-table.component.html

<h4 class="page-header">Datatable Kullanımı</h4>
<table datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger" style="width: 100%"
class="table table-striped table-bordered">
 <thead>
  <tr>
   <th>Kitap Id</th>
   <th>Kitap Adı</th>
   <th>Yazar Adı</th>
   <th>Kitap Türü</th>
   <th>Alınma Tarihi</th>
   <th>Yayınevi</th>
   <th></th>
  </tr>
 </thead>
 <tbody>
  <tr *ngFor="let kitap of kitaplar">
   <td>{{kitap.kitapId}}</td>
   <td>{{kitap.kitapAd}}</td>
   <td>{{kitap.yazarAd}}</td>
   <td>{{kitap.kitapTuru.name | uppercase}}</td>
   <td>{{kitap.alinmaTarihi}}</td>
   <td>{{kitap.yayinEvi.label}}</td>
   <td>
     <button type="button" (click)="kitapDetayAc(kitap)" class="btn btn-       link">Detay</button>
   </td>
  </tr>
 </tbody>
</table>


Çalıştırdığımız takdirde sonuç aşağıda ki gibi olur.





Sorularınızı mesutemrecelenk@gmail.com adresine yazabilirsiniz.Başka bir yazıda görüşmek üzere hoşçakalın.

13 Ocak 2018 Cumartesi

MySQL "The user specified as a definer ('applica1'@'localhost') does not exist ve "this is incompatible with sql_mode=only_full_group_by" Hatalarının Çözümü

MySQL ' de

1.)SQL Error [1449] [HY000]: The user specified as a definer ('applica1'@'localhost') does not exist The user specified as a definer ('applica1'@'localhost') does not exist

hatasını alıyorsanız aşağıda ki sorguyu çalıştırmanız problemi çözecektir.

        grant all on *.* to 'applica1'@'localhost' identified by 'password' with grant option;

2.)this is incompatible with sql_mode=only_full_group_by

hatasını alıyorsanız aşağıda ki sorguyu çalıştırıp Database'e disconnect olup tekrar connect olduğunuz takdirde hata çözülmüş olacaktır.

set global       sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_ DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

Herkese iyi çalışmalar...