2017-04-20 35 views
0

我有以下語義的UI反應的JSX頁的表如何使用反應以html格式獲取表格?

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

我需要把這個表給HTML字符串中調用方法

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

,並得到它的POST方法:

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

如何以html格式獲取表格?我在jQuery中發現了一個.html()函數,但無法理解如何使用它。我試過了:

<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() { 
     } 
    }); 
} 

但它不起作用。任何人都可以幫忙嗎?

回答

1

你應該有正確的選擇,使其工作:

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

或者用普通JS你可以使用元素的outerHtml屬性:

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

這有助於獲得JS HTML字符串,所以當我呼叫警報(HTML)與正確的HTML字符串警告窗口中顯示。但它沒有發送到GetData方法 - 收入值爲空 – Bashnia007

+0

@ Bashnia007,'HttpPost(「html」)'做什麼?嘗試刪除參數,也許這是一些限制之王? –

2

確保您的代碼被包裹在裏面componentDidMount。安裝組件後立即調用componentDidMount

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() { 
     } 
    }); 
} 
相關問題