Skip to content

Commit 5926ede

Browse files
committed
Support previous iteration
1 parent 5f839b3 commit 5926ede

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

resources/js/laravel-livewire-tables.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -250,33 +250,44 @@ document.addEventListener('alpine:init', () => {
250250

251251
}
252252
}));
253-
254253

255-
Alpine.data('booleanFilter', (filterKey,tableName,defaultValue) => ({
254+
Alpine.data('booleanFilter', (wire,filterKey,tableName,defaultValue) => ({
255+
switchOn: false,
256+
value: wire.entangle('filterComponents.'+filterKey).live,
257+
init() {
258+
this.switchOn = false;
259+
if (typeof this.value !== 'undefined') {
260+
this.switchOn = Boolean(Number(this.value));
261+
}
262+
this.listeners.push(
263+
Livewire.on('filter-was-set', (detail) => {
264+
if(detail.tableName == tableName && detail.filterKey == filterKey) {
265+
this.switchOn = detail.value ?? defaultValue;
266+
}
267+
})
268+
);
269+
}
270+
}));
271+
272+
Alpine.data('newBooleanFilter', (filterKey,tableName,defaultValue) => ({
256273
switchOn: false,
257274
value: false,
258275
toggleStatus()
259276
{
260277
let tempValue = Boolean(Number(this.$wire.get('filterComponents.'+filterKey) ?? this.value));
261278
let newBoolean = !tempValue;
262-
let newValue = Number(newBoolean);
263-
this.value = newBoolean;
264-
this.switchOn = newBoolean;
265-
this.updateStatus(newValue);
279+
this.switchOn = this.value = newBoolean;
280+
return Number(newBoolean);
266281
},
267-
toggleStatusWithReset()
282+
toggleStatusWithUpdate()
268283
{
269-
let tempValue = Boolean(Number(this.$wire.get('filterComponents.'+filterKey) ?? this.value));
270-
let newBoolean = !tempValue;
271-
let newValue = Number(newBoolean);
272-
this.value = newBoolean;
273-
this.switchOn = newBoolean;
274-
this.$wire.call('resetFilter',filterKey);
275-
284+
let newValue = this.toggleStatus();
285+
this.$wire.set('filterComponents.'+filterKey, newValue);
276286
},
277-
updateStatus(newValue)
287+
toggleStatusWithReset()
278288
{
279-
this.$wire.set('filterComponents.'+filterKey, newValue);
289+
let newValue = this.toggleStatus();
290+
this.$wire.call('resetFilter',filterKey);
280291
},
281292
setSwitchOn(val)
282293
{
@@ -286,8 +297,7 @@ document.addEventListener('alpine:init', () => {
286297
init() {
287298
this.$nextTick(() => {
288299
this.value = this.$wire.get('filterComponents.'+filterKey) ?? defaultValue;
289-
let number = Number(this.value ?? 0);
290-
this.switchOn = Boolean(number);
300+
this.setSwitchOn(this.value ?? 0);
291301
});
292302

293303
this.listeners.push(

0 commit comments

Comments
 (0)