perf(table): 新增 v-model:selectedKeys 选中项
This commit is contained in:
parent
e5f53064a3
commit
c559a2d74a
@ -107,8 +107,6 @@ export default {
|
|||||||
|
|
||||||
:::
|
:::
|
||||||
|
|
||||||
<lay-button></lay-button>
|
|
||||||
|
|
||||||
::: field page attributes
|
::: field page attributes
|
||||||
|
|
||||||
:::
|
:::
|
||||||
|
@ -127,7 +127,7 @@ export default {
|
|||||||
|
|
||||||
<template>
|
<template>
|
||||||
{{selectedKeys}}
|
{{selectedKeys}}
|
||||||
<lay-table :columns="columns" id="id" :dataSource="dataSource" default-toolbar="true" :selectedKeys="selectedKeys" @changeSelectedKeys="changeSelectedKeys" checkbox="true">
|
<lay-table :columns="columns" id="id" :dataSource="dataSource" default-toolbar="true" v-model:selectedKeys="selectedKeys" @changeSelectedKeys="changeSelectedKeys" checkbox="true">
|
||||||
<template v-slot:toolbar>
|
<template v-slot:toolbar>
|
||||||
<lay-button>新增</lay-button>
|
<lay-button>新增</lay-button>
|
||||||
<lay-button>删除</lay-button>
|
<lay-button>删除</lay-button>
|
||||||
@ -149,10 +149,6 @@ export default {
|
|||||||
|
|
||||||
const selectedKeys = ref(['1'])
|
const selectedKeys = ref(['1'])
|
||||||
|
|
||||||
const changeSelectedKeys = function(val) {
|
|
||||||
selectedKeys.value = val
|
|
||||||
}
|
|
||||||
|
|
||||||
const columns = [
|
const columns = [
|
||||||
{
|
{
|
||||||
title:"账户",
|
title:"账户",
|
||||||
@ -184,8 +180,7 @@ export default {
|
|||||||
return {
|
return {
|
||||||
columns,
|
columns,
|
||||||
dataSource,
|
dataSource,
|
||||||
selectedKeys,
|
selectedKeys
|
||||||
changeSelectedKeys
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -203,5 +198,5 @@ export default {
|
|||||||
| dataSource | 数据源 | -- |
|
| dataSource | 数据源 | -- |
|
||||||
| checkbox | 开启复现框 | -- |
|
| checkbox | 开启复现框 | -- |
|
||||||
| id | 主键 | -- |
|
| id | 主键 | -- |
|
||||||
| selectKeys | 选中项 | -- |
|
| selectedKeys ( v-model ) | 选中项 | -- |
|
||||||
| default-toolbar | 开启工具栏 | -- |
|
| default-toolbar | 开启工具栏 | -- |
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup name="LayCheckbox" lang="ts">
|
<script setup name="LayCheckbox" lang="ts">
|
||||||
import { defineProps, ref } from 'vue'
|
import { defineProps } from 'vue'
|
||||||
|
|
||||||
const props =
|
const props =
|
||||||
defineProps<{
|
defineProps<{
|
||||||
@ -32,7 +32,7 @@ const emit = defineEmits(['update:checked', 'change'])
|
|||||||
const handleClick = function () {
|
const handleClick = function () {
|
||||||
if (!props.disabled) {
|
if (!props.disabled) {
|
||||||
emit('update:checked', !props.checked)
|
emit('update:checked', !props.checked)
|
||||||
emit('change', !props.checked)
|
emit('change', { checked: !props.checked, value: props.label })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
49
src/module/table/component/checkbox.vue
Normal file
49
src/module/table/component/checkbox.vue
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<template>
|
||||||
|
<span @click="handleClick">
|
||||||
|
<input type="checkbox" :name="name" :value="label" />
|
||||||
|
<div
|
||||||
|
class="layui-unselect"
|
||||||
|
:class="[
|
||||||
|
{
|
||||||
|
'layui-checkbox-disbaled layui-disabled': disabled,
|
||||||
|
},
|
||||||
|
'layui-form-checkbox',
|
||||||
|
props.modelValue.includes(props.label) ? 'layui-form-checked' : '',
|
||||||
|
]"
|
||||||
|
:lay-skin="skin"
|
||||||
|
>
|
||||||
|
<span><slot /></span>
|
||||||
|
<i v-if="skin == 'primary'" class="layui-icon layui-icon-ok" />
|
||||||
|
<i v-if="!skin" class="layui-icon layui-icon-ok" />
|
||||||
|
</div>
|
||||||
|
</span>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup name="LayCheckbox" lang="ts">
|
||||||
|
import { defineProps, ref } from 'vue'
|
||||||
|
|
||||||
|
const props =
|
||||||
|
defineProps<{
|
||||||
|
modelValue: string[]
|
||||||
|
label: string
|
||||||
|
disabled?: boolean
|
||||||
|
name?: string
|
||||||
|
skin?: string
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const hasValue = ref(false)
|
||||||
|
|
||||||
|
const emit = defineEmits(['update:modelValue'])
|
||||||
|
|
||||||
|
const handleClick = function () {
|
||||||
|
if (!props.disabled) {
|
||||||
|
if (!props.modelValue.includes(props.label)) {
|
||||||
|
props.modelValue.push(props.label)
|
||||||
|
} else {
|
||||||
|
let index = props.modelValue.indexOf(props.label)
|
||||||
|
props.modelValue.splice(index, 1)
|
||||||
|
}
|
||||||
|
emit('update:modelValue', props.modelValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
@ -30,9 +30,10 @@
|
|||||||
<th v-if="checkbox" class="layui-table-col-special">
|
<th v-if="checkbox" class="layui-table-col-special">
|
||||||
<div class="layui-table-cell laytable-cell-checkbox">
|
<div class="layui-table-cell laytable-cell-checkbox">
|
||||||
<lay-checkbox
|
<lay-checkbox
|
||||||
v-model="allSelectedKeys"
|
v-model:checked="allChecked"
|
||||||
skin="primary"
|
skin="primary"
|
||||||
label="all"
|
label="all"
|
||||||
|
@change="changeAll"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</th>
|
</th>
|
||||||
@ -55,14 +56,14 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<td v-if="checkbox" class="layui-table-col-special">
|
<td v-if="checkbox" class="layui-table-col-special">
|
||||||
<div class="layui-table-cell laytable-cell-checkbox">
|
<div class="layui-table-cell laytable-cell-checkbox">
|
||||||
<lay-checkbox
|
<table-item-checkbox
|
||||||
v-model="tableSelectedKeys"
|
|
||||||
skin="primary"
|
skin="primary"
|
||||||
:label="data[id]"
|
:label="data[id]"
|
||||||
/>
|
v-model="tableSelectedKeys"
|
||||||
|
>
|
||||||
|
</table-item-checkbox>
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
<template v-for="column in columns" :key="column">
|
<template v-for="column in columns" :key="column">
|
||||||
<template v-if="column.customSlot">
|
<template v-if="column.customSlot">
|
||||||
<td>
|
<td>
|
||||||
@ -112,7 +113,9 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<script setup name="LayTable" lang="ts">
|
<script setup name="LayTable"></script>
|
||||||
|
<script setup lang="ts">
|
||||||
|
import tableItemCheckbox from './component/checkbox.vue'
|
||||||
import {
|
import {
|
||||||
defineProps,
|
defineProps,
|
||||||
ref,
|
ref,
|
||||||
@ -120,7 +123,6 @@ import {
|
|||||||
watch,
|
watch,
|
||||||
withDefaults,
|
withDefaults,
|
||||||
defineEmits,
|
defineEmits,
|
||||||
computed,
|
|
||||||
} from 'vue'
|
} from 'vue'
|
||||||
import { Recordable } from '/@src/module/type'
|
import { Recordable } from '/@src/module/type'
|
||||||
|
|
||||||
@ -146,30 +148,40 @@ const props = withDefaults(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
const allSelectedKeys = ref([])
|
const emit = defineEmits(['change', 'update:selectedKeys'])
|
||||||
const tableSelectedKeys = ref(props.selectedKeys)
|
|
||||||
|
|
||||||
watch(
|
const slot = useSlots()
|
||||||
allSelectedKeys,
|
const slots = slot.default && slot.default()
|
||||||
function (val: string[]) {
|
|
||||||
|
const allChecked = ref(false)
|
||||||
|
const tableSelectedKeys = ref([...props.selectedKeys])
|
||||||
|
|
||||||
|
const changeAll = function ({ checked, value }: any) {
|
||||||
const ids = props.dataSource.map((item: any) => {
|
const ids = props.dataSource.map((item: any) => {
|
||||||
return item[props.id]
|
return item[props.id]
|
||||||
})
|
})
|
||||||
tableSelectedKeys.value.splice(0, ids.length)
|
tableSelectedKeys.value.splice(0, ids.length)
|
||||||
if (val.includes('all')) {
|
if (checked) {
|
||||||
ids.forEach((id) => {
|
ids.forEach((id) => {
|
||||||
tableSelectedKeys.value.push(id)
|
tableSelectedKeys.value.push(id)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
emit('update:selectedKeys',tableSelectedKeys.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
watch(
|
||||||
|
tableSelectedKeys,
|
||||||
|
function () {
|
||||||
|
if (tableSelectedKeys.value.length === props.dataSource.length) {
|
||||||
|
allChecked.value = true
|
||||||
|
} else {
|
||||||
|
allChecked.value = false
|
||||||
|
}
|
||||||
|
emit('update:selectedKeys',tableSelectedKeys.value)
|
||||||
},
|
},
|
||||||
{ deep: true }
|
{ deep: true }
|
||||||
)
|
)
|
||||||
|
|
||||||
const emit = defineEmits(['change', 'changeSelectedKeys'])
|
|
||||||
|
|
||||||
const slot = useSlots()
|
|
||||||
const slots = slot.default && slot.default()
|
|
||||||
|
|
||||||
const change = function (page: any) {
|
const change = function (page: any) {
|
||||||
emit('change', page)
|
emit('change', page)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user