Merge pull request '2805-fix(entry_latestBuys): save chekeds when crudModel load more data' (#1045) from 2805-entry_latestBuy_saveChekeds into dev

Reviewed-on: #1045
Reviewed-by: Joan Sanchez <joan@verdnatura.es>
This commit is contained in:
Joan Sanchez 2022-09-21 06:47:29 +00:00
commit fcce73086e
3 changed files with 69 additions and 9 deletions

View File

@ -4,6 +4,7 @@
order="itemFk DESC" order="itemFk DESC"
limit="20" limit="20"
data="$ctrl.buys" data="$ctrl.buys"
on-data-change="$ctrl.reCheck()"
auto-load="true"> auto-load="true">
</vn-crud-model> </vn-crud-model>
<vn-portal slot="topbar"> <vn-portal slot="topbar">
@ -21,7 +22,7 @@
<smart-table <smart-table
model="model" model="model"
view-config-id="latestBuys" view-config-id="latestBuys"
options="$ctrl.smartTableOptions" options="$ctrl.smartTableOptions"
expr-builder="$ctrl.exprBuilder(param, value)"> expr-builder="$ctrl.exprBuilder(param, value)">
<slot-table> <slot-table>
<table> <table>
@ -123,17 +124,18 @@
<tbody> <tbody>
<tr ng-repeat="buy in $ctrl.buys" <tr ng-repeat="buy in $ctrl.buys"
vn-anchor="::{ vn-anchor="::{
state: 'entry.card.buy.index', state: 'entry.card.buy.index',
params: {id: {{::buy.entryFk}}} params: {id: {{::buy.entryFk}}}
}"> }">
<td> <td>
<vn-check <vn-check
ng-model="buy.checked" ng-model="buy.checked"
on-change="$ctrl.saveChecked(buy.id)"
vn-click-stop> vn-click-stop>
</vn-check> </vn-check>
</td> </td>
<td > <td >
<img <img
ng-src="{{::$root.imagePath('catalog', '50x50', buy.itemFk)}}" ng-src="{{::$root.imagePath('catalog', '50x50', buy.itemFk)}}"
zoom-image="{{::$root.imagePath('catalog', '1600x900', buy.itemFk)}}" zoom-image="{{::$root.imagePath('catalog', '1600x900', buy.itemFk)}}"
vn-click-stop vn-click-stop
@ -147,12 +149,12 @@
</span> </span>
</td> </td>
<td number> <td number>
<vn-chip class="transparent" translate-attr="buy.groupingMode == 2 ? {title: 'Minimun amount'} : {title: 'Packing'}" ng-class="{'message': buy.groupingMode == 2}"> <vn-chip class="transparent" translate-attr="buy.groupingMode == 2 ? {title: 'Minimun amount'} : {title: 'Packing'}" ng-class="{'message': buy.groupingMode == 2}">
<span>{{::buy.packing | dashIfEmpty}}</span> <span>{{::buy.packing | dashIfEmpty}}</span>
</vn-chip> </vn-chip>
</td> </td>
<td number> <td number>
<vn-chip class="transparent" translate-attr="buy.groupingMode == 1 ? {title: 'Minimun amount'} : {title: 'Grouping'}" ng-class="{'message': buy.groupingMode == 1}"> <vn-chip class="transparent" translate-attr="buy.groupingMode == 1 ? {title: 'Minimun amount'} : {title: 'Grouping'}" ng-class="{'message': buy.groupingMode == 1}">
<span>{{::buy.grouping | dashIfEmpty}}</span> <span>{{::buy.grouping | dashIfEmpty}}</span>
</vn-chip> </vn-chip>
</td> </td>
@ -186,7 +188,7 @@
<vn-check <vn-check
disabled="true" disabled="true"
ng-model="::buy.isActive"> ng-model="::buy.isActive">
</vn-check> </vn-check>
</td> </td>
<td>{{::buy.family}}</td> <td>{{::buy.family}}</td>
<td> <td>
@ -204,7 +206,7 @@
<vn-check <vn-check
disabled="true" disabled="true"
ng-model="::buy.isIgnored"> ng-model="::buy.isIgnored">
</vn-check> </vn-check>
</td> </td>
<td number>{{::buy.price2 | currency: 'EUR':2}}</td> <td number>{{::buy.price2 | currency: 'EUR':2}}</td>
<td number>{{::buy.price3 | currency: 'EUR':2}}</td> <td number>{{::buy.price3 | currency: 'EUR':2}}</td>
@ -231,7 +233,7 @@
</vn-button> </vn-button>
</vn-vertical> </vn-vertical>
</div> </div>
<vn-dialog class="edit" <vn-dialog class="edit"
vn-id="edit" vn-id="edit"
on-accept="$ctrl.onEditAccept()" on-accept="$ctrl.onEditAccept()"
on-close="$ctrl.editedColumn = null"> on-close="$ctrl.editedColumn = null">

View File

@ -7,6 +7,7 @@ export default class Controller extends Section {
super($element, $); super($element, $);
this.editedColumn; this.editedColumn;
this.checkAll = false; this.checkAll = false;
this.checkedBuys = [];
this.smartTableOptions = { this.smartTableOptions = {
activeButtons: { activeButtons: {
@ -139,6 +140,7 @@ export default class Controller extends Section {
uncheck() { uncheck() {
this.checkAll = false; this.checkAll = false;
this.checkedBuys = [];
} }
get totalChecked() { get totalChecked() {
@ -148,6 +150,23 @@ export default class Controller extends Section {
return this.checked.length; return this.checked.length;
} }
saveChecked(buyId) {
const index = this.checkedBuys.indexOf(buyId);
if (index !== -1)
return this.checkedBuys.splice(index, 1);
return this.checkedBuys.push(buyId);
}
reCheck() {
if (!this.$.model.data) return;
if (!this.checkedBuys.length) return;
this.$.model.data.forEach(buy => {
if (this.checkedBuys.includes(buy.id))
buy.checked = true;
});
}
onEditAccept() { onEditAccept() {
const rowsToEdit = []; const rowsToEdit = [];
for (let row of this.checked) for (let row of this.checked)

View File

@ -57,5 +57,44 @@ describe('Entry', () => {
expect(result.length).toEqual(0); expect(result.length).toEqual(0);
}); });
}); });
describe('reCheck()', () => {
it(`should recheck buys`, () => {
controller.$.model.data = [
{checked: false, id: 1},
{checked: false, id: 2},
{checked: false, id: 3},
{checked: false, id: 4},
];
controller.checkedBuys = [1, 2];
controller.reCheck();
expect(controller.$.model.data[0].checked).toEqual(true);
expect(controller.$.model.data[1].checked).toEqual(true);
expect(controller.$.model.data[2].checked).toEqual(false);
expect(controller.$.model.data[3].checked).toEqual(false);
});
});
describe('saveChecked()', () => {
it(`should check buy`, () => {
const buyCheck = 3;
controller.checkedBuys = [1, 2];
controller.saveChecked(buyCheck);
expect(controller.checkedBuys[2]).toEqual(buyCheck);
});
it(`should uncheck buy`, () => {
const buyUncheck = 3;
controller.checkedBuys = [1, 2, 3];
controller.saveChecked(buyUncheck);
expect(controller.checkedBuys[2]).toEqual(undefined);
});
});
}); });
}); });