diff --git a/cypress/integration/grid_pagination.js b/cypress/integration/grid_pagination.js index c07230d2b8..84b3320282 100644 --- a/cypress/integration/grid_pagination.js +++ b/cypress/integration/grid_pagination.js @@ -13,7 +13,7 @@ context('Grid Pagination', () => { it('creates pages for child table', () => { cy.visit('/app/contact/Test Contact'); cy.get('.frappe-control[data-fieldname="phone_nos"]').as('table'); - cy.get('@table').find('.current-page-number').should('contain', '1'); + cy.get('@table').find('.current-page-number').should('have.value', '1'); cy.get('@table').find('.total-page-number').should('contain', '20'); cy.get('@table').find('.grid-body .grid-row').should('have.length', 50); }); @@ -21,10 +21,10 @@ context('Grid Pagination', () => { cy.visit('/app/contact/Test Contact'); cy.get('.frappe-control[data-fieldname="phone_nos"]').as('table'); cy.get('@table').find('.next-page').click(); - cy.get('@table').find('.current-page-number').should('contain', '2'); + cy.get('@table').find('.current-page-number').should('have.value', '2'); cy.get('@table').find('.grid-body .grid-row').first().should('have.attr', 'data-idx', '51'); cy.get('@table').find('.prev-page').click(); - cy.get('@table').find('.current-page-number').should('contain', '1'); + cy.get('@table').find('.current-page-number').should('have.value', '1'); cy.get('@table').find('.grid-body .grid-row').first().should('have.attr', 'data-idx', '1'); }); it('adds and deletes rows and changes page', () => { @@ -32,14 +32,35 @@ context('Grid Pagination', () => { cy.get('.frappe-control[data-fieldname="phone_nos"]').as('table'); cy.get('@table').findByRole('button', {name: 'Add Row'}).click(); cy.get('@table').find('.grid-body .row-index').should('contain', 1001); - cy.get('@table').find('.current-page-number').should('contain', '21'); + cy.get('@table').find('.current-page-number').should('have.value', '21'); cy.get('@table').find('.total-page-number').should('contain', '21'); cy.get('@table').find('.grid-body .grid-row .grid-row-check').click({ force: true }); cy.get('@table').findByRole('button', {name: 'Delete'}).click(); cy.get('@table').find('.grid-body .row-index').last().should('contain', 1000); - cy.get('@table').find('.current-page-number').should('contain', '20'); + cy.get('@table').find('.current-page-number').should('have.value', '20'); cy.get('@table').find('.total-page-number').should('contain', '20'); }); + it('go to specific page, use up and down arrow, type characters, 0 page and more than existing page', () => { + cy.visit('/app/contact/Test Contact'); + cy.get('.frappe-control[data-fieldname="phone_nos"]').as('table'); + cy.get('@table').find('.current-page-number').focus().clear().type('17').blur(); + cy.get('@table').find('.grid-body .row-index').should('contain', 801); + + cy.get('@table').find('.current-page-number').focus().type('{uparrow}{uparrow}'); + cy.get('@table').find('.current-page-number').should('have.value', '19'); + + cy.get('@table').find('.current-page-number').focus().type('{downarrow}{downarrow}'); + cy.get('@table').find('.current-page-number').should('have.value', '17'); + + cy.get('@table').find('.current-page-number').focus().clear().type('700').blur(); + cy.get('@table').find('.current-page-number').should('have.value', '20'); + + cy.get('@table').find('.current-page-number').focus().clear().type('0').blur(); + cy.get('@table').find('.current-page-number').should('have.value', '1'); + + cy.get('@table').find('.current-page-number').focus().clear().type('abc').blur(); + cy.get('@table').find('.current-page-number').should('have.value', '1'); + }); // it('deletes all rows', ()=> { // cy.visit('/app/contact/Test Contact'); // cy.get('.frappe-control[data-fieldname="phone_nos"]').as('table'); diff --git a/frappe/public/js/frappe/form/grid_pagination.js b/frappe/public/js/frappe/form/grid_pagination.js index 35daafe89d..76a5f7b50b 100644 --- a/frappe/public/js/frappe/form/grid_pagination.js +++ b/frappe/public/js/frappe/form/grid_pagination.js @@ -46,8 +46,55 @@ export default class GridPagination { this.last_page_button.on('click', () => { this.go_to_page(this.total_pages); }); + + this.$page_number.on('keyup', (e) => { + e.currentTarget.style.width = ((e.currentTarget.value.length + 1) * 8) + 'px'; + }); + + this.$page_number.on('keydown', (e) => { + e = (e) ? e : window.event; + var charCode = (e.which) ? e.which : e.keyCode; + let arrow = { up: 38, down: 40 }; + + switch (charCode) { + case arrow.up: + this.inc_dec_number(true); + break; + case arrow.down: + this.inc_dec_number(false); + break; + } + + // only allow numbers from 0-9 and up, down, left, right arrow keys + if (charCode > 31 && (charCode < 48 || charCode > 57) && + ![37, 38, 39, 40].includes(charCode)) { + return false; + } + return true; + }); + + this.$page_number.on('focusout', (e) => { + if (this.page_index == e.currentTarget.value) return; + this.page_index = e.currentTarget.value; + + if (this.page_index < 1) { + this.page_index = 1; + } else if (this.page_index > this.total_pages) { + this.page_index = this.total_pages; + } + + this.go_to_page(); + }); } + inc_dec_number(increment) { + let new_value = parseInt(this.$page_number.val()); + increment ? new_value++ : new_value--; + + if (new_value < 1 || new_value > this.total_pages) return; + + this.$page_number.val(new_value); + } update_page_numbers() { let total_pages = Math.ceil(this.grid.data.length/this.page_length); @@ -65,7 +112,7 @@ export default class GridPagination { get_pagination_html() { let page_text_html = `