vue2.0和quasarUI里面q-markup-table和输入框封装表格过滤组件

文章描述了一个Vue组件中如何使用计算属性filteredTableData,通过特定条件(包括名称、热量和脂肪含量)对数组tableData进行过滤,实现实时搜索和显示相关数据。
摘要由CSDN通过智能技术生成

特定的条件筛选出数组中的项

完整代码

<template>
  <div class="q-pa-md">
    <q-markup-table>
      <thead>
        <tr>
          <th class="text-center" style="width: 150px; text-align: center">
            代理
            <q-input outlined v-model="filterText" />
          </th>
          <th class="text-center">
            名称
            <q-input outlined v-model="name" />
          </th>
          <th class="text-center">
            周期
            <q-input outlined v-model="period" />
          </th>
          <th class="text-center">
            单位
            <q-input outlined v-model="unit" />
          </th>
          <th class="text-center">
            类型
            <q-input outlined v-model="type" />
          </th>
          <th class="text-center">
            状态
            <q-input outlined v-model="text" />
          </th>
          <th class="text-center">
            有效标志
            <q-input outlined v-model="text" />
          </th>
          <th class="text-center">
            更新人
            <q-input outlined v-model="text" />
          </th>
          <th class="text-center">
            更新时间
            <q-input outlined v-model="text" />
          </th>
          <th class="text-center">
            备注
            <q-input outlined v-model="text" />
          </th>
        </tr>
      </thead>

      <tbody>
        <tr v-for="(n, i) in filteredTableData" :key="i">
          <td class="text-center">
            {{ n.name }}
          </td>
          <td class="text-center">
            {{ n.calories }}
          </td>
          <td class="text-center">
            {{ n.fat }}
          </td>
          <td class="text-center">
            {{ n.carbs }}
          </td>
          <td class="text-center">
            {{ n.protein }}
          </td>
          <td class="text-center">
            {{ n.sodium }}
          </td>
          <td class="text-center">
            <q-checkbox disable v-model="n.ironss" />
          </td>
          <td class="text-center">
            {{ n.calcium }}
          </td>
          <td class="text-center">
            {{ n.iron }}
          </td>
          <td class="text-center">
            {{ n.irons }}
          </td>
        </tr>
      </tbody>
    </q-markup-table>
  </div>
</template>

<script>
export default {
  name: "GhDialog",
  data() {
    return {
      filterText: "",
      text: "",
      period: "",
      type: "",
      name: "",
      unit: "",
      val: [],
      tableData: [
        {
          name: "Frozen Yogurt",
          calories: "11",
          fat: 6.0,
          carbs: 24,
          protein: 4.0,
          sodium: 87,
          calcium: "14%",
          iron: "1%",
          irons: "1%",
          ironss: true,
        },
        {
          name: "Ice cream sandwich",
          calories: "237",
          fat: 9.0,
          carbs: 37,
          protein: 4.3,
          sodium: 129,
          calcium: "8%",
          iron: "1%",
          irons: "1%",
          ironss: false,
        },
      ],
    };
  },
  computed: {
    filteredTableData() {
      return this.tableData.filter((item) => {
        return (
          item.name.toLowerCase().includes(this.filterText.toLowerCase()) &&
          item.calories.toLowerCase().includes(this.name.toLowerCase())&&
          item.fat.toString().toLowerCase().includes(this.period.toLowerCase())
        );
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .q-field__control {
  height: 32px;
}
table {
  border-collapse: collapse;
}
table thead tr th {
  border-bottom: 1px solid #000000;
}

table th,
table td {
  border: 1px solid #d9d9d9;
}
</style>

它用于过滤tableData数组。具体来说,它根据特定的条件筛选出数组中的项。下面是对代码的解释:

  1. filteredTableData() {: 定义一个名为filteredTableData的函数
  2. return this.tableData.filter((item) => {: 使用数组的filter方法来过滤tableData数组。对于数组中的每一项(这里称为item),都会执行括号中的函数,并根据该函数的返回值决定是否保留该项。
  3. return (: 开始定义返回的过滤函数。
  4. em.name.toLowerCase().includes(this.filterText.toLowerCase()) &&: 检查当前项的name属性(转换为小写后)是否包含this.filterText(也转换为小写后)。这是第一个过滤条件。
  5. item.calories.toLowerCase().includes(this.name.toLowerCase()) &&: 检查当前项的calories属性(转换为小写后)是否包含this.name(也转换为小写后)。这是第二个过滤条件。
  6. item.fat.toString().toLowerCase().includes(this.period.toLowerCase()): 检查当前项的fat属性(先转换为字符串,再转换为小写后)是否包含this.period(也转换为小写后)。这是第三个过滤条件。
  7. );: 结束返回的过滤函数。
  8. });: 结束数组的filter方法。
  9. },: 结束filteredTableData函数的定义。

效果图:

通过搜索过滤表格里面的数据

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue2.0和ElementUI2.0中使用el-table表格组件时,自适应高度可以通过以下步骤来实现: 1. 确定数据源 首先,我们需要确定用于填充表格的数据源。在el-table中,数据通常以数组的形式传递,其中每个数组元素代表表格中的一行数据。 2. 计算表格高度 在Vue中,我们可以使用计算属性来动态计算表格的高度。计算属性会根据数据源的变化自动更新表格高度。例如,我们可以使用下面的代码来计算表格高度: computed: { tableHeight: function() { // 获取窗口可见区域高度 var viewportHeight = window.innerHeight; // 计算表格头和分页组件的高度 var tableHeaderHeight = this.$refs.table.$el.querySelector('.el-table__header-wrapper').offsetHeight; var paginationHeight = this.$refs.pagination.$el.offsetHeight; // 计算表格内容区域的高度 var contentHeight = viewportHeight - tableHeaderHeight - paginationHeight - 20; return contentHeight; } } 在上面的代码中,我们首先获取窗口可见区域的高度,然后根据表格头和分页组件的高度计算表格内容区域的高度。最后,将计算出的高度返回。 3. 绑定表格高度 我们将计算属性中计算出的表格高度绑定到el-table组件中的height属性上,以实现自适应高度。例如,我们可以使用下面的代码将表格高度绑定到el-table组件上: <el-table :data="tableData" ref="table" :height="tableHeight"> 在上面的代码中,我们首先使用data属性传递数据源,然后使用ref属性给el-table组件添加一个引用,最后使用height属性将计算属性中计算出的表格高度绑定到组件上。 通过上述方法,我们可以在Vue2.0和ElementUI2.0中实现自适应高度的el-table表格组件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值