Skip to content
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

Can i bind my own response data to table #11

Closed
rohitsharmahub opened this issue Jan 26, 2021 · 7 comments
Closed

Can i bind my own response data to table #11

rohitsharmahub opened this issue Jan 26, 2021 · 7 comments

Comments

@rohitsharmahub
Copy link

I created my own search bar rather than using inbuilt search from this table. Now I am looking to pass my server response having exact required format to this Table so i could populate the data over it.

Any comment will be helpful for me. Thanks

@AsemAlalami
Copy link
Owner

@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!

@rohitsharmahub
Copy link
Author

Thank you for your reply.
I tried assigning the data into this.options.responseAdapter(data);
but it does not reflect on the table

@AsemAlalami
Copy link
Owner

@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!

@rohitsharmahub
Copy link
Author

rohitsharmahub commented Feb 3, 2021

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

@rohitsharmahub
Copy link
Author

If am doing it in a wrong way then it would be great if you could share some pieces of code for this task.

@AsemAlalami
Copy link
Owner

@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>
}

@rohitsharmahub
Copy link
Author

Thank you @AsemAlalami :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants