简体   繁体   English

检查 p 中有多少行文本并存储在变量中

[英]Check how many lines of text in p and store in variable

I have a table with a column called "comments".我有一个表,其中有一列名为“comments”。 In a particular cell, ONLY when there are more than 2 lines of text I want to:在特定单元格中,仅当我想要的文本超过 2行时:

  • render a clickable text showing "show more"呈现显示“显示更多”的可点击文本
  • line clamp the text to 2 lines line 将文本钳制为 2 行

Currently, I have a line clamp in every cell set like so:目前,我在每个单元组中都有一个线夹,如下所示:

const ClampedText = styled.p`
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
`;

This element is used like so: <ClampedText>{description}</ClampedText> where description is the text in a cell.该元素的使用方式如下: <ClampedText>{description}</ClampedText>其中description是单元格中的文本。

Using styled-components.使用样式组件。

What I basically want to do is get a boolean variable stating wether there are more than two lines of text in a particular p- and then conditionally render the component accordingly (with/without "show more" and line-clamp.)我基本上想要做的是获得一个 boolean 变量,说明特定 p 中是否有超过两行文本,然后相应地有条件地渲染组件(有/没有“显示更多”和线夹。)

How can this be done?如何才能做到这一点?

Idea主意

The "comments" column has 10% of the width of the table, so I'm thinking that can be used somehow with som JS and calculating the number of lines going to be showing. “comments”列的宽度是表格宽度的 10%,所以我认为它可以以某种方式与 som JS 一起使用并计算要显示的行数。

You can use string.length to know the length and decide to show a show more link or not.您可以使用string.length来了解长度并决定是否show more链接。 Here is the full code on codesandbox这是codesandbox上的完整代码

  const [paragraph, setParagraph] = useState({
    show: null,
    paragraph: "",
    clicked: false
  });

   ...

  return (
    <>
      <p>
        {paragraph.paragraph}
        <span
          style={{ display: paragraph.show ? "inline" : "none" }}
          onClick={() =>
            setParagraph({
              show: true,
              clicked: !paragraph.clicked,
              paragraph: paragraph.clicked
                ? dummyText.substring(0, 25).concat(" ... ")
                : dummyText
            })
          }
        >
          {paragraph.clicked ? "Show less" : "Show more"}
        </span>
      </p>
    </>
  );

Since text line count can be different according to screen size, you can check string length for the condition:由于文本行数可能因屏幕大小而异,因此您可以检查条件的字符串长度:

const ClampedText = (props) => {

   const [isExpanded, setIsExpanded] = useState(false);

   const expander = () => {
       if(props.children.toString().length >= 200){
          setIsExpanded(!isExpanded);
       }
   }

   return (
    <div onClick={() => expander()}} className={isExpanded ? 'expanded' : ''}>
     <p>
      {props.children.toString().length < 200 ? 
          props.children.toString() :
          (isExpanded? props.children.toString(): props.children.toString().substr(0,200) + '...')  }
     </p>
    </div>
   )
}

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

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