Skip to content

Commit 55f112c

Browse files
authored
Merge pull request #20 from vue-pivottable/feat/#19-calc-attrValues
Feat/#19 calc attr values
2 parents b7ca1f7 + 02989b1 commit 55f112c

File tree

3 files changed

+85
-47
lines changed

3 files changed

+85
-47
lines changed

src/components/pivottable-ui/VPivottableUi.vue

+18-47
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
:aggregatorName="state.aggregatorName"
3333
:rowOrder="state.rowOrder"
3434
:colOrder="state.colOrder"
35-
:attributeNames="state.attributeNames"
35+
:attributeNames="attributeNames"
3636
:hiddenFromAggregators="state.hiddenFromAggregators"
3737
:vals="state.vals"
3838
@update:aggregatorName="onUpdateAggregatorName"
@@ -107,7 +107,7 @@ import VAggregatorCell from './VAggregatorCell.vue'
107107
import VDragAndDropCell from './VDragAndDropCell.vue'
108108
import { VPivottable } from '@/'
109109
import { computed, ref, toRefs, watch } from 'vue'
110-
import { usePropsState } from '@/composables'
110+
import { usePropsState, useMaterializeInput } from '@/composables'
111111
import TableRenderer from '../pivottable/renderer/index'
112112
113113
const props = defineProps({
@@ -145,6 +145,7 @@ const props = defineProps({
145145
default: false
146146
}
147147
})
148+
// TODO
148149
const pivotUiState = ref({
149150
unusedOrder: props.unusedAttrs,
150151
zIndices: {},
@@ -156,7 +157,14 @@ const pivotUiState = ref({
156157
})
157158
const propsRefs = toRefs(props)
158159
159-
const { state, updateState, updateMultiple } = usePropsState(propsRefs)
160+
const { state, updateState } = usePropsState(propsRefs)
161+
const { allFilters } = useMaterializeInput(
162+
computed(() => props.data),
163+
{
164+
derivedAttributes: computed(() => props.derivedAttributes)
165+
}
166+
)
167+
160168
const rendererItems = computed(() => Object.keys(state.value.renderers).length ? state.value.renderers : TableRenderer)
161169
const aggregatorItems = computed(() => state.value.aggregators)
162170
const rowAttrs = computed(() => {
@@ -173,8 +181,14 @@ const colAttrs = computed(() => {
173181
!state.value.hiddenFromDragDrop.includes(e)
174182
)
175183
})
184+
const attributeNames = computed(() => {
185+
return Object.keys(allFilters.value).filter(e =>
186+
!state.value.hiddenAttributes.includes(e) &&
187+
!state.value.hiddenFromAggregators.includes(e)
188+
)
189+
})
176190
const unusedAttrs = computed(() => {
177-
return state.value.attributes
191+
return attributeNames.value
178192
.filter(
179193
e =>
180194
!state.value.rows.includes(e) &&
@@ -185,48 +199,6 @@ const unusedAttrs = computed(() => {
185199
.sort(sortAs(state.value.unusedOrder))
186200
})
187201
188-
const materializeInput = nextData => {
189-
if (props.data === nextData) {
190-
return
191-
}
192-
const newState = {
193-
data: nextData,
194-
attrValues: {},
195-
materializedInput: []
196-
}
197-
198-
let recordsProcessed = 0
199-
PivotData.forEachRecord(
200-
newState.data,
201-
props.derivedAttributes,
202-
function (record) {
203-
newState.materializedInput.push(record)
204-
for (const attr of Object.keys(record)) {
205-
if (!(attr in newState.attrValues)) {
206-
newState.attrValues[attr] = {}
207-
if (recordsProcessed > 0) {
208-
newState.attrValues[attr].null = recordsProcessed
209-
}
210-
}
211-
}
212-
for (const attr in newState.attrValues) {
213-
const value = attr in record ? record[attr] : 'null'
214-
if (!(value in newState.attrValues[attr])) {
215-
newState.attrValues[attr][value] = 0
216-
}
217-
newState.attrValues[attr][value]++
218-
}
219-
recordsProcessed++
220-
}
221-
)
222-
223-
updateMultiple({
224-
...state.value,
225-
...newState,
226-
...pivotUiState.value
227-
})
228-
}
229-
230202
const onMoveFilterBoxToTop = ({ attribute }) => {
231203
updateState('maxZIndex', state.value.maxZIndex++)
232204
updateState('zIndices', {
@@ -268,7 +240,6 @@ const pivotData = computed(() => new PivotData(state.value))
268240
269241
watch(() => props.data, value => {
270242
updateState('unusedOrder', props.unusedAttrs)
271-
materializeInput(value)
272243
})
273244
</script>
274245

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 { useMaterializeInput } from './useMaterializeInput'
+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 useMaterializeInput (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)