如何将从ANT Design ReaCJS选择的过滤器发送到Nodejs Backend?
如何将选择从Ant Design Reacjs选择的过滤器参数发送到Nodejs Backend? 我想应用过滤。因此,为此,我制作后端API,当我从 Postman 发送数据时,它可以正常工作。现在,我希望应该从fron-end(reactjs)发送过滤器数据参数。如何使用Ant Design Tree 组件从前端发送它? 这是我的代码:
// popup
const [isModalVisible, setIsModalVisible] = useState(false);
// dropdown tree states
const [expandedKeys, setExpandedKeys] = useState([]);
const [checkedKeys, setCheckedKeys] = useState([]);
const [selectedKeys, setSelectedKeys] = useState([]);
const [autoExpandParent, setAutoExpandParent] = useState(true);
const [information, setInformation] = useState([])
// handle tree's categories functions
const onExpand = (expandedKeys, info) => {
console.log("onExpand info", info)
console.log("onExpand", expandedKeys); // if not set autoExpandParent to false, if children expanded, parent can not collapse.
// or, you can remove all expanded children keys.
setExpandedKeys(expandedKeys);
setAutoExpandParent(false);
};
const onCheck = (checkedKeys, info) => {
// console.log("onCheck", checkedKeys)
console.log("onCheck info", info.node)
setCheckedKeys(checkedKeys);
setInformation((prevSelected)=>[...prevSelected, info.node])
};
const onSelect = (selectedKeys, info) => {
console.log("onSelect selectedKeys",selectedKeys)
console.log("onSelect info", info);
setSelectedKeys(selectedKeys);
};
// popup functions
const showModal = () => {
setIsModalVisible(true);
};
const handleOk = () => {
setIsModalVisible(false);
};
const handleCancel = () => {
setIsModalVisible(false);
};
const handleApplyFilter = ()=>{
console.log("Apply button ", information);
}
const treeData = [
{
title: "Shelf Life",
key: "0",
checkable: false,
children: [
{
title: "10",
key: "0-1",
checkable: true,
},
{
title: "20",
key: "0-2",
checkable: true,
}
],
}
];
return(
<div>
<div>
<Button
type="default"
size="large"
onClick={showModal}
className="filteration"
>
<FilterOutlined /> Filters
</Button>
<Modal
title="Filters"
visible={isModalVisible}
onOk={handleOk}
onCancel={handleCancel}
footer={[
<Button key="1" onClick={()=>setCheckedKeys([])}>Reset</Button>,
<Button key="2" type="primary" onClick={handleApplyFilter}>
Apply
</Button>,
]}
>
<Tree
checkable
onExpand={onExpand}
expandedKeys={expandedKeys}
autoExpandParent={autoExpandParent}
onCheck={onCheck}
checkedKeys={checkedKeys}
onSelect={onSelect}
selectedKeys={selectedKeys}
treeData={treeData}
/>
</Modal>
</div>
</div>
)
现在我希望当我单击“应用按钮”按钮时,提出了后端的帖子请求。发布请求数据将类似于我从Postman发送的数据。 请帮助我!提前致谢
How can I send filters parameters selected from Ant design Reacjs to nodejs backend?
I want to apply filtration. So for this purpose I make backend API which works correctly when I send data from Postman. Now I want that Filters data parameter should be send from fron-end(reactjs). How Can I send it from frontend using Ant design Tree component?
Here is my code:
// popup
const [isModalVisible, setIsModalVisible] = useState(false);
// dropdown tree states
const [expandedKeys, setExpandedKeys] = useState([]);
const [checkedKeys, setCheckedKeys] = useState([]);
const [selectedKeys, setSelectedKeys] = useState([]);
const [autoExpandParent, setAutoExpandParent] = useState(true);
const [information, setInformation] = useState([])
// handle tree's categories functions
const onExpand = (expandedKeys, info) => {
console.log("onExpand info", info)
console.log("onExpand", expandedKeys); // if not set autoExpandParent to false, if children expanded, parent can not collapse.
// or, you can remove all expanded children keys.
setExpandedKeys(expandedKeys);
setAutoExpandParent(false);
};
const onCheck = (checkedKeys, info) => {
// console.log("onCheck", checkedKeys)
console.log("onCheck info", info.node)
setCheckedKeys(checkedKeys);
setInformation((prevSelected)=>[...prevSelected, info.node])
};
const onSelect = (selectedKeys, info) => {
console.log("onSelect selectedKeys",selectedKeys)
console.log("onSelect info", info);
setSelectedKeys(selectedKeys);
};
// popup functions
const showModal = () => {
setIsModalVisible(true);
};
const handleOk = () => {
setIsModalVisible(false);
};
const handleCancel = () => {
setIsModalVisible(false);
};
const handleApplyFilter = ()=>{
console.log("Apply button ", information);
}
const treeData = [
{
title: "Shelf Life",
key: "0",
checkable: false,
children: [
{
title: "10",
key: "0-1",
checkable: true,
},
{
title: "20",
key: "0-2",
checkable: true,
}
],
}
];
return(
<div>
<div>
<Button
type="default"
size="large"
onClick={showModal}
className="filteration"
>
<FilterOutlined /> Filters
</Button>
<Modal
title="Filters"
visible={isModalVisible}
onOk={handleOk}
onCancel={handleCancel}
footer={[
<Button key="1" onClick={()=>setCheckedKeys([])}>Reset</Button>,
<Button key="2" type="primary" onClick={handleApplyFilter}>
Apply
</Button>,
]}
>
<Tree
checkable
onExpand={onExpand}
expandedKeys={expandedKeys}
autoExpandParent={autoExpandParent}
onCheck={onCheck}
checkedKeys={checkedKeys}
onSelect={onSelect}
selectedKeys={selectedKeys}
treeData={treeData}
/>
</Modal>
</div>
</div>
)
Here is my post request which I send from Postman
Now I want that when I click Apply button a post request is made to backend. Post request data will be similar to the data that I send from Postman.
Kindly Help me out! Thanks in Advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在Treedata中,在每个子节点/对象中添加父和值密钥:
现在创建一个新状态:
当您选中任何复选框时,它将调用OnCheck函数。
这里每个节点都有其父键。在INFO中,我们具有
checkednodes
,其中包含所有标记的节点。我只是在它们上映射并从每个节点获取值,然后将数组分配给该节点的父。In treeData, add a parent and value key in each children node/object:
Now create a new state:
When you check any checkbox, it will call onCheck function.
Here each node have its parent key. In info, we have
checkedNodes
which have all the nodes that are marked checked. I just mapped over them and get the value from each node and assign the array to parent of that node.