Skip to content

Commit 40c5811

Browse files
committed
feat: update attrValues #19
1 parent c4c7714 commit 40c5811

File tree

3 files changed

+83
-46
lines changed

3 files changed

+83
-46
lines changed

src/components/pivottable-ui/VPivottableUi.vue

+16-46
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ import VAggregatorCell from './VAggregatorCell.vue'
104104
import VDragAndDropCell from './VDragAndDropCell.vue'
105105
import { VPivottable } from '@/'
106106
import { computed, ref, toRefs, watch } from 'vue'
107-
import { usePropsState } from '@/composables'
107+
import { usePropsState, usePivotDataProcessing } from '@/composables'
108108
import TableRenderer from '../pivottable/renderer/index'
109109
110110
const props = defineProps({
@@ -153,7 +153,14 @@ const pivotUiState = ref({
153153
})
154154
const propsRefs = toRefs(props)
155155
156-
const { state, updateState, updateMultiple } = usePropsState(propsRefs)
156+
const { state, updateState } = usePropsState(propsRefs)
157+
const { allFilters } = usePivotDataProcessing(
158+
computed(() => props.data),
159+
{
160+
derivedAttributes: computed(() => props.derivedAttributes)
161+
}
162+
)
163+
157164
const rendererItems = computed(() => Object.keys(state.value.renderers).length ? state.value.renderers : TableRenderer)
158165
const aggregatorItems = computed(() => state.value.aggregators)
159166
const rowAttrs = computed(() => {
@@ -170,8 +177,14 @@ const colAttrs = computed(() => {
170177
!state.value.hiddenFromDragDrop.includes(e)
171178
)
172179
})
180+
const attributeNames = computed(() => {
181+
return Object.keys(allFilters.value).filter(e =>
182+
!state.value.hiddenAttributes.includes(e) &&
183+
!state.value.hiddenFromAggregators.includes(e)
184+
)
185+
})
173186
const unusedAttrs = computed(() => {
174-
return state.value.attributes
187+
return attributeNames.value
175188
.filter(
176189
e =>
177190
!state.value.rows.includes(e) &&
@@ -182,48 +195,6 @@ const unusedAttrs = computed(() => {
182195
.sort(sortAs(state.value.unusedOrder))
183196
})
184197
185-
const materializeInput = nextData => {
186-
if (props.data === nextData) {
187-
return
188-
}
189-
const newState = {
190-
data: nextData,
191-
attrValues: {},
192-
materializedInput: []
193-
}
194-
195-
let recordsProcessed = 0
196-
PivotData.forEachRecord(
197-
newState.data,
198-
props.derivedAttributes,
199-
function (record) {
200-
newState.materializedInput.push(record)
201-
for (const attr of Object.keys(record)) {
202-
if (!(attr in newState.attrValues)) {
203-
newState.attrValues[attr] = {}
204-
if (recordsProcessed > 0) {
205-
newState.attrValues[attr].null = recordsProcessed
206-
}
207-
}
208-
}
209-
for (const attr in newState.attrValues) {
210-
const value = attr in record ? record[attr] : 'null'
211-
if (!(value in newState.attrValues[attr])) {
212-
newState.attrValues[attr][value] = 0
213-
}
214-
newState.attrValues[attr][value]++
215-
}
216-
recordsProcessed++
217-
}
218-
)
219-
220-
updateMultiple({
221-
...state.value,
222-
...newState,
223-
...pivotUiState.value
224-
})
225-
}
226-
227198
const onMoveFilterBoxToTop = ({ attribute }) => {
228199
updateState('maxZIndex', state.value.maxZIndex++)
229200
updateState('zIndices', {
@@ -256,7 +227,6 @@ const pivotData = computed(() => new PivotData(state.value))
256227
257228
watch(() => props.data, value => {
258229
updateState('unusedOrder', props.unusedAttrs)
259-
materializeInput(value)
260230
})
261231
</script>
262232

src/composables/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { usePivotData, providePivotData } from './pivotData'
22
export { usePropsState } from './usePropsState'
3+
export { usePivotDataProcessing } from './usePivotDataProcessing'
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { ref, watch } from 'vue'
2+
import { PivotData } from '@/helper/utilities.js'
3+
4+
export function usePivotDataProcessing (dataSource, options) {
5+
const rawData = ref(null)
6+
const allFilters = ref({})
7+
const materializedInput = ref([])
8+
9+
function processData (data) {
10+
if (!data || rawData.value === data) return
11+
12+
rawData.value = data
13+
const newAllFilters = {}
14+
const newMaterializedInput = []
15+
16+
let recordsProcessed = 0
17+
18+
PivotData.forEachRecord(
19+
data,
20+
options.derivedAttributes.value,
21+
function (record) {
22+
newMaterializedInput.push(record)
23+
24+
for (const attr of Object.keys(record)) {
25+
if (!(attr in newAllFilters)) {
26+
newAllFilters[attr] = {}
27+
if (recordsProcessed > 0) {
28+
newAllFilters[attr].null = recordsProcessed
29+
}
30+
}
31+
}
32+
33+
for (const attr in newAllFilters) {
34+
const value = attr in record ? record[attr] : 'null'
35+
if (!(value in newAllFilters[attr])) {
36+
newAllFilters[attr][value] = 0
37+
}
38+
newAllFilters[attr][value]++
39+
}
40+
41+
recordsProcessed++
42+
}
43+
)
44+
45+
allFilters.value = newAllFilters
46+
materializedInput.value = newMaterializedInput
47+
48+
return {
49+
AllFilters: newAllFilters,
50+
materializedInput: newMaterializedInput
51+
}
52+
}
53+
54+
watch(() => dataSource.value, processData, { immediate: true })
55+
56+
watch(() => options.derivedAttributes.value, () => {
57+
processData(dataSource.value)
58+
})
59+
60+
return {
61+
rawData,
62+
allFilters,
63+
materializedInput,
64+
processData
65+
}
66+
}

0 commit comments

Comments
 (0)