【发布时间】:2022-01-25 17:55:40
【问题描述】:
我想根据 db 表上存在的值设置一个 ion-toggle。
如果值存储在表中,则切换应为 ON,如果不是 OFF。关(左)开(右)
所以,如果 Artist_interests 表中的艺术家列有 Cinematographer 的值,那么我想要
这是我目前所拥有的。
html
<ion-item>
<ion-label>Cinematographers</ion-label>
<ion-toggle color="success" [(ngModel)]="artistInterests.Cinematographers" (click)="artistInterest(artists, 'Cinematographers')"></ion-toggle>
</ion-item>
<ion-item>
<ion-label>Graphic Designers</ion-label>
<ion-toggle color="gold-white" [(ngModel)]="artistInterests.graphicDesigners" (click)="artistInterest(artists, 'Graphic Designers')"></ion-toggle>
</ion-item>
.ts 调用 API 函数在 DB 表上存储值(例如图形设计师) - 我的意思是它将“图形设计师”存储在 DB 表的艺术家列中。
artistInterest(artists:string, messageTitle: string)
{
this.userData.artistSettings(this.userDetails.uid, messageTitle).pipe(
map((data: any) => {
if (data.success) {
Swal.fire({
icon: 'success',
title: messageTitle,
showConfirmButton: false,
backdrop: false,
timer: 2500
})
}
})
).subscribe()
}
.ts 函数从 api 函数中检索数据
export class CreativeSettingsPage implements OnInit {
creativeInterests: any= {};
creative: any= {};
artistInterests: any= {};
artists: any= {};
userDetails: any = {};
constructor(
public userData: UserData
) {
this.userDetails = this.userData.getUserData();
this.creativeInsterestSet();
this.artistsInsterestSet;
}
ngOnInit() {
this.artistsInsterestSet()
}
artistsInsterestSet() {
this.userData.artistsInterests(this.userDetails.uid).pipe(
map((data: any) => {
if (data.success) {
this.artistInterests = data.artistInterests;
console.log( this.artistInterests);
}
})
).subscribe()
}
userData.ts
artistsInterests(uid:number) {
const url = this.appData.getApiUrl() + 'getArtistInterests';
const data = this.jsonToURLEncoded({
uid: uid
});
return this.http.post(url, data, { headers: this.options });
}
php函数getArtistInterests
function getArtistInterests()
{
$request = \Slim\Slim::getInstance()->request();
$data = json_decode($request->getBody());
$response['success'] = true; // 1
$uid = $request->post('uid');
$db = getDB();
$sql = "SELECT A.artist, U.profile_pic,U.first_name,U.last_name,
DATE_FORMAT(A.created, '%W %D %M %Y, %l %i %p') as date
FROM artists_interests A
LEFT JOIN users U ON A.uid_fk=U.uid
WHERE A.uid_fk=? GROUP BY A.artist";
$stmt = $db->prepare($sql);
$stmt->execute(array($uid));
$db = null;
$artistInterests = $stmt->fetchAll(PDO::FETCH_OBJ);
foreach ($artistInterests as $feed) {
$feed->profile_pic = SITE_URL . 'usersPic/' . $feed->profile_pic;
}
$data['success'] = true;
$data['artistInterests'] = $artistInterests;
echo json_encode($data);
}
值存储为例如Artist_interests 表中的平面设计师(两个词),因此 [(ngModel)]="artistInterests.graphicDesigners" 将不起作用。但据我所知,它应该适用于 [(ngModel)]="artistInterests.Cinematographers"。通过工作,我的意思是打开工具(就在幻灯片上)
【问题讨论】:
-
使用
checked属性。例如:<ion-toggle color="success" [(ngModel)]="artistInterests.Cinematographers" checked="artistInterests.Cinematographers ? true : false" (ionChange)="artistInterest(artists, 'Cinematographers')"></ion-toggle>.也为事件使用 ionchange -
如果您不使用 ngFor,您如何获得所有这些切换按钮?
-
查看代码 Najam