Skip to content

perPage & defaultPerPage #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ And then.. use the component:
Of course, code above will render garbage. Here are the props it accepts to render some sensible data:

```
Prop name => Description => Example
Prop name => Description => Example

title => The title of the datatable => "Todos" // Name in top
columns => Columns. => [ // Array of objects
title => The title of the datatable => "Todos" // Name in top
columns => Columns. => [ // Array of objects
{
label: 'Name', // Column name
field: 'name', // Field name from row
Expand All @@ -79,23 +79,24 @@ columns => Columns. => [
html: false, // Escapes output if false.
}
]
rows => Rows. => [ // Array of objects
rows => Rows. => [ // Array of objects
{
name: "test", // Whatever.
...
}
]
perPage => Number of rows per.. page => 10 (default) // Results per page
onClick => Func. to execute on click => console.log // Function, row 1st param
clickable => Rows are clickable. => true (default) // Row is passed in the event payload
Will fire `row-click` event // See react to click on row (below)
sortable => Cause column-click to sort => true (default) // Whether sortable
searchable => Add fuzzy search functionality => true (default) // Whether searchable
exactSearch => Disable fuzzy search => true (default) // Whether only exact matches are returned
paginate => Add footer next/prev. btns => true (default) // Whether paginated
exportable => Add button to export to Excel => true (default) // Whether exportable
printable => Add printing functionality => true (default) // Whether printable
customButtons => Custom buttons thingy => [ // Array of objects
perPage => Numbers of rows per page => [10, 20, 30, 40, 50] (default) // Results per page
defaultPerPage => Default rows per page => 10 (default) // Default results per page, otherwise it will be the first value of perPage
onClick => Func. to execute on click => console.log // Function, row 1st param
clickable => Rows are clickable. => true (default) // Row is passed in the event payload
Will fire `row-click` event // See react to click on row (below)
sortable => Cause column-click to sort => true (default) // Whether sortable
searchable => Add fuzzy search functionality => true (default) // Whether searchable
exactSearch => Disable fuzzy search => true (default) // Whether only exact matches are returned
paginate => Add footer next/prev. btns => true (default) // Whether paginated
exportable => Add button to export to Excel => true (default) // Whether exportable
printable => Add printing functionality => true (default) // Whether printable
customButtons => Custom buttons thingy => [ // Array of objects
{
hide: false, // Whether to hide the button
icon: 'search', // Materialize icon
Expand Down Expand Up @@ -128,3 +129,25 @@ var app = new Vue({
...

```

### Rows per page

You can specify the options of rows per page with the `perPage` prop. The first value will be the default value and the array will be sorted, so you can put whatever number you want.

```html
<!-- The default value will be 100 -->
<datatable :perPage="[100, 10, 25, 50, 500]"></datatable>

```

The options will be rendered as `[10, 20, 50, 100, 500]`

![Rows per page](http://i.imgur.com/kPtppKz.png)

Otherwise, you can specify the default rows per page with the `defaultPerPage` prop.

```html
<!-- The default value will be 100 -->
<datatable :perPage="[10, 25, 50, 100, 500]" :defaultPerPage="100"></datatable>

```
34 changes: 27 additions & 7 deletions src/DataTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,9 @@
<label>
<span>Rows per page:</span>
<select class="browser-default" @change="onTableLength">
<option value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
<option value="50">50</option>
<option value="-1">All</option>
<option v-for="option in perPageOptions" :value="option" :selected="option == currentPerPage">
{{ option === -1 ? 'All' : option }}
</option>
</select>
</label>
</div>
Expand Down Expand Up @@ -112,7 +109,8 @@
rows: {},
clickable: {default: true},
customButtons: {default: () => []},
perPage: {default: 10},
perPage: {default: [10, 20, 30, 40, 50]},
defaultPerPage: {default: null},
sortable: {default: true},
searchable: {default: true},
exactSearch: {
Expand Down Expand Up @@ -255,6 +253,28 @@
},

computed: {
perPageOptions: function() {
Copy link
Owner

@MicroDroid MicroDroid Jun 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be very significantly shortened with arrow-functions, and the first few lines can just be

var options = (Array.isArray(this.perPage) && this.perPage) || [10, 20, 30, 40, 50];

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I edited the readme for the "typo" and used the shorter way for options

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced the longer functions with arrow functions. The rest looks good to me and works in my demo.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @danilopolani

var options = (Array.isArray(this.perPage) && this.perPage) || [10, 20, 30, 40, 50];

// Force numbers
options = options.map( v => parseInt(v));

// Set current page to first value
this.currentPerPage = options[0];

// Sort options
options.sort((a,b) => a - b);

// And add "All"
options.push(-1);

// If defaultPerPage is provided and it's a valid option, set as current per page
if (options.indexOf(this.defaultPerPage) > -1) {
this.currentPerPage = parseInt(this.defaultPerPage);
}

return options;
},
processedRows: function() {
var computedRows = this.rows;

Expand Down