![](/img/trans.png)
[英]show more and show less toggle button is flatlist objects in react native
[英]How to make Show more / Show less toggle button for API dynamic data in paragraph or simple text in REACT?
我的要求是,我在 web 頁面上有多個選項卡,並且每個選項卡中的數據來自 API。 數據是動態的並且每天都在變化。 我有一個 div 說一個固定高度的矩形框,在它下面,有一個按鈕“閱讀更多/閱讀更少”。 如果該矩形框中包含的數據超過該框的高度,則會顯示“閱讀更多”按鈕,否則如果數據量小,則按鈕不可見。 我已經完成了大部分這些事情,我的代碼有兩個問題是,
我附上了我的示例代碼。 出於安全問題,我刪除了一些類名,請幫助解決以上兩點..
<div id="tab_number" >
<div className="">
<div className=''>
<div ref={`showTheScrolling`} className={`${Theme} parent`} style={{overflow:"hidden"}}>
<h3>Tab Heading</h3>
{
<div className="content" style={{height:"475px"}} dangerouslySetInnerHTML={{ __html: this.state.thisData.tabData }}></div>
}
</div>
{this.state.thisData.tabData.length > 1500 ?
<div>
<div ref={`buttonShowMore`} className="">
<a id="ClickForShowMore" onClick={() => { this.toggleShow_more() }}><b>Show More</b></a>
</div>
<div ref={`buttonShowLess`} className=" showButton_hide">
<a id="ClickForShowMore" onClick={() => { this.toggleShow_less() }}><b>Show Less</b></a>
</div>
</div> : ""
}
</div>
</div>
和 render() 部分上面的兩個函數
toggleShow_more = () => {
console.log(this.refs)
this.refs[`showTheScrolling`].classList.add('filterscroller');
this.refs[`buttonShowMore`].classList.add('showButton_hide');
this.refs[`buttonShowLess`].classList.remove('showButton_hide');
}
toggleShow_less = () => {
console.log(this.refs)
this.refs[`showTheScrolling`].classList.remove('filterscroller');
this.refs[`buttonShowMore`].classList.remove('showButton_hide');
this.refs[`buttonShowLess`].classList.add('showButton_hide');
this.refs[`showTheScrolling_${id}`].scrollTo({top: 0, behavior: 'smooth'});
}
對於上述第一個問題,我們可以使用 splitString()。 這里我們最多計算 300 個字符,如果計數更多,那么我們檢查前 50 個單詞並在小 window 中顯示它們,然后提供切換按鈕以供完整查看。 如果字符數少於 300,則不切換。
所以,這里的切換 window 大小是動態的。 它取決於字符或字數,並且隨着字體大小而變化。 在這里可以輕松解決在其他一些 div、box 或 button 后面部分查看文本的問題。
{
// if tabData character length > 300 then
this.state.thisData.tabData.length > 300 ?
<div>
{this.state.readMore ?
<div className="your-classname" style={{ margin: "20px" }}
dangerouslySetInnerHTML={{ __html: this.state.thisData.tabData }}>
</div>
:
<div className="your-classname" style={{ margin: "20px" }}
dangerouslySetInnerHTML={{ __html: this.splitString(this.state.thisData.tabData, " ", 50) }}>
</div>
}
<div>
<a className="your-classname2" style={{ }}
onClick={() => this.toggleReadMore()}>
{this.state.addRead ?
// if you are working with mobile view also, the for css adjustment select appropriate class
<div className={this.props.isMobileView ? "selectclass-if-isMobileViewTrue":"selectclass-if-isMobileViewFalse"}
>READ MORE
</div>
</React.Fragment>
:
<React.Fragment>
<div className={this.props.isMobileView ? "selectclass-if-isMobileViewTrue":"selectclass-if-isMobileViewFalse"}
>READ LESS
</div>
</React.Fragment>}
</a>
</div>
</div>
:
// if tabData character length < 300 then
<div className="your-classname" style={{ margin: "20px" }}
dangerouslySetInnerHTML={{ __html: this.state.thisData.tabData }}>
</div>}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.