Kumpul4semut Com Tutorial Lengkap Crud Codeigniter 4 Dan React Js 4 Edit Data
Kumpul4semut Com Tutorial Lengkap Crud Codeigniter 4 Dan React Js 4 Edit Data
public function edit($id = NULL)
{
$get = $this‐>model‐>getQuotes($id);
if ($get) {
$code = 200;
$response = [
'status' => $code,
'error' => false,
'data' => $get,
];
} else {
$code = 401;
$msg = ['message' => 'Not Found'];
$response = [
'status' => $code,
'error' => true,
'data' => $msg,
];
}
return $this‐>respond($response, $code);
}
public function update($id = NULL)
{
$data = $this‐>request‐>getRawInput();
$simpan = $this‐>model‐>updateQuotes($data, $id);
if ($simpan) {
$msg = ['message' => 'Updated Quotes successfully'];
$response = [
'status' => 200,
'error' => false,
'data' => $msg,
];
return $this‐>respond($response, 200);
}
}
Penjelasan!
public function getQuotes($id = false)
{
if ($id === false) {
return $this‐>findAll();
} else {
return $this‐>getWhere(['quote_id' => $id])‐>getRowArray();
}
}
public function updateQuotes($data, $id)
{
return $this‐>db‐>table($this‐>table)‐>update($data, ['quote_id' => $id]);
}
http://localhost:8080/quotes/id/edit
// * id
// ganti dengan id data yang mau di edit
Dan update data di
http://localhost:8080/quotes/id
//dengan method put atau patch
//dengan mengirim body quotes baru
Biar tidak bingung mari kita pasang di react js nya jangan lupa
jalankan webpack –watch agar ketika kita mengedit project react js
nya langsung connect dengan codeigniter 4.
constructor(props) {
super(props);
this.state = {
listquotes: [],
quotes: "",
quote_id_edit: "",
isEdit: false,
};
this.onChange = this.onChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
quotes_id_edit merupakai state yang akan kita isi oleh id data yang mau kita edit
isEdit Ini untuk keperluan ui jadi kita akan pakai sebuah form jika state ini true maka form tersebut
terbuka dan tentunya untuk mengedit data.
<a
href="#"
class="badge badge‐warning"
onClick={() => this.edit(data.quote_id)}
>
Disitu kita memberi event jika tombol di klik makan fungsi edit jalan
sambil mengirim id data yang akan diedit.
{this.state.isEdit ? (
<div>
<input
class="btn btn‐warning btn‐sm"
type="button"
value="Edit Quotes"
onClick={() => this.update()}
/>
<input
class="btn btn‐danger btn‐sm"
type="button"
value="Close"
onClick={() => this.setState({ isEdit: false })}
onClick={() => this.setState({ isEdit: false })}
/>
</div>
) : (
<input
class="btn btn‐primary btn‐sm"
type="submit"
value="Add Quotes"
/>
)}
Penjelasan
Jadi tombol tambah data kita beri logic jika state isEdit true bukan
tombol tambah data yang ditampilkan melainkan tombol edit data.
edit(id) {
fetch("http://localhost:8080/quotes/" + id + "/edit", {
method: "get",
})
.then((respon) => respon.json())
.then((ra) => {
this.setState({
quotes: ra.data.quote,
quote_id_edit: id,
isEdit: true,
});
})
.catch((err) => {
console.log(err);
});
}
update() {
fetch("http://localhost:8080/quotes/" + this.state.quote_id_edit, {
method: "put",
headers: {
Accept: "application/json",
"Content‐Type": "application/x‐www‐form‐urlencoded",
},
body: `quote=${this.state.quotes}`,
})
.then((respon) => respon.json())
.then((ra) => {
this.componentDidMount();
});
}
import React, { Component } from "react";
import Shimmer from "react‐js‐loading‐shimmer";
export default class Home extends Component {
constructor(props) {
super(props);
this.state = {
listquotes: [],
quotes: "",
quote_id_edit: "",
isEdit: false,
};
this.onChange = this.onChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
onChange(event) {
this.setState({ quotes: event.target.value });
console.log(this.state.quotes);
}
handleSubmit(event) {
fetch("http://localhost:8080/quotes/create", {
method: "post",
headers: {
Accept: "application/json",
"Content‐Type": "application/x‐www‐form‐urlencoded",
},
body: `quote=${this.state.quotes}`,
})
.then((respon) => respon.json())
.then((ra) => {
this.componentDidMount();
});
event.preventDefault();
}
componentDidMount() {
fetch("http://localhost:8080/quotes")
.then((respon) => respon.json())
.then((ra) =>
this.setState({
listquotes: ra,
})
);
}
edit(id) {
fetch("http://localhost:8080/quotes/" + id + "/edit", {
method: "get",
})
.then((respon) => respon.json())
.then((ra) => {
this.setState({
quotes: ra.data.quote,
quote_id_edit: id,
isEdit: true,
});
})
.catch((err) => {
console.log(err);
});
}
update() {
update() {
fetch("http://localhost:8080/quotes/" + this.state.quote_id_edit, {
method: "put",
headers: {
Accept: "application/json",
"Content‐Type": "application/x‐www‐form‐urlencoded",
},
body: `quote=${this.state.quotes}`,
})
.then((respon) => respon.json())
.then((ra) => {
this.componentDidMount();
});
}
render() {
return (
<div class="container">
<div class="row">
<div class="col">
<div class="d‐flex justify‐content‐center">
<form onSubmit={this.handleSubmit}>
<label>
<input
type="text"
name="name"
class="form‐controll"
value={this.state.quotes}
onChange={this.onChange}
/>
</label>
{this.state.isEdit ? (
<div>
<input
class="btn btn‐warning btn‐sm"
type="button"
value="Edit Quotes"
onClick={() => this.update()}
/>
<input
class="btn btn‐danger btn‐sm"
type="button"
value="Close"
onClick={() => this.setState({ isEdit: false })}
/>
</div>
) : (
<input
class="btn btn‐primary btn‐sm"
class="btn btn‐primary btn‐sm"
type="submit"
value="Add Quotes"
/>
)}
</form>
</div>
<table class="table table‐hover">
<thead>
<tr>
<th scope="col">Quotes</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
{this.state.listquotes.map((data) => {
return (
<tr>
<td>{data.quote || <Shimmer height={"50px"} />}</td>
<td class="d‐flex justify‐content‐around">
<a
href="#"
class="badge badge‐danger"
>
delete
</a>
<a
href="#"
class="badge badge‐warning"
onClick={() => this.edit(data.quote_id)}
>
edit
</a>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
</div>
</div>
);
}
}
Tutorial Lengkap CRUD Codeigniter 4 dan React js Tutorial Lengkap CRUD Codeigniter 4 dan React js #5
#3 Show Data Delete Data
Tinggalkan Balasan
Komentar
Nama *
Email *
Situs Web
Simpan nama, email, dan situs web saya pada peramban ini untuk
komentar saya berikutnya.
Kirim Komentar
Kategori
codeigniter 4 ﴾5﴿
PHP ﴾4﴿
Python ﴾7﴿
react js ﴾5﴿
Termux ﴾5﴿
vps ﴾1﴿