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">
@ -129,6 +130,7 @@
<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>

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);
});
});
}); });
}); });