当前位置:   article > 正文

element-ui table 默认勾选 、编辑自动勾选_el table中点击行默认打勾

el table中点击行默认打勾

在这里插入图片描述

多选框全部禁用
多选框使用selectable决定这一行是否可以勾选
注意:不显示多选框的行也要禁用,这样在使用toggleAllSelection()不会选中这些行

this.$refs.editList.toggleRowSelection(row, true/false) //当前行勾选切换

this.$refs.editList.toggleAllSelection() //默认勾选

<template>
  <div>
    <el-dialog
      title="编辑折扣模板"
      top="6vh"
      :visible="editTemplateShow"
      width="80%"
      :before-close="beforeClose"
      @close="handleCloseDialog"
      @open="openDialog"
    >
      <el-form
        ref="discountForm"
        :model="discountForm"
        label-width="120px"
        :rules="discountFormRules"
        style="width: 92%"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="折扣模板名称" prop="planName">
              <el-input v-model="discountForm.planName" placeholder="请输入折扣模板名称" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-table
        ref="editList"
        v-loading="loading"
        border-bottom
        style="width: 100%; margin-top: 20px"
        class="table"
        :header-cell-style="{
          background: '#FAFAFA'
        }"
        :data="editList"
        @selection-change="SelectionChange"
      >
        <el-table-column type="selection" width="55" :selectable="checkSelectable" />
        <el-table-column type="index" :index="indexMethod" align="center" width="55" label="序号" />
        <el-table-column prop="cloudManufacturerName" label="云厂商" align="center" />
        <el-table-column prop="cloudProductName" label="云产品名称" align="center" />
        <el-table-column prop="cloudProductType" label="云产品类型" align="center" />
        <el-table-column prop="discount" label="折扣权限" align="center">

          <template slot-scope="scope">
            <el-input-number
              v-model.number="scope.row.discount"
              size="small"
              :precision="2"
              :step="0.1"
              :min="0.1"
              :max="10"
              placeholder="请输入折扣权限 ( 0.1 - 10.00 折 )"
              @change="handleChange"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{ row }">
            <el-button
              :row-key="row.id"
              size="small"
              type="text"
              icon="el-icon-circle-edit"
              @click="save(row)"
            >保存</el-button>
            <el-button
              :row-key="row.id"
              size="small"
              type="text"
              icon="el-icon-circle-edit"
              @click="nosave(row)"
            >取消</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-divider class="divider" content-position="left" />
      <el-button type="primary" size="small" @click="show = !show">+添加更多模板</el-button>

      <el-form
        v-show="show"
        ref="moduleForm"
        :model="moduleForm"
        label-width="100px"
        style="width: 92%"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="云厂商名称" prop="name">
              <el-select
                v-model="moduleForm.cloudManufacturerId"
                placeholder="请选择云厂商名称"
                clearable
                @change="changeSelect"
              >
                <el-option v-for="item in listCloud" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :span="6">
            <el-form-item label="云产品名称" prop="cloudProductName">
              <el-select
                v-model="moduleForm.cloudProductName"
                placeholder="请选择云产品名称"
                clearable
              >
                <!-- <el-option label="区域一" value="shanghai" /> -->
                <el-option v-for="item in ListCloudProducts" :key="item.id" :label="item.name" :value="item.name" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="云产品类型" prop="cloudProductType">
              <el-select
                v-model="moduleForm.cloudProductType"
                placeholder="请选择云产品类型"
                clearable
              >
                <el-option v-for="item in ListCloudProducts" :key="item.id" :label="item.type" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="-2">
            <el-form-item label-width="20px">
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="onSearch"
              >查询</el-button>
              <el-button
                icon="el-icon-refresh"
                @click.native="clearFrom"
              >重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table
        v-show="show"
        ref="discountModuleList"
        v-loading="loading"
        border-bottom
        style="width: 100%; margin-top: 20px"
        class="table"
        :header-cell-style="{
          background: '#FAFAFA'
        }"
        :data="discountModuleList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" :selectable="checkSelectable" />
        <el-table-column type="index" :index="indexMethod" align="center" width="55" label="序号" />
        <el-table-column prop="cloudManufacturerName" label="云厂商" align="center" />
        <el-table-column prop="name" label="云产品名称" align="center" />
        <el-table-column prop="type" label="云产品类型" align="center" />
        <el-table-column prop="discount" label="折扣权限" align="center">

          <template slot-scope="scope">
            <el-input-number
              v-model.number="scope.row.discount"
              size="small"
              :precision="2"
              :step="0.1"
              :min="0.1"
              :max="10"
              placeholder="请输入折扣权限 ( 0.1 - 10.00 折 )"
              @change="handleChange"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{ row }">
            <el-button
              :row-key="row.id"
              size="small"
              type="text"
              icon="el-icon-circle-edit"
              @click="edit(row)"
            >保存</el-button>
            <el-button
              :row-key="row.id"
              size="small"
              type="text"
              icon="el-icon-circle-edit"
              @click="del(row)"
            >取消</el-button>
          </template>
        </el-table-column>
      </el-table>

      <template v-slot:footer>
        <el-button
          style="margin-top: 12px"
          type="primary"
          @click="onSubmit"
        >确定</el-button>
        <el-button @click="handleCloseDialog">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// 获取云厂商列表 // 获取云厂商名称 // 获取云产品类型和名称 // 新增折扣模块// 编辑回显 //编辑确认
import { reqGetListPageCloudProducts, reqGetListCloudManufacturers, reqGetListCloudProducts,
  reqGetDiscountTemplateInfo,
  reqEditDiscountTemplate } from '@/api/discount'
export default {
  name: 'CloudControlTemplateEdit',
  props: {
    editTemplateShow: {
      type: Boolean,
      required: true
    },
    editId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      discountForm: {
        planName: '' // 折扣模板名称
      },
      discountFormRules: {
        planName: [{ required: true, message: '请输入折扣模板名称', trigger: 'blur' }]
      },
      moduleForm: {
        cloudManufacturerName: '', // 云厂商
        cloudProductName: '', // 云产品名称
        cloudProductType: '' // 云产品类型
      },
      pageSize: 1000,
      page: 1,
      total: 20,
      discountModuleList: [], // table数据
      multipleSelection: [],
      listCloud: [],
      ListCloudProducts: [],

      editList: [], // 编辑table数据
      Selection: [],
      show: false
    }
  },

  mounted() {

  },

  methods: {
    // 开启弹层
    openDialog() {
      this.getListPageCloudProducts()
      this.getListCloudManufacturers()
      this.getListCloudProducts()
    },
    // 查询
    onSearch() {
      this.getListPageCloudProducts()
      // console.log(this.moduleForm)
    },
    // 重置
    clearFrom() {
      this.moduleForm = {
        cloudManufacturerName: '', // 云厂商
        cloudProductName: '', // 云产品名称
        cloudProductType: '' // 云产品类型
      }
      this.getListPageCloudProducts()
    },
    // 编辑数据
    async reqGetDiscountTemplateInfo() {
      const { data: res } = await reqGetDiscountTemplateInfo(this.editId)
      console.log(res)
      this.editList = res.productList
      this.discountForm.planName = res.planName
      this.$refs.editList.toggleAllSelection()
    },
    // 编辑保存
    save(row) {
      this.$refs.editList.toggleRowSelection(row, true)
      this.$message.success('保存折扣权限成功')
    },
    // 取消
    nosave(row) {
      this.$refs.editList.toggleRowSelection(row, false)
      this.$message.success('已取消')
    },
    // 选中
    SelectionChange(val) {
      console.log(val)
      this.Selection = val
    },
    // 保存
    edit(row) {
      // console.log(row)
      // console.log(row.discount)
      if (!row.discount) return this.$message.warning('请输入折扣权限')
      // this.tempObj = row
      this.$refs.discountModuleList.toggleRowSelection(row, true)
      this.$message.success('保存折扣权限成功')
    },
    // 取消
    del(row) {
      // this.$refs.discountModuleList.clearSelection()
      this.$refs.discountModuleList.toggleRowSelection(row, false)
      this.$message.success('已取消')
    },
    // 数量框变化
    handleChange(value) {
      console.log(value)
    },
    // 获取 云厂商列表
    async getListPageCloudProducts() {
      this.loading = true
      const { data: res } = await reqGetListPageCloudProducts(this.page, this.pageSize, {
        ...this.moduleForm
      })
      this.discountModuleList = res.records
      this.total = res.total
      this.loading = false

      //   console.log(this.discountModuleList.filter(item => item.id.inclouds(this.editList.map(item => item.productId))))
    },
    // 获取云厂商名称
    async getListCloudManufacturers() {
      const res = await reqGetListCloudManufacturers()
      this.listCloud = res.data
      // console.log(res)
    },
    changeSelect(val) {
      this.moduleForm.cloudProductName = ''
      this.moduleForm.cloudProductType = ''
      this.getListCloudProducts()
    },
    // 获取云产品名称和 类型
    async getListCloudProducts() {
      const res = await reqGetListCloudProducts()
      this.ListCloudProducts = res.data.filter(item => item.cloudManufacturerId === this.moduleForm.cloudManufacturerId)
      // console.log(this.ListCloudProducts)
    },
    handleSizeChange(val) {
      this.page = val
      this.getListPageCloudProducts()
    },

    handleCurrentChange(val) {
      this.pageSize = val
      this.page = 1
      this.getListPageCloudProducts()
    },

    // reqEditDiscountTemplate

    // 确认
    async onSubmit() {
      this.$refs.discountForm.validate(async(valid) => {
        if (!valid) return
        if (this.multipleSelection.length <= 0 && this.Selection.length <= 0) return this.$message.error('请勾选折扣权限')
        const productListOwn = this.Selection.map((item, index) => {
          return Object.assign({}, { 'productId': item.productId, 'discount': item.discount })
        })
        const productListChoose = this.multipleSelection.map((item, index) => {
          return Object.assign({}, { 'productId': item.id, 'discount': item.discount })
        })
        const productList = productListOwn.concat(productListChoose)
        // console.log(productList)
        const Obj = {
          planName: this.discountForm.planName, // 折扣模板名称
          // type: 0, // 预留字段先传0或1,
          id: this.editId,
          productList
          // [{
          //   'productId': 0, // 从梁久庆那边查ID
          //   'discount': 10
          // }]
        }
        const res = await reqEditDiscountTemplate(Obj)
        // console.log(res)
        if (res.code === 200) this.$message.success('操作成功')
        this.handleCloseDialog()
        this.$emit('getlistTemplate')
      })
    },
    // 关闭弹层前
    async beforeClose(done) {
      const confirmResult = await this.$confirm(
        '内容还未保存,此操作会导致信息丢失!是否继续?',
        '温馨提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)

      if (confirmResult === 'cancel') return
      done()
    },
    // 取消
    handleCloseDialog() {
      this.$emit('update:editTemplateShow', false)
      this.show = false
      this.discountForm = {
        planName: '' // 折扣模板名称
      }
      this.moduleForm = {
        cloudManufacturerName: '', // 云厂商
        name: '', // 云产品名称
        type: '' // 云产品类型
      }
      this.$refs.discountForm.resetFields()
      this.$refs.moduleForm.resetFields()
    },
    // 选中当前行
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    indexMethod(index) {
      return index + 1 + (this.page - 1) * this.pageSize
    },

    checkSelectable(row, index) {
      // console.log(row, index)
      return row.discount >= 0 && row.discount <= 10
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__body{
  padding: 0px 20px !important;
}
.divider{
  margin-top: -2px;
}
</style>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/114315?site
推荐阅读
相关标签
  

闽ICP备14008679号