table-search.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <template>
  2. <el-input v-model="tableSearch.filter" clearable placeholder="搜索当前表格" @keyup="searchInTable" @clear="searchInTable">
  3. <template #prefix><sc-iconify icon="ep:search"></sc-iconify></template>
  4. </el-input>
  5. </template>
  6. <script setup>
  7. import XEUtils from "xe-utils";
  8. const props = defineProps({
  9. renderOpts: { type: Object, default: () => {} },
  10. params: { type: Object, default: () => {} }
  11. })
  12. const tableSearch = ref({
  13. filter: ""
  14. })
  15. const searchInTable = () => {
  16. const filterName = XEUtils.toValueString(tableSearch.value.filter).trim().toLowerCase();
  17. const tableData = XEUtils.clone(props.params.$grid?.getData(), true);
  18. if (filterName) {
  19. const filterRE = new RegExp(filterName.replace(/([.?*+^$[\]\\(){}|-])/gi, "\\$1"), "gi");
  20. const searchColumns = props.params.$grid?.getColumns()?.filter(col => col.field);
  21. const data = tableData?.map(item => {
  22. searchColumns?.forEach(col => XEUtils.set(
  23. item,
  24. col.field,
  25. props.params.$grid?.getCellLabel(item, col.field)
  26. ));
  27. return item;
  28. })?.filter(item => searchColumns?.some(col => XEUtils.toValueString(XEUtils.get(item, col.field)).toLowerCase().includes(filterName)))?.map(row => {
  29. const item = Object.assign({}, row);
  30. searchColumns?.filter(col => col.type === "html").forEach(col => {
  31. XEUtils.set(
  32. item,
  33. col.field,
  34. XEUtils.toValueString(XEUtils.get(item, col.field)).replace(filterRE, match => `<span class="keyword-lighten">${match}</span>`)
  35. )
  36. });
  37. return item;
  38. })
  39. props.params.$grid?.reloadData(data);
  40. } else props.params.$grid?.reloadData(tableData);
  41. }
  42. </script>
  43. <style scoped>
  44. .el-input {width: 180px;}
  45. </style>