0

I have the following semantic-ui-react Table on jsx page

<Table id='myTable' celled selectable sortable headerRow={['Id', 'Name']} renderBodyRow={this.renderBody} tableData={list}  />

I need to put this table to html string in calling method

getHtmlTable() {
    var html = document.getElementById('myTable');
    $.ajax({
        type: "POST",
        url: "http://localhost:50000/api/table/",
        data: html,
        beforeSend: function() {
        },
        success:function() {
        }
    });
}

And get it in the post method:

[HttpPost("html")]
public void GetData(string html)
{        
}

How can i get the table in html format? I found a .html() funciton in jQuery, but can't understand how to use it. I tried so:

<div className="htmlData">
    <Table id='myTable' celled selectable sortable headerRow={['Id', 'Name']} renderBodyRow={this.renderBody} tableData={list}  />
</div>

getHtmlTable() {
    var html = $('htmlData').html();
    $.ajax({
        type: "POST",
        url: "http://localhost:50000/api/table/",
        data: html,
        beforeSend: function() {
        },
        success:function() {
        }
    });
}

But it doesn't work. Can anybody help?

2 Answers 2

2

Make sure your code is wrapped inside componentDidMount. componentDidMount is invoked immediately after a component is mounted.

componentDidMount

componentDidMount(){
  var html = document.getElementById('myTable').outerHTML
  this.getHtmlTable(html)
}
getHtmlTable(html) {
    $.ajax({
        type: "POST",
        url: "http://localhost:50000/api/table/",
        data: html,
        beforeSend: function() {
        },
        success:function() {
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

1

You should have proper selector to make it work:

var html = $('.htmlData').html();

Alternatively with plain JS you can use element's outerHtml property:

var html = document.querySelector("#myTable").outerHtml;

2 Comments

That helps to get html string in js, so when i call alert(html) the alert window with correct html string is shown. But it doesn't sent to GetData method - the income value is null
@Bashnia007, what does HttpPost("html") do? Try remove parameter, maybe it is some king of restriction?

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.