如何在React Native中呈现html表?

时间:2018-12-12 05:09:25

标签: javascript html reactjs react-native render-html

在我的项目中,我正在从服务器获取html内容的json响应。

我使用react-native-render-html来呈现html。但是表内容没有显示。

有什么方法可以在react native中显示html表吗?那怎么可能呢?任何帮助都是非常可观的。请帮忙。我尝试了诸如react-native-htmlview之类的不同方法。似乎没有按预期工作,以下是我使用react-native-render-html呈现html内容的代码。

json

    [{
    data: {
        course: {
            id: 2864,
            name: "2. This is the title",

            instructor: {
                sub: ""
            },
            menu_order: 0
        },
        description: "<p style="
        text - align: justify;
        ">India is undergoing an economic, social and technological transformation. Perhaps no other phase is going to be as critical as the present one in shaping the future of the country as well as determining the welfare of the people. The economic transition is the vital ingredient of these overall change. Faster growth accompanied by industrial sector expansion, skill addition to the people, creation of quality infrastructure etc. will fuel this growth phase.</p> <p style="
        text - align: justify;
        ">For understanding how important is the present development phase, we have to adopt a historical and comparative study. Following factors helps us to understand the present development phase of India.</p> <ol style="
        text - align: justify;
        "> <li><strong>Achievement of higher growth rate as a middle-income economy. </strong></li> </ol> <p style="
        text - align: justify;
        ">India at present is a lower middle-income economy and has to become a high-income economy undergoing a rapid growth phase extending at least three decades.</p> <p style="
        text - align: justify;
        ">Most important narrative about the Indian economy is that it is the third largest in the world in terms of Purchasing Power Parity GDP. But a superior way to asses a country’s development is to consider per capita income. The widely used ranking about countries’ economic position is that of the World Bank’s GDP Per capita (constant US $) and the data for 2016 shows that India’s per capita income is $1861 compared to China’s $ 6994.</p> <p style="
        text - align: justify;
        ">Table: Categorization of countries by World Bank</p> <table> <tbody> <tr> <td width="
        198 "><strong>Category</strong></td> <td width="
        318 "><strong>PCI as on 2015 in constant US Dollar</strong></td> </tr> <tr> <td width="
        198 ">Low Income Economy</td> <td width="
        318 ">$ 1025 or less</td> </tr> <tr> <td width="
        198 ">Middle Income Economy (India - $ 1861)</td> <td width="
        318 ">$1026 to $4035 (Lower Middle Income) $4036 to $12475 (Upper Middle Income)</td> </tr> <tr> <td width="
        198 ">Higher Income Economy</td> <td width="
        318 ">$ 12476 and above</td> </tr> </tbody> </table> <p style="
        text - align: justify;
        ">According to World Bank metrics, a country with less than $1045 is considered as low-income economy whereas one with a PCI of $12736 or higher is considered as a higher income economy. Higher income means higher standard of living. India is at the bottom of the lower middle-income economies and has to achieve higher economic growth, structural changes including industrialisation to raise per capita income near to the $12736 mark in the long run. This is what the country has to achieve through the transition.</p> <ol style="
        text - align: justify;
        " start="
        2 "> <li><strong> Industrial sector expansion</strong></li> </ol> <p style="
        text - align: justify;
        ">There are several factors that drives the economy to prosperity. Per capita income is just a monitored goal. How it can be raised is through achieving more productivity and employment generation in the sectors that can create big changes is decisive element for the country’s transition. Here comes the role of industrial sector. The industrial sector is known for generating huge employment with minimum skill addition. Similarly, no other sector has higher level of tradability as the industrial sector (means a country can earn big income through exports). Graduating to an expanded services sector without undergoing industrialisation will be self-defeating and unsuitable to a big economy like India. Depending on services sector for exports will not reward as other countries like to protect their services sector from the inflow of India’s skilled persons. If India can increase the contribution of the industrial sector in GDP from the present 30 per cent to say 40-45 %, it implies that sizable income and employment are created in the sector.</p> <ol style="
        text - align: justify;
        " start="
        3 "> <li><strong> Skilling the people when demography favours.</strong></li> </ol> <p style="
        text - align: justify;
        ">India has the largest number of young people in the world besides having largest workforce age group population. This situation is expected to remain till 2045. Now, youth means higher ability to produce, consume and thus stimulate overall economic activities. As in the case of an individual, better things happen for an economy when it is young.</p> <ol style="
        text - align: justify;
        curriculum: false,
        purchase_link: false
    },
    headers: [],
    status: 200
}]

我只粘贴了我的json响应的一小部分,只是为了显示html内容。下面的代码也只是我的渲染方式的一小部分,请帮助我找到解决方案。

<Content>
      {this.state.section.map(article =>

         <CardItem>

         <HTML  html={article.data.description} imagesMaxWidth={Dimensions.get('window').width} />
         </CardItem>
           )}
        </Content>

3 个答案:

答案 0 :(得分:0)

尝试使用以下代码段,基本上所有表标签都将替换为iframe。

let staticHtml = `<h1>This HTML snippet is now rendered with native components !</h1> <h2>Enjoy a webview-free and blazing fast application</h2> <img src="i.imgur.com/dHLmxfO.jpg?2" /> <em style="textAlign: center;">Look at how happy this native cat is</em> <h2>HTML Table</h2> <table> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr></table> `;
  //
  let content = staticHtml;
  const tables = staticHtml.match(/(<table(?:.|\n)*?<\/table>)/g);
  
  tables.map((table) => {
    content = content.replace(table, `<iframe srcdoc="${table}"></iframe>`);
  });

console.log("content after including iframe =>", content)

答案 1 :(得分:0)

您可以使用react-native-render-html部分进行此操作。

检查以下网址的答案,它解决了我的问题。

https://github.com/archriss/react-native-render-html/issues/43#issuecomment-426698839

答案 2 :(得分:0)

<块引用>

推荐方法

您可以使用@native-html/table-plugin

<块引用>

不推荐的方法

或者对 html 内容中的每个单元格使用内联样式

<td style="border: 1px solid black">Hashan</th>