1

I have seen several working examples in jQuery but none in the context of React. So wanted this specific query raised.

I have a react Component Class which renders a datatable (as in datatables.net). I have added a View Button for each row in the datatable using column:defaultContent of datatable-Options.

I have written a class Method which is supposed to execute when the view Button is pressed.

The embedded HTML in columnDefault Option is

                columns: [
                  { "defaultContent":`<button onClick=${this.onView}>View</button>`},
                  { data: "name" },
                  { data: "email" },
                  { data: "mobile", "defaultContent": "<i>Not set</i>" },
                  { data: "landline", "defaultContent": "<i>Not set</i>"}
                ]

I can see that the Buttons are rendered.

The challenge is to get the onView method to execute with the specific recordId.

1
  • where that id exists can you please explain your scenario Commented Jan 24, 2019 at 14:58

3 Answers 3

2

if you want to pass data to the callback function which is bind to onclick of button you can use below mentioned code

<button onClick=${() => this.onView(recordId)}>View</button>
Sign up to request clarification or add additional context in comments.

4 Comments

it give me Uncaught SyntaxError: Unexpected end of input I tried using function keyword as well { "defaultContent":<button onClick=${function(){this.onView(_id)}}>View</button>},
it also says _id not found during compilation... meaning the _id is a column name of the table which datatable can see but not React.
try wrapping button in parenthesis { "defaultContent":(<button onClick=${function(){this.onView(_id)}}>View</button>)}
Use debugger statement to test weather the id exists or not before that line executed
0

My solution

   "columns": [
                   {
                       "data": "Column 1",
                   },
                   {
                       "data": "Column 2",
                   },
                   {
                       "data": "Column 3",
                   },
                   {
                       "data": null,
                       "searchable": false,
                       "orderable": false,
                       "render": function (data, type, full, meta) {
                           return ''
                       }
                   },
               ], 
 "columnDefs": [
                   {
                       targets: 3,
                       createdCell: (td, cellData, rowData, row, col) =>
                         ReactDOM.render(
                           <button className="btn btn-warning" style={{borderRadius: '30px'}}
                             onClick={changeName}>
                            data
                           </button>, td),
                     } 
               ]
    const changeName = async (e) => {
        alert('ok')
    }

Comments

0
  columns: [
    { data: 'Column 3' },
    {
      data: null,
      searchable: false,
      orderable: false,
      render: function(data, type, full, meta) {
        return '';
      },
    },
  ],
  columnDefs: [
    { targets: 3, createdCell: (td, cellData, rowData, row, col) => ReactDOM.render(
      <button className="btn btn-warning" style={{borderRadius: '30px'}} onClick={changeName}></button>,
      data,
      td)
    },
  ],

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.