-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistView_old.js
165 lines (138 loc) · 6.24 KB
/
listView_old.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Importing the node modules, child components, services and controllers used
inside IntranetHome component */
import React from 'react';
import { Component } from 'react';
import $ from "jquery";
import { Link } from 'react-router-dom';
import axios from 'axios';
import update from 'react-addons-update';
/* IntranetHome Component initialization */
class ListView extends Component {
/* Initializing objects of its IntranetHome class */
constructor(props) {
super(props)
this.state = {
userid: '',
title: '',
data: [
],
index: -1
}
}
/* It is invoked immediately after a component is mounted */
componentDidMount() {
//console.log('statevalue1',$.getJSON('https://jsonplaceholder.typicode.com/posts'));
// var retData = $.getJSON('https://jsonplaceholder.typicode.com/posts');
// this.setState({ data: "tests" });
// console.log('newstate',retData);
// fetch('https://jsonplaceholder.typicode.com/posts').
// then(response => response.json()).then((repos) => {
// console.log(repos);
// this.setState({
// data: repos
// });
// });
let xyz = 'test';
let _this = this;
axios.get('https://jsonplaceholder.typicode.com/posts')
.then(function (response) {
_this.setState({
data: response.data
});
})
.catch(function (error) {
console.log(error);
});
}
onEditclick(val, index) {
this.setState({ userid: val.id, title: val.title, body: val.body, index});
}
async saveData(e){
const existingPost = this.state.data[this.state.index];
const updatedPost = update(existingPost, { id: {$set: Number(this.state.userid)}, title: {$set: this.state.title}, body: {$set: this.state.body}});
let updatedPosts = update(this.state.data, { [this.state.index]: {$set: updatedPost} });
await this.setState({data : updatedPosts});
setTimeout(function(){
$("#listViewModal").hide();
},1000)
}
setValue(field, e) {
var object = {};
object[field] = e.target.value;
this.setState(object);
}
/* It is invoked to return html content */
render() {
console.log(this.state.data, '== data');
var tableStyle = {
border: '1px solid #ccc',
border_collapse: 'collapse',
width: '70%',
margin: '2% auto'
};
var thstyle = {
borderRight: '1px solid #ccc',
width: 'auto',
padding: '8px',
background: '#000',
color: '#fff'
}
var trstyle = {
border: '1px solid #ccc'
}
var tdstyle = {
borderRight: '1px solid #ccc',
width: 'auto',
padding: '8px'
}
return (
<div>
<table style={tableStyle}>
<thead>
<th style={thstyle}>ID</th>
<th style={thstyle}>Title</th>
<th style={thstyle}>Body</th>
<th style={thstyle}>Actions</th>
</thead>
{this.state.data.slice(0, 5).map((dynamicComponent, index) => <tbody key={index}>
<tr style={trstyle}>
<td style={tdstyle}>{dynamicComponent.id}</td>
<td style={tdstyle}>{dynamicComponent.title}</td>
<td style={tdstyle}>{dynamicComponent.body}</td>
<td style={tdstyle}>
<button onClick={this.onEditclick.bind(this, dynamicComponent,index)} data-toggle="modal" data-target="#listViewModal" className="btn"><i className="fa fa-pencil" aria-hidden="true"></i>
</button>
</td>
</tr>
</tbody>)}
</table>
<div className="modal fade" id="listViewModal" role="dialog">
<div className="modal-dialog">
<div className="modal-content rm-border-radius">
<div className="modal-header">
<h2 style={{float:'left'}}>Edit Details</h2>
<button type="button" className="close" data-dismiss="modal" aria-label="">
<span>×</span>
</button>
</div>
<div className="modal-body">
<div className="form-group">
<label htmlFor="userid">User ID:</label>
<input className="form-control" required id="userid" placeholder='UserID' onChange={this.setValue.bind(this, 'userid')} value={this.state.userid} />
<label htmlFor="title">Title:</label>
<input className="form-control" required id="title" placeholder='Title' onChange={this.setValue.bind(this, 'title')} value={this.state.title} />
<label htmlFor="body">Body:</label>
<textarea className="form-control" id="body" placeholder='Body' onChange={this.setValue.bind(this, 'body')} value={this.state.body} />
<div className="clearfix"></div>
<label style={{margin:'30px 0'}}> </label>
<button onClick={this.saveData.bind(this)} className="btn btn-primary" >Submit</button>
</div>
</div>
</div>
</div>
</div>
</div>
);
}
}
export default ListView;