如何在React Native中更改特定列的样式? [英] How to change style for an particular column in React Native?

查看:74
本文介绍了如何在React Native中更改特定列的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我一直在进行矩阵设计的桌子设计,以将可用的座椅放在公共汽车上!因此,我遍历2d和3d数组以创建此视图!

So, i have been making a matrix kind of table design to place available seats in bus ! So, am looping though 2d and 3d arrays to make this view !

这是输出的图片!

在您看到的这张照片中-第一行首先包含一个大的垂直矩形座椅,然后是三个方形座椅!但是,作为支票的第二行-它跳了很大的差距?

In this pic as you see - the first row contains a big vertical rectangle seat first and then three square seats ! But, as the check the second row - it has jumped with a huge gap ?

这是由于第一个垂直矩形而发生的,因为矩形延伸了一点-第二行从此之后开始,

As this is happening because of the first vertical rectangle, as the rectangle extends a bit - the second rows starts after that,

但是预期的结果如下:

如何执行此操作,因为我尝试将样式设置为正方形,但是没有发生任何预期的事情!

How to implement this, as i have tried to style to squares but there nothing happening as expected !

这是我生成布局的代码!

Here is my code for layout generation !

{seatsMapping.map((z, i) => {
                    return (
                      <View key={i} style={{  }}>  
                        {z.map((y, j) => {
                          return (
                            <View style={{ flexDirection: 'row' }} key={j}>
                              {y.reverse().map((seat, p) => {
                                return (
                                  <View
                                    style={{ flex: 1,  }}
                                    key={p}>
                                        {
                                            seat != null &&  
                                            <View style={{ flexDirection: 'column' }}>
                                                {this.seatLayoutgeneration(seat)}
                                            </View>
                                        }
                                  </View>
                                );
                              })}
                            </View>
                          );
                        })}
                      </View>
                    );
                  })}

从上面可以看到,我们正在循环进入3d数组,所以如果你们需要这些功能,请告诉我会再次更新!

As from the above, you can see am looping into 3d array, so if you guys need those function do let me know will update again !

在此处查看复制的小吃

推荐答案

答案是思考专栏"

您正在创建一个数组并将其呈现为行,无论如何都会以间距问题呈现数据.因此,解决此问题的最佳方法是在列中呈现数据.

You are creating an array and rendering it as rows which would anyway render data with the spacing issue you have. So the best way to sort this problem would be to render data in columns.

在这里,您必须遍历行并先创建列,然后再呈现它们.您可以看到该逻辑的内联注释.

Here you have to iterate through the rows and create columns first and then render them. You can see the inline comments for the logic.

  const renderData = (arr) => {
    const columns = [];

    //Go through the items in the row
    for (let i = arr[0].length - 1; i > -1; i--) {
      const column = [];
      //For each column in the row generate the contents
      for (let x = 0; x < arr.length; x++) {
        const seat = arr[x][i];
        const output = (
          <View style={{ flex: 1 }}>
            {seat?.name && seat.length === '2' && seat.width === '1' ? (
              <View
                style={{
                  width: 35,
                  height: 80,
                  borderRadius: 5,
                  borderColor: '#000',
                  backgroundColor: '#000',
                  borderWidth: 1,
                  marginBottom: 10,
                }}></View>
            ) : seat?.name && seat.length === '1' && seat.width === '1' ? (
              <View
                style={{
                  width: 35,
                  height: 35,
                  borderWidth: 1,
                  marginBottom: 10,
                  borderColor: 'red',
                }}></View>
            ) : (
              <View style={{ width: 40 }}></View>
            )}
          </View>
        );
        //Add the content to the column
        column.push(output);
      }
      //Add it to main container
      columns.push(<View>{column}</View>);
    }

    return columns;
  };

  return (
    <View>
      <Text>----- Seating ----</Text>

      {seatsMapping.map((z, i) => {
        return (
          <View key={i}>
            <Text>----- Z Axis Level: {i} ----</Text>
            <View style={{ flexDirection: 'row' }}>{renderData(z)}</View>
          </View>
        );
      })}
    </View>
  );

零食与输出 https://snack.expo.io/@guruparan/77427f

编辑

没有所有这些处理,您就可以做到这一点,这是紧接着又直接的方式

Without all this processing you can do this, which is way after and straight forward

export default function App() {
  const page1 = data.seats.filter((x) => x.zIndex == 0);
  const columnCount = 3;
  return (
    <View style={styles.container}>
      {page1.map((item) => (
        <View
          style={{
            width: 35 * item.width,
            height: 35 * item.length,
            backgroundColor: 'red',
            margin: 3,
            position: 'absolute',
            left: (columnCount - item.row) * 40,
            top: item.column * 40,
          }}>
          <Text>{columnCount - item.row + ' ' + item.column}</Text>
        </View>
      ))}
    </View>
  );
}

您可以在这里尝试 https://snack.expo.io/@guruparan/busseat

这篇关于如何在React Native中更改特定列的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆