简体   繁体   English

反应:无法设计列表项的索引号(使用地图)

[英]React: Cannot design list item's index number (using map)

I am trying to design a list, which consists a name, a highscore to the corresponding item, and the index before all.我正在尝试设计一个列表,其中包含一个名称、相应项目的高分和索引。 With easy 'className' designing, the highscore and name objects are successfully designable, but I cannot design the index.通过简单的“className”设计,可以成功设计高分和名称对象,但我无法设计索引。 I would like to align the text to the left, and also set the width of it.我想将文本向左对齐,并设置它的宽度。

.js file: .js 文件:

 <div className="category">
    Expert
    <ol>
      {expert.map((data) => (
        <li className="item" key={data.id}>
          <div>
            <div className="name">{data.name}</div>
            <div className="score">{data.highscore}</div>
          </div>
        </li>
      ))}
    </ol>
  </div>

the result looks the following way currently:结果目前看起来如下:

在此处输入图像描述

What is the way to limit the shorten the width of the index number, and align the text of it to the left (if it reaches 10 or 100, it is very ugly)有什么办法限制索引号的缩短宽度,把它的文字左对齐(如果达到10或者100,就很丑了)

The currently used.css classes have no influence on the index number, hence I don't post the code here, but it's kinda only the border coloring I did, and some width changing当前使用的.css 类对索引号没有影响,因此我不在这里发布代码,但它有点像我做的边框着色,以及一些宽度变化

If you want the index columns width to be narrower you can use padding-left: 0 on the element.如果您希望索引列的宽度更窄,您可以在元素上使用padding-left: 0

The marker for <ol> and <ul> elements cannot be styled (unless the ::marker pseudo-element is updated and supported). <ol><ul>元素的标记无法设置样式(除非更新并支持::marker伪元素)。 You'll have to generate your own counter.您必须生成自己的计数器。 For example:例如:

ol {
  counter-reset: ol; // the value can be anything
  list-style-type: none;
  padding-left: 40px;
}
li {
  position: relative;
}
li::before {
  content: counter(ol); // same case-sensitive name used above
  counter-increment: ol; // same case-sensitive name used above
  position: absolute;
  left: -40px;
}

You can adjust the padding-left and left properties to reduce/expand the width of the generated content.您可以调整padding-leftleft属性以减小/扩展生成内容的宽度。

Read more:阅读更多:

For simplicity, I would prefer first reset the at css the list styles as ol { list-style:none; }为简单起见,我宁愿首先在 css 将列表 styles 重置为ol { list-style:none; } ol { list-style:none; } (you can't style them after all). ol { list-style:none; } (毕竟你不能给它们设置样式)。 Finally, at your component i would use the second param index to create a div or span with a proper className where I could manipulate it as:最后,在您的组件中,我将使用第二个参数索引创建一个具有适当类名的 div 或跨度,我可以将其操作为:

 <div className="category">
    Expert
    <ol>
      {expert.map((data, index) => (
        <li className="item" key={data.id}>
          <div>
            <div className="list-id">{++index}</div>
            <div className="name">{data.name}</div>
            <div className="score">{data.highscore}</div>
          </div>
        </li>
      ))}
    </ol>
  </div>

I tried with li:before and this is the result.我用li:before试过,结果就是这样。

 * { box-sizing: border-box; } ol { counter-reset: ol; padding: 0; } li { display: flex; list-style: none; align-items: center; } li:before { padding: 8px; content: counter(ol); counter-increment: ol; width: 40px; text-align: right; padding-right: 5px; background: #ccc; } li div { border: 1px solid #000; padding: 8px; }
 <ol> <li> <div class="name"> Option 1 </div> <div class="score"> 100 </div> </li> <li> <div class="name"> Option 2 </div> <div class="score"> 100 </div> </li> <li> <div class="name"> Option 3 </div> <div class="score"> 30 </div> </li> </ol>

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

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