简体   繁体   English

td 内容太长时如何显示?

[英]How to show td content when its too long?

I have an html table which one of it's field is too big, its a JSON file.我有一个 html 表,其中一个字段太大,它是一个 JSON 文件。 I would like to show it in the following way: the TD content will be a button, and when its clicked, the JSON content is shown in a new window.我想通过以下方式显示它:TD 内容将是一个按钮,单击它时,JSON 内容将显示在新窗口中。

I thought of doing it by putting the JSON as, for example, an hidden div, and when the button is clicked, I will show that div's content, but it seems ugly, and I think that there is a better and more elegant way of doing it.我想通过把 JSON 作为一个隐藏的 div 来做,当按钮被点击时,我会显示那个 div 的内容,但它看起来很丑陋,我认为有一个更好更优雅的方式正在做。

If it's helping, I am using rails.如果有帮助,我正在使用导轨。

What is the best way of doing that?这样做的最佳方法是什么?

Here is a previous question that has a good answer for this problem. 这是以前的问题,对这个问题有很好的答案。

You can add an onClick event where you remove the cropping, to show the whole contents and vice versa.您可以添加一个 onClick 事件,您可以在其中删除裁剪,以显示整个内容,反之亦然。

I have once dealt with a very large grid (167 columns and 2000 rows), one column in which has a really long contents, so did this, I showed about first 200 chars of content then "...", and when user hovers on that cell, I simply showed the whole content in tooltip.我曾经处理过一个非常大的网格(167 列和 2000 行),其中一列的内容非常长,所以这样做了,我显示了大约前 200 个字符的内容,然后是“...”,当用户悬停时在那个单元格上,我只是在工具提示中显示了整个内容。 So basically put the whole content in cell but show only limited content at first by making overflow hidden and putting some marker like "..." and then on hover show whole contents in tooltip.因此,基本上将整个内容放在单元格中,但首先通过隐藏溢出并放置一些诸如“...”之类的标记来仅显示有限的内容,然后悬停在工具提示中显示整个内容。

The bottom line here is you need to use Javascript on the button to load the pop-up / modal ;这里的底线是您需要在按钮上使用 Javascript 来加载pop-up / modal and then show the contents of the JSON file in that new element.然后在该新元素中显示 JSON 文件的内容。 How you show that JSON should either be with AJAX or by using a hidden div如何显示 JSON 应该使用AJAX或使用hidden div

-- ——

Modal模态

在此处输入图片说明

Modal elements are simply divs positioned absolutely on the page (on top of your other content).模态元素只是绝对定位在页面上(在其他内容之上)的divs They can contain any "normal" HTML you wish, and basically just allow you to give the impression of a pop-up .它们可以包含您希望的任何“普通”HTML,并且基本上只是让您给人一种pop-up的印象。

They are really just JS-enabled div elements which appear after event binding.它们实际上只是在事件绑定之后出现的启用 JS 的 div 元素。 I would recommend using one of these with your button - you'll need to bind the button's "click" event to trigger the modal to load:我建议在您的按钮上使用其中之一 - 您需要绑定按钮的“点击”事件以触发模式加载:

#app/assets/javascripts/application.js
$(document).on("click", ".button", function(){
   // Trigger modal
});

There are many modal JS plugins you can use:您可以使用许多模态 JS 插件:

-- ——

JSON JSON

The way to handle your hidden text is going to be slightly trickier.处理隐藏文本的方法会稍微棘手一些。

There are two ways to do it:有两种方法可以做到:

  1. AJAX 2 Hidden Div AJAX 2 隐藏 Div

The Ajax way is most efficient (avoids any superfluous data being added to your page); Ajax 方式是最有效的(避免将任何多余的数据添加到您的页面); but it's the most tricky.但这是最棘手的。

Here's what I'd do:这是我要做的:

#app/assets/javascripts/application.js
$(document).on("click", ".button", function(){
    $.ajax({
        url: "your_json_url",
        dataType: "json",
        success: function(data){
            // Trigger Modal
        }
    });
});

This will allow you to create a model & handle any JSON request like this:这将允许您创建模型并处理任何 JSON 请求,如下所示:

#app/controllers/your_controller.rb
respond_to :json, only: :your_json_action

def your_json_action
    respond_with @your_code
end

This will basically take the ajax response, and append it to a form which you'll be able to then show.这将基本上采用ajax响应,并将其附加到您可以显示的表单中。

- ——

An alternative is to use a hidden div :另一种方法是使用隐藏的 div

#css
.hidden { display: none; }

#view
<table>
    <tr>
        <td>test</td>
        <td>test2</td>
        <td>
            test3
            <div class="hidden">
                your json content here
            </div>
        </td>
    </tr>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM