如何解决JSON数据未呈现我的HTML网格表
我无法使用来自mysql数据库的json数据以角度显示html网格表。请有人帮助我。
export class UserlistComponent implements OnInit {
users: Observable<User[]>;
constructor(private _service:NgserviceService,private _route:Router) { }
ngOnInit() {
this.reloadData();
}
reloadData(){
this.users = this._service.getUserList();
}
}
解决方法
似乎您收到的是数组而不是对象。您只需映射接收到的数据并保留其余数据即可。
reloadData() {
this.users = this._service.getUserList().pipe(
map(arrayUsers => arrayUsers.map(arrayUser => ({
id: arrayUser[0],email: arrayUser[1],firstname: arrayUser[2],lastname: arrayUser[3],})))
);
}
,
您的输出似乎是CSV类型(数组的数组)而不是JSON。因此,您需要使用import 'package:flutter/material.dart';
import 'dart:async';
final Color darkBlue = Color.fromARGB(255,18,32,47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),debugShowCheckedModeBanner: false,home: Scaffold(
body: Center(
child: TextFieldExample(),),);
}
}
class TextFieldExample extends StatefulWidget {
@override
_TextFieldExampleState createState() => _TextFieldExampleState();
}
class _TextFieldExampleState extends State<TextFieldExample> {
@override
void dispose() {
_username.close();
_password.close();
super.dispose();
}
String validatorMessage;
bool validate = false; //will be true if the user clicked in the login button
final _username = StreamController<String>(); //stream to validate the text
final _password = StreamController<String>();
Widget build(BuildContext context) {
return Scaffold(
body: Container(
margin: EdgeInsets.symmetric(vertical: 5),padding: EdgeInsets.symmetric(horizontal: 15),decoration: BoxDecoration(
borderRadius: BorderRadius.circular(29),//expose streambuilder to the column widget to use on multiple widgets
child: StreamBuilder<String>(
initialData: '',stream: _username.stream,builder: (context,usernameSnapshot) {
return StreamBuilder<String>(
initialData: '',stream: _password.stream,passwordSnapshot) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
TextField(
onChanged: _username.add,//everytime the text changes a new value will be added to the stream
decoration: InputDecoration(
icon: Icon(Icons.person),hintText: "Email",TextField(
obscureText: true,onChanged: _password.add,//everytime the text changes a new value will be added to the stream
decoration: InputDecoration(
icon: Icon(Icons.visibility),hintText: "Password",Container(
margin: EdgeInsets.symmetric(vertical: 5),child: ClipRRect(
borderRadius: BorderRadius.circular(29),child: RaisedButton(
padding: EdgeInsets.symmetric(vertical: 10),//when user presses button,validate turns to true and we check snapshots to get the data for the entries
onPressed: () async {
if (usernameSnapshot.data.isNotEmpty && passwordSnapshot.data.isNotEmpty) {
try {
//sign in with usernameSnapshot.data and passwordSnapshot.data
} catch (e) {
print(e);
}
validate = true;
} else {
setState(() {
validate = true;
});
}
},child: Text("Login"),if (usernameSnapshot.data.isEmpty &&validate == true) //checking the stream and if the user clicked in the button
Container(
alignment: Alignment.center,child: Text(
'Check your e-mail and password.',style: TextStyle(
color: Colors.red,)
else
Container()
],);
});
})),);
}
}
来代替{{user[1]}}
来发送电子邮件,{{user.email}}
来代替{{user[2]}}
来代替名字{p}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。