Comments (7)
@rohitsharmahub thanks for using this package
you can pass the data to the table from your server response by responseAdapter
option
and the data will bind automatically to the table by names of columns,
but you can customize any column by defining a function in the ServerTable body that make a switch case for the customized columns (don't forget default
case)
...
const url = 'https://example.test/api/users';
const columns = ['id', 'name', 'email', 'avatar', 'address', 'created_at', 'actions'];
const options = {
...
responseAdapter: function (resp_data) {
return {data: resp_data.data, total: resp_data.meta.total}
},
};
...
<ServerTable columns={columns} url={url} options={options}>
{
function (row, column) {
switch (column) {
case 'avatar':
return (<img src={row.avatar} className="table-image"/>);
case 'address':
return (
<ul>
<li>Street: {row.address.address1}</li>
<li>City: {row.address.city}</li>
<li>Country: {row.address.country}</li>
</ul>
);
case 'actions':
return (
<div style={{textAlign: 'center'}}>
<a className="btn btn-primary btn-xs table-actions-btn"
href={'users/' + row.id + '/edit'}>
<i className="fa fa-pencil-alt"/></a>
<a className="btn btn-danger btn-xs table-actions-btn">
<i className="fa fa-trash"/></a>
</div>
);
default: // Don't forget this
return (row[column]);
}
}
}
</ServerTable>
you can view a full example here
Sorry for the late reply
Thanks!
from react-strap-table.
Thank you for your reply.
I tried assigning the data into this.options.responseAdapter(data);
but it does not reflect on the table
from react-strap-table.
@rohitsharmahub the responseAdapter
option accepts a callback function that should return an object of data and total properties
please, share a snippet of your code to review it!
from react-strap-table.
Hi @AsemAlalami , Thanks for looking into my issue, Please find the code snippet.
i am trying to plot the data into table via this method setTableData() but it does not reflect the passed data on table.
constructor () {
this.checkAllInput = (
<input
type='checkbox'
ref={this.check_all}
onChange={this.handleCheckboxTableAllChange}
/>
);
this.columns = [
'id',
....
];
this.options = {
headings: {
.....
},
sortable: [
.....
],
columnsAlign: {
....
},
icons: {
....
},
texts: {
....
},
responseAdapter: (resp_data) => {
console.log(resp_data);
let usersIDs = resp_data.data.map((a) => +a.id);
this.setState({ usersIDs: usersIDs }, () => {
this.check_all.current.checked =
_.difference(this.state.usersIDs, this.state.selectedUsers)
.length === 0;
});
return { data: resp_data.data, total: resp_data.total };
},
};
}
}
setTableData(resp_data) {
this.options.responseAdapter = (resp_data) => {
console.log(resp_data);
let usersIDs = resp_data.data.map((a) => +a.id);
this.setState({ usersIDs: usersIDs }, () => {
this.check_all.current.checked =
_.difference(this.state.usersIDs, this.state.selectedUsers).length ===
0;
});
return { data: resp_data.data, total: resp_data.total };
};
this.options.responseAdapter(resp_data);
// this.options = {
// ...this.options,
// responseAdapter: (resp_data) => {
// console.log(resp_data);
// let usersIDs = resp_data.data.map((a) => +a.herdbook_id);
// this.setState({ usersIDs: usersIDs }, () => {
// this.check_all.current.checked =
// _.difference(this.state.usersIDs, this.state.selectedUsers)
// .length === 0;
// });
// return { data: resp_data.data, total: resp_data.total };
// },
// };
console.log(this.options);
}
async getServerTableData() {
const response = await Axios.get(
`${apiBaseUrls}dataapi?query=&limit=10&page=2&orderBy=&direction=desc`
);
if (response && response.data) {
console.log(response.data);
this.setTableData(response.data);
}
}
render() {
let self = this;
<ServerTable
columns={this.columns}
url={this.url}
options={this.options}
bordered
hover
>
{(row, column) => {
switch (column) {
case 'id':
return (
<input
key={+row.id}
type='checkbox'
value={+row.id}
onChange={self.handleCheckboxTableChange}
checked={self.state.selectedUsers.includes(
+row.id
)}
/>
);
default:
return row[column];
}
}}
</ServerTable>
}
from react-strap-table.
If am doing it in a wrong way then it would be great if you could share some pieces of code for this task.
from react-strap-table.
@rohitsharmahub I think you trying to use the ServerTable
component as client-side data, but it designs to use as server-side,
means just you need to pass your API URL (without query, ordering, pagination params) and the API configuration.
constructor () {
// API url
this.url = `${apiBaseUrls}dataapi`
this.checkAllInput = (
<input
type='checkbox'
ref={this.check_all}
onChange={this.handleCheckboxTableAllChange}
/>
);
this.columns = [
'id',
....
];
this.options = {
headings: {
.....
},
sortable: [
.....
],
columnsAlign: {
....
},
icons: {
....
},
texts: {
....
},
responseAdapter: (resp_data) => {
console.log(resp_data);
let usersIDs = resp_data.data.map((a) => +a.id);
this.setState({ usersIDs: usersIDs }, () => {
this.check_all.current.checked =
_.difference(this.state.usersIDs, this.state.selectedUsers)
.length === 0;
});
return { data: resp_data.data, total: resp_data.total };
},
};
}
}
render() {
let self = this;
<ServerTable
columns={this.columns}
url={this.url}
options={this.options}
bordered
hover
>
{(row, column) => {
switch (column) {
case 'id':
return (
<input
key={+row.id}
type='checkbox'
value={+row.id}
onChange={self.handleCheckboxTableChange}
checked={self.state.selectedUsers.includes(
+row.id
)}
/>
);
default:
return row[column];
}
}}
</ServerTable>
}
from react-strap-table.
Thank you @AsemAlalami :)
from react-strap-table.
Related Issues (6)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-strap-table.