@layui/layui-vue
Version:
a component library for Vue 3 base on layui-vue
37 lines (36 loc) • 1.69 kB
JavaScript
import { ref as o, watch as r, reactive as p, computed as S, toRaw as m } from "vue";
import { isValueArray as f, loopForEach as g } from "../../utils/arrayUtil.js";
function K(t, u) {
const n = o(t.selectedKey);
r(() => t.selectedKey, (e) => {
n.value = e;
});
const i = o(!1), h = o(!1), c = p([]), s = S(() => {
const e = [], l = (a) => {
a.forEach((d) => {
e.push(d[t.id]), f(d[t.childrenColumnName]) && l(d[t.childrenColumnName]);
});
};
return l(t.dataSource), e;
});
return r(() => t.selectedKeys, (e) => {
c.splice(0, c.length, ...m(e || []));
}, { immediate: !0, deep: !0 }), r(() => [c, s.value], ([e, l]) => {
var a;
i.value = l.length > 0 && l.every((d) => e.includes(d)), h.value = l.length > 0 && l.some((d) => e.includes(d)), e.length !== ((a = t.selectedKeys) == null ? void 0 : a.length) && u("update:selectedKeys", [...e]);
}, { immediate: !0, deep: !0 }), { tableSelectedKey: n, toggleSelected: function(e) {
n.value = e, u("update:selectedKey", e);
}, allMSelected: i, setMAllSelected: function(e) {
e ? c.push(...s.value.filter((l) => !c.includes(l))) : c.splice(0, c.length, ...c.filter((l) => !s.value.includes(l))), u("checkbox-all", e ? s.value : []);
}, someMSelected: h, tableMSelectedKeys: c, toggleMSelected: function(e, l) {
l ? c.push(e[t.id]) : c.splice(c.indexOf(e[t.id]), 1), u("checkbox", l, m(e));
}, getAllSelectedDataSource: function() {
const e = [.../* @__PURE__ */ new Set([n.value, ...c])], l = [];
return g(t.dataSource, t.childrenColumnName, (a) => {
e.includes(a[t.id]) && l.push({ ...a });
}), l;
} };
}
export {
K as useTableSelected
};