简体   繁体   English

使用Bootstrap进行条件渲染,保持布局完整

[英]React Conditional Rendering With Bootstrap, Keeping Layout In Tact

I'm working on a project using React and Bootstrap. 我正在使用React和Bootstrap进行项目。 I have one big bar graph and 2 little boxes that all need to sit horizontally together. 我有一个大条形图和两个小盒子,它们都需要水平放置在一起。

Here's how it should work.. expand the pen window to see them sit horizontally together (the correct way): 这是它应该如何工作的..展开笔窗口以查看它们水平放置在一起(正确的方式):

https://codesandbox.io/s/lO9rvrBYM https://codesandbox.io/s/lO9rvrBYM

However I need some conditional rendering logic in there in case there is no data and I don't want to display the big bar graph. 但是,在没有数据并且我不想显示大条形图的情况下,我需要一些条件渲染逻辑。 Check that out here: 在这里查看:

https://codesandbox.io/s/ELLzLo3g https://codesandbox.io/s/ELLzLo3g

In the example with conditional rendering it can no longer properly position the elements. 在带有条件渲染的示例中,它不再能够正确定位元素。 They sit permanently on top of one another. 他们永久地坐在彼此之上。

I think it has to do with line 69. I need to render that graph with one less closing </div> to keep the horizontal layout in tact, but that throws a syntax error. 我认为这与第69行有关。我需要使用较少的闭合</div>来呈现该图,以保持水平布局的完整性,但这会引发语法错误。

Anyone great at Bootstrap have any tips to solve this? Bootstrap的出色人士有解决此问题的提示吗?

First of all, your question & samples are not that clear on what you want to achieve, so my answer might be missing the point :) 首先,您的问题和示例不清楚您想要实现的目标,因此我的答案可能没有重点:)

You basically want a 2 column layout mixed with a conditionnal display for the first column. 基本上,您希望两列布局与第一列的条件显示混合在一起。

The column display will be handled fully by bootstrap (and you actually messed up a lot with your columns here), the conditionnal display should be managed through your component state. 引导程序将完全处理列显示(您实际上在这里对列进行了很多混乱),应该通过组件状态来管理条件显示。

First your layout should look like somthing like that : 首先,您的布局应如下所示:

<div className='row'>
    <div className='col-sm-7'>
        <div> [your chart here] </div>
    </div>
    <div className='col-sm-5'>
        <div> [your first block here] </div>
        <div> [your second block here] </div>
    </div>
</div>

Then you need a boolean value inside your component state that will decide whether or not the chart should be displayed. 然后,您需要在组件状态内部有一个布尔值,该值将决定是否应显示图表。 Lets assume you store this information in this.state.showChart 假设您将此信息存储在this.state.showChart中

<div className='row'>
    {this.state.showChart ?
        <div className='col-sm-7'>
            <div> [your chart here] </div>
        </div>
    : '' }
    <div className='col-sm-5'>
        <div> [your first block here] </div>
        <div> [your second block here] </div>
    </div>
</div>

If this is insuffisant, try to improve your explanations a bit and I can work on your sandbox samples directly :) 如果这还不够,请尝试改善您的解释,我可以直接在您的沙箱样本上进行工作:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM