install
$ meteor add cesarve:react-auto-table $ meteor npm install react-table --save
Usage
1 2//on server && client 3import createAutoTable from 'meteor/cesarve:react-auto-table' 4import {userSchema as schema} from './Users' 5const Collection = Meteor.users 6const columns = [ 7 { 8 Header: 'Email', 9 accessor: 'emails.0.address' 10 }, 11 { 12 Header: 'First name', 13 accessor: 'profile.firstName' 14 }, 15 { 16 Header: 'Family name', 17 accessor: 'profile.familyName' 18 }, 19] 20 21export default createAutoTable({Collection, schema, columns})
Props
| Property | Type | Required/default | Where | Explanation |
|---|---|---|---|---|
| Collection | MongoCollection | required | Both | Where the data comes from |
| columns | Array | required | Both | List of columns to show. see columns prop above |
| schema | SimpleSchema | undefined | Both | Schema. This is used to automatically create filters based on field type |
| query | Object | {} | Both | Mongo query for filter the data |
| publish | Function | ()=>true | Server | Function for determine if it is allowed or not publish the data on server side. see publish prop above |
| defaultTypeFilters | Object | seetype filter | Both | Defaults cell filterMethod and filter. see defaultTypeFilters prop above |
| filterKeyUpDelay | Number | 400 | Client | Number of milliseconds for fetch the data after keyup on filters |
Columns prop
for complete options see react-table.
There are two special properties
- filterMethod
- filter
if you pass schema prop both will be filled by AutoTable according to defaultTypeFilters or if not both will filled with:
1filterMethod = (filter, row) => ({[filter.id]: filter.value}), 2filter = ({filter, onChange}) => <input type="text" onChange={event => onChange(event.target.value)} value={filter.value}/> 3
defaultTypeFilters prop
publish prop
This function will receive as unique param a object with {Collection, columns, schema, query} properties and it will have as a context the same context than a publish meteor function (normally you only will need this.userId).
Finally this function need to return:
- true for automatic publication based on columns prop
- false for denied publication
- Any thing else for a custom publication, probably you will need to use this.added('collection_name',_id,{customObject}); this.ready();
Examples:
True or false example
1publish=({Collection, columns, schema, query})=> Roles.userIsInRole(this.userId,'admin')
Custom publish example
1publish=({Collection, columns, schema, query})=> { 2 const handle = Collection.find(query).observeChanges({ 3 added: (id,doc) => { 4 doc.joinField=OtherCollection.find({_id: doc.otherCollectionId}).fetch() /*is just a example, don't abuse of this*/ 5 this.added(Collection._name, id, doc); 6 }, 7 changed: (id) => { 8 /**handle changed**/ 9 }, 10 removed: (id) => { 11 /**handle removed**/ 12 } 13 }); 14 15 this.ready(); 16 this.onStop(() => handle.stop()); 17 /* 18 note: 19 I'm returning undefined in this function, 20 if true is returned automatic publication will be run as well 21 If false is returned, previous publication will be executed but not the automatic 22 */ 23}
publish