简体   繁体   English

如何使用 Reactjs 根据其项目键打开模式对话框

[英]How to open a modal dialog based on its item key using Reactjs

Here I am facing some issue to open the modal dialog based on its item key .在这里,我面临一些基于其项目key打开模式对话框的问题。

I am creating a bog page where i want to display my blogs and open/ close the modal according to its key .我正在创建一个沼泽页面,我想在其中显示我的博客并根据其key打开/关闭模式。 and I want to use pagination as well to display 10 blogs per page.我也想使用分页来每页显示 10 个博客。 But somehow its taking same data for every modal open.但不知何故,它为每个打开的模式获取相同的数据。

any suggestion how can i handle this modal open and close?有什么建议我该如何处理这个模式的打开和关闭?

Thanks..谢谢..

//blog.js //blog.js

class Blogs extends Component{
    constructor(props) {
        super(props);
            this.state = {
                modal: false
            };

            this.handleOpenDialog = this.handleOpenDialog.bind(this);
            this.handleCloseDialog = this.handleCloseDialog.bind(this);
        }

    static propTypes = {
        getContact: PropTypes.func.isRequired,
        deleteContact: PropTypes.func.isRequired,
        resume: PropTypes.object.isRequired,
        isAuthenticated: PropTypes.bool,
        auth: PropTypes.object.isRequired,
        loading: PropTypes.object.isRequired
    }

    toggle = () => {
        this.setState({
            modal: !this.state.modal
        });
    }

    handleOpenDialog(id) {
        this.setState({
          openDialog: true,
          justClicked: id

        });
    }

    handleCloseDialog() {
    this.setState({
        openDialog: false
    });
    }

    componentDidMount() {
        this.props.getBlog();
    }

    onDeleteBlogClick = (id) => {
        this.props.deleteBlog(id);
    };


  cardDialog(blogs, user){
    return(
        <Grid>

             {blogs.map(({ _id, blog_short_desc, blog_name, blog_desc, blog_image_link, blog_by }) => (
            <Cell col={12}>
                <Dialog open={this.state.openDialog} className="open-dialog">
                    <FABButton onClick={this.handleCloseDialog} className="close-button" mini ripple>
                        <Icon name="close" />
                    </FABButton>
                    <DialogTitle>{blog_short_desc}</DialogTitle>
                    <img className="blogs-contents-image" src={blog_image_link} alt="blogs"/>
                    <h4>{blog_name}</h4>
                    <DialogContent className="dialog-contents">{blog_desc} </DialogContent>

                </Dialog>
            </Cell>
             ))}
        </Grid>
    )

  }

    render(){
        const { blogs, loading} = this.props.resume;
        const { isAuthenticated, user } = this.props.auth;
        return(
            <Container>

            {loading ? (
            <div><Loading/></div>
            ) : (
                <div>
                    { isAuthenticated ? 
                    <Button 
                        color="dark"
                        style={{marginBottom: '2rem'}}
                        onClick={this.toggle}
                    >
                        Add Blog
                    </Button> : <h4 className="mb-3 ml-4"> Please login to manage blog</h4> }
                    {this.cardDialog(blogs, user)}
                    <Grid id="todo">
                    {blogs.map(({ _id, blog_name, blog_by, date }) => (

                    <Cell key={_id} data-id={_id}>   
                    { this.props.isAuthenticated && (user.is_admin === true) ? 
                            <Button className="remove-btn"
                            color="danger"
                            size="sm"
                            onClick= {this.onDeleteBlogClick.bind(this, _id)}>
                                &times;
                            </Button> : null }
                        <Card shadow={5} className="cards-grid">
                            <CardTitle className="card-title-image"></CardTitle>
                            <CardText>
                                <b>{blog_name}</b>
                            </CardText>

                            <CardActions border>
                                <Button onClick={this.handleOpenDialog.bind(this, _id)}>Read</Button>
                                <p style={{ fontStyle:'italic', fontWeight:'bold'}}>By-{blog_by} <span style={{float:'right',}}>{Moment(date).format('Do MMMM YYYY')}</span></p>

                            </CardActions>
                        </Card>
                    </Cell>   
                ))} 
           </Grid>
                </div> 
                )}
            </Container>
        ) 
   }
}

const mapStateToProps = (state) => ({
    resume: state.resume,
    isAuthenticated : state.auth.isAuthenticated,
    auth: state.auth,
    loading: state.apiCallsInProgress > 0
});

export default connect(mapStateToProps, {getBlog, deleteBlog }) (Blogs);

//JSON //JSON

[
    {
        "_id": "5e9b895ae3929322e426eba5",
        "blog_short_desc": "Hey tammy",
        "blog_name": "ssfs",
        "blog_desc": "jhjhkj",
        "blog_image_link": "https://www.shutterstock.com/image-photo/studio-shot-lovely-woman-wears-halloween-1523214461",
        "date": "2020-04-18T23:12:26.415Z",
        "__v": 0
    },
    {
        "_id": "5e9b7f3a3e382b0a609409fa",
        "blog_short_desc": "heey siri",
        "blog_name": "sirirrrheyyy siri",
        "blog_desc": "hey siri",
        "blog_image_link": "https://www.shutterstock.com/image-photo/studio-shot-lovely-woman-wears-halloween-1523214461",
        "date": "2020-04-18T22:29:14.850Z",
        "__v": 0
    }
]

//current visual //当前视觉在此处输入图像描述

I think if you change我想如果你改变

<Dialog open={this.state.openDialog} className="open-dialog">

to

<Dialog open={this.state.openDialog && this.state.justClicked === _id} className="open-dialog">

It should work.它应该工作。

Currently it's displaying every modal when openDialog is true .目前,当openDialogtrue时,它会显示每个模式。 You just can't see it as they are layered on the screen.您只是看不到它,因为它们在屏幕上分层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 ReactJs 中的键从父级打开子组件模态 - How to open child component modal from parent by using its key in ReactJs 使用 ReactJS 上的 Props 打开和关闭对话框? 我的模态保持打开而不关闭 - Open and close Dialog using Props on ReactJS? My modal keeps open and don't close 如何在ReactJS中显示模式对话框? - How to display a modal dialog box in ReactJS? Reactjs:如何根据其各自的id执行编辑操作和打开模态 - Reactjs: How to perform edit action and open modal according to its respective id 如何从C#打开对话框(模式) - How open a dialog (modal) from c# 在Reactjs中打开子模式时如何关闭父模式? - how close parent modal when open child modal in Reactjs? 如何使用Jquery在URL中基于主题标签打开模式 - How to open a modal based on the hashtag in the URL using Jquery Jquery Bootstrap Modal ...如何从另一个模态对话框中打开另一个模态对话框 - Jquery Bootstrap Modal… How to open a different modal dialog from inside another modal dialog 如何在对话框加载时打开多字段的第一项? - How to open first item of multifield on load of dialog? 如何使用JQuery或JavaScript打开没有ID或类的引导方式对话框? - how to open bootstrap modal-dialog without id or class using JQuery or JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM