哼啊啊啊啊啊啊啊啊啊

const app = createApp(App)
app.config.globalProperties.$http = axios
axios.defaults.baseURL = 'https://www.escook.cn'
app.use(store).use(router).mount('#app')
 <template>
  <table class="table table-bordered table-striped">
    <!-- 表格的标题区域 -->
    <thead>
      <tr>
        <slot name="header"></slot>
      </tr>
    </thead>
    <!-- 表格的主体区域 -->
    <tbody>
      <tr v-for="(item, index) of data" :key="item.id">
        <slot name="body" :item="item" :index="index"></slot>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  name: 'mytable',
  props: {
    data: {
      type: Array,
      required: true,
      default: []
    }
  }
}
</script>

<style></style>
<template>
  <div id="app">
    <my-table :data="goodslist">
      <template v-slot:header>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th> 
      </template>
      <template v-slot:body="{ item, index }">
        <td>{{ index + 1 }}</td>
        <td>{{ item.goods_name }}</td>
        <td>¥{{ item.goods_price }}</td>
        <td>
          <input
            type="text"
            class="form-control form-control-sm ipt-tag"
            v-if="item.inputVisible"
            v-focus
            v-model.trim="item.inputValue"
            @blur="onInputConfirm(item)"
            @keyup.enter="onInputConfirm(item)"
            @keyup.esc="item.inputValue = ''"
          />
          <button
            type="button"
            class="btn btn-primary btn-sm"
            v-else
            @click="item.inputVisible = true"
          >
            +Tag
          </button>
          <span
            class="badge badge-warning ml-2"
            v-for="item of item.tags"
            :key="item"
            >{{ item }}
          </span>
        </td>
        <td>
          <button
            type="button"
            class="btn btn-danger"
            @click="onRemove(item.id)"
          >
            删除
          </button>
        </td>
      </template>
    </my-table>
  </div>
</template>

<script>
import MyTable from '@/components/mytable/MyTable.vue'
export default {
  name: 'app',
  components: {
    MyTable
  },
  directives: {
    focus(el) {
      el.focus()
    }
  },
  setup() {},
  data() {
    return {
      goodslist: []
    }
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get('/api/goods')
      if (res.status !== 0) return console.log('失败')
      this.goodslist = res.data
      console.log(res.data)
    },
    onRemove(id) {
      this.goodslist = this.goodslist.filter((x) => x.id !== id)
    },
    onInputConfirm(item) {
      const val = item.inputValue
      item.inputValue = ''
      item.inputVisible = false
      if (!val || item.tags.indexOf(val) !== -1) return
      item.tags.push(val)
    }
  },
  created() {
    this.getGoodsList()
  }
}
</script>

<style lang="less"></style>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值