feat: ListVirtual can select all
This commit is contained in:
parent
77110e1e9a
commit
1d7370e557
3737
dist/index.js
vendored
3737
dist/index.js
vendored
File diff suppressed because it is too large
Load Diff
13
src/App.jsx
13
src/App.jsx
@ -5,7 +5,7 @@ import { useState } from "react";
|
||||
|
||||
const Testing = () => {
|
||||
const token = API_TOKEN;
|
||||
const [selectedVehicle, setSelectedVehicle] = useState(null);
|
||||
const [selectedVehicle, setSelectedVehicle] = useState([]);
|
||||
|
||||
return (
|
||||
<div style={{ padding: 8, marginTop: 12 }}>
|
||||
@ -19,8 +19,9 @@ const Testing = () => {
|
||||
label="Company"
|
||||
name="name"
|
||||
url="/company"
|
||||
defaultValue={442}
|
||||
defaultValue={[442,722]}
|
||||
size="small"
|
||||
multiple
|
||||
/>
|
||||
<ListVirtual
|
||||
label="License Plate"
|
||||
@ -28,14 +29,14 @@ const Testing = () => {
|
||||
url="/vehicle"
|
||||
defaultValue={selectedVehicle}
|
||||
getData={(data) => {
|
||||
if (data) {
|
||||
setSelectedVehicle(data.id);
|
||||
}
|
||||
const ids = data.map(item => item.id);
|
||||
setSelectedVehicle(ids);
|
||||
}}
|
||||
size="small"
|
||||
filter={[
|
||||
{name:"company_id",value: "442","logic_operator":"=","operator":"AND"}
|
||||
{name:"company_id",value: "442,722","logic_operator":"IN","operator":"AND"}
|
||||
]}
|
||||
multiple
|
||||
/>
|
||||
</ApiProvider>
|
||||
</div>
|
||||
|
||||
@ -72,6 +72,10 @@ export default function ListVirtual({
|
||||
filter.forEach(f => payload.where(f.logic_operator || "=", f.name, f.value, f.operator || "AND", f.value1 || null, f.table_name || null));
|
||||
}
|
||||
|
||||
if (filter.some(f => f.name === "company_id")) {
|
||||
payload.addJoin("company", ["name"]);
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await apiRequest.search(
|
||||
url,
|
||||
@ -155,18 +159,23 @@ export default function ListVirtual({
|
||||
useEffect(() => {
|
||||
if (defaultValue == null) return;
|
||||
|
||||
// Already object(s)
|
||||
if (typeof defaultValue === "object") {
|
||||
if (multiple) {
|
||||
// kosong, jangan request
|
||||
if (Array.isArray(defaultValue) && defaultValue.length === 0) {
|
||||
setSelected([]);
|
||||
return;
|
||||
}
|
||||
|
||||
// object
|
||||
if (
|
||||
Array.isArray(defaultValue) &&
|
||||
defaultValue.length &&
|
||||
typeof defaultValue[0] === "object"
|
||||
) {
|
||||
setSelected(defaultValue);
|
||||
return;
|
||||
}
|
||||
} else if (!Array.isArray(defaultValue)) {
|
||||
} else {
|
||||
if (typeof defaultValue === "object" && !Array.isArray(defaultValue)) {
|
||||
setSelected(defaultValue);
|
||||
return;
|
||||
}
|
||||
@ -208,10 +217,7 @@ export default function ListVirtual({
|
||||
};
|
||||
|
||||
const displayValue = multiple
|
||||
? (selected || [])
|
||||
.map((x) => x[name]?.trim())
|
||||
.filter(Boolean)
|
||||
.join(", ")
|
||||
? `${selected?.length || 0} ${label} selected`
|
||||
: selected?.[name] || "";
|
||||
|
||||
const isSelected = (row) => {
|
||||
@ -222,6 +228,56 @@ export default function ListVirtual({
|
||||
return (selected || []).some((x) => x.id === row.id);
|
||||
};
|
||||
|
||||
const handleSelectAll = async () => {
|
||||
setLoading(true);
|
||||
|
||||
const pageSize = 1000;
|
||||
const requests = [];
|
||||
|
||||
for (let offset = 0; offset < totalRecord; offset += pageSize) {
|
||||
const payload = new FilterTableBuilder()
|
||||
.setPaging(offset, pageSize)
|
||||
.setOrder([name], true);
|
||||
|
||||
if (search) {
|
||||
payload.like(name, search);
|
||||
}
|
||||
|
||||
if (filter?.length) {
|
||||
filter.forEach(f =>
|
||||
payload.where(
|
||||
f.logic_operator || "=",
|
||||
f.name,
|
||||
f.value,
|
||||
f.operator || "AND",
|
||||
f.value1 || null,
|
||||
f.table_name || null
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
requests.push(
|
||||
apiRequest.search(
|
||||
url,
|
||||
payload.build(),
|
||||
{
|
||||
headers: headersRequest,
|
||||
},
|
||||
isApiGo
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const responses = await Promise.all(requests);
|
||||
|
||||
const allRows = responses.flatMap(res => res.data || []);
|
||||
|
||||
setSelected(allRows);
|
||||
getData?.(allRows);
|
||||
|
||||
setLoading(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<TextField
|
||||
@ -300,7 +356,7 @@ export default function ListVirtual({
|
||||
<>
|
||||
{multiple && (
|
||||
<ListItemButton
|
||||
onClick={() => console.log("")}
|
||||
onClick={handleSelectAll}
|
||||
sx={{ fontSize: 14 }}
|
||||
>
|
||||
Select All
|
||||
@ -316,7 +372,7 @@ export default function ListVirtual({
|
||||
<Checkbox checked={isSelected(row)} />
|
||||
)}
|
||||
|
||||
<ListItemText primary={row[name]} />
|
||||
<ListItemText primary={row[name]} secondary={row.join?.company_name} />
|
||||
</ListItemButton>
|
||||
))}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user