使用View Design 组件库的 Table 组件,列的显示和隐藏

文章介绍了ViewDesign组件库中Table组件如何设置列,包括使用变量columns、插槽功能的不同实现以及如何通过计算属性动态控制列的显示和隐藏。作者提到在处理不同条件下的列显示时可能会遇到复杂性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先View Design 组件库的 Table 设置列的时候是使用一个变量 columns 来设置的一般写为:

<Table :columns="columns" :data="data"></Table>
<script>
    export default {
        data () {
            return {
                columns: [
                    {
                        title: 'Name',
                        key: 'name'
                    },
                    {
                        title: 'Age',
                        key: 'age'
                    },
                    {
                        title: 'Address',
                        key: 'address'
                    }
                ],
                data: []
            }
        }
    }
</script>

然后呢这个表格就会显示 columns 中所有的列都会按照当columns的顺序进行列展示(好像是改不了的,也不能不显示某一列,在项目中的个人见解哈)

这个也是可以设置插槽的目前看到两种写法:

1.官网写法

<Table :columns="columns" :data="data">
   <template #name="{ row }">
        {{ row.name? row.name: '--' }}
   </template>
   <template  #action="{ row, index }">
        {{ row.age? row.age: 0 }}
   </template>
</Table>

2.这种我用过也可以

<Table :columns="columns" :data="data">
 <template slot-scope="{ row, index }" slot="name">
     {{ row.name? row.name: '--' }}
  </template>
</Table>

<script>
    export default {
        data () {
            return {
                columns: [
                    {
                        title: 'Name',
                        key: 'name',
                        slot: 'name'
                    },
                    {
                        title: 'Age',
                        key: 'age',
                        slot: 'name'
                    },
                    {
                        title: 'Address',
                        key: 'address'
                    }
                ],
                data: []
            }
        }
    }
</script>

说了这么多基础下面来说说怎么控制列的显示和隐藏吧:

就是把columns放到计算属性里面 让他跟随条件的变化而变化

但是我觉得这样写也是有问题的,那如果不同的条件显示的列不一样呢这样就会非常麻烦的,因为他在Table标签里面没有办法控制他的显示与隐藏(这里讲的也不知道对不对,可能有方法我不知道吧)

 computed:{
    columns: function () {
      let columns =  columns: [
                    {
                        title: 'Name',
                        key: 'name',
                        slot: 'name'
                    },
                    {
                        title: 'Age',
                        key: 'age',
                        slot: 'name'
                    },
                    {
                        title: 'Address',
                        key: 'address'
                    }
                ],
      // 控制性别的显示 并且显示在第一个
      if( 需要展示性别的条件成立){
       // 反正这里就是根据你的要求  使用数组的方法  需要放到第几个就放在第几个喽
        columns.unshift({
          title: 'Gender',
          key: 'gender',
        })
      }
      return columns;
    },

### 如何在 Bootstrap Table使用 iView 组件进行格式化 为了实现在 `Bootstrap Table` 中利用 `iView` 组件来格式化特定的数据展示效果,可以通过配置 `formatter` 属性并结合 Vue.js 的渲染机制完成此功能。下面提供一段具体的实现方式: #### 实现思路 通过设置表格的 `formatter` 函数,在该函数内部创建一个临时 DOM 节点用于挂载 Vue 实例及其对应的 iView 组件,从而动态生成所需的 UI 控件。 #### 示例代码 ```html <!-- HTML 部分 --> <table id="table" data-toggle="table"> <thead> <tr> <th data-field="state">状态</th> <!-- 使用 formatter 进行特殊处理的一 --> <th data-field="name">名称</th> <th data-field="price">价格</th> </tr> </thead> </table> <script type="text/x-template" id="statusTemplate"> <div> <Button :type="props.row.state === &#39;active&#39; ? &#39;success&#39; : &#39;error&#39;" @click="handleClick">{{ props.row.state }}</Button> </div> </script> ``` ```javascript // JavaScript (Vue + iView)部分 new Vue({ el: &#39;#app&#39;, components: { Button: require(&#39;view-design&#39;).Button // 导入 iView 的按钮组件 }, methods: { handleClick(event){ console.log("点击事件触发", event); } } }); $(&#39;#table&#39;).bootstrapTable({ columns: [{ field: &#39;state&#39;, title: &#39;状态&#39;, formatter: function(value, row, index) { let div = document.createElement(&#39;div&#39;); const vm = new Vue({ template: "#statusTemplate", data() {return {row}}, mounted(){ this.$nextTick(() => { ReactDOM.render(React.createElement(this.$options.template), div); }); } }).$mount(div); return div.innerHTML; } }, /* ...其他... */], }); ``` 上述代码展示了如何在一个名为 "状态" 的中嵌入来自 `iView` 库中的按钮控件,并依据实际数据改变其外观样式[^1]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值