根据条件动态生成wxml元素的问题
发布于 6 年前 作者 vcai 17489 次浏览 来自 问答

前端代码

  <block wx:for='{{sudoku}}' wx:key='*this'>
    <block wx:if='{{index%9==0}}'>
      <view class='tr'>
    </block>
    <!--other codes here-->
    <block wx:if='{{index%9==0}}'>
      </view>
    </block>
  </block>

js代码:

data: {
  sudoku: new Array(81)
},

但这样报错,该如何实现类似功能?效果希望是这样的

5 回复

换个思路的话,我会考虑把sudoku转化为二维数组,然后双重遍历,不知道是不是适用

我最后放弃了,改成如下代码方式了:

<view class='table'>
  <block wx:for='{{sudoku}}' wx:key='*this'>
    <block wx:if='{{index%9==0}}'>
      <view class='tr line{{index/9 + 1}}'>
        <view class='td col1 {{(index/9==0||index/9==1 || index/9==2 || index/9==6 ||index/9==7 || index/9==8)?"":"tdbg"}} {{cellSelected==index?"cell-selected":"cell"}}' data-cell='{{index}}' bindtap='onCellClick'>0</view>
        <view class='td col2 {{(index/9==0||index/9==1 || index/9==2 || index/9==6 ||index/9==7 || index/9==8)?"":"tdbg"}} {{cellSelected==index+1?"cell-selected":"cell"}}' data-cell='{{index+1}}' bindtap='onCellClick'>0</view>
        <view class='td col3 {{(index/9==0||index/9==1 || index/9==2 || index/9==6 ||index/9==7 || index/9==8)?"":"tdbg"}} {{cellSelected==index+2?"cell-selected":"cell"}}' data-cell='{{index+2}}' bindtap='onCellClick'>0</view>
        <view class='td col4 {{(index/9==3||index/9==4 || index/9==5)?"":"tdbg"}} {{cellSelected==index+3?"cell-selected":"cell"}}' data-cell='{{index+3}}' bindtap='onCellClick'>0</view>
        <view class='td col5 {{(index/9==3||index/9==4 || index/9==5)?"":"tdbg"}} {{cellSelected==index+4?"cell-selected":"cell"}}' data-cell='{{index+4}}' bindtap='onCellClick'>0</view>
        <view class='td col6 {{(index/9==3||index/9==4 || index/9==5)?"":"tdbg"}} {{cellSelected==index+5?"cell-selected":"cell"}}' data-cell='{{index+5}}' bindtap='onCellClick'>0</view>
        <view class='td col7 {{(index/9==0||index/9==1 || index/9==2 || index/9==6 ||index/9==7 || index/9==8)?"":"tdbg"}} {{cellSelected==index+6?"cell-selected":"cell"}}' data-cell='{{index+6}}' bindtap='onCellClick'>0</view>
        <view class='td col8 {{(index/9==0||index/9==1 || index/9==2 || index/9==6 ||index/9==7 || index/9==8)?"":"tdbg"}} {{cellSelected==index+7?"cell-selected":"cell"}}' data-cell='{{index+7}}' bindtap='onCellClick'>0</view>
        <view class='td col9 {{(index/9==0||index/9==1 || index/9==2 || index/9==6 ||index/9==7 || index/9==8)?"":"tdbg"}} {{cellSelected==index+8?"cell-selected":"cell"}}' data-cell='{{index+8}}' bindtap='onCellClick'>0</view>
      </view>
    </block>
  </block>
</view>

    <block wx:if=’{{index%9==0}}’>

      <view class=‘tr’>

    </block>

    <!–other codes here–>

    <block wx:if=’{{index%9==0}}’>

      </view>

    </block>

你这个是啥操作???一个if头部<view>    一个if尾部的</view>???肯定报错啊

两个if条件一模一样,你为什么要把view拆开。

<block wx:for='{{sudoku}}' wx:key='*this'>
    <block wx:if='{{index%9==0}}'>
      <view class='tr'></view>
    </block>
    <!--other codes here-->
    <block wx:else>
      <view class='tr'></view>
    </block>
</block>
回到顶部