简体   繁体   English

jQuery 数据表:避免单元格中的换行符

[英]jQuery DataTables: Avoid linebreaks in cells

I don't have a CSS problem, but a logical problem.我没有 CSS 问题,而是逻辑问题。 I want to get rid of automatic linebreaks in my table cells.我想摆脱表格单元格中的自动换行符。 For some reason, long pieces of data are split in two lines, even in the HTML itself.由于某种原因,长数据被分成两行,即使在 HTML 本身也是如此。 This means that none of the classic CSS attributes are going to help here.这意味着经典的 CSS 属性在这里没有任何帮助。

Example:例子:

<td class="sorting_1">SAP-IT Projekt
 Welle 1</td>

The value inside of the td element is cut in two. td 元素内部的值被一分为二。

What I've tried:我试过的:

            {
                "data": "Project.Name",
                "editField": "ProjectEntry.IdProject",
                "render": function (project) {
                    // This code doesn't really work, the cells still contain linebreaks.
                    project = project.trim().replace(/(\r\n\t|\n|\r\t)/gm, "");
                    console.log(project);
                    return project;
                }
            }

Note: The logging in this render function outputs the string without linebreaks.注意:此渲染中的日志记录 function 输出没有换行符的字符串。

I think I'm missing a crucial piece of configuration that prevents these automatic linebreaks.我想我错过了一个防止这些自动换行符的关键配置。

The table used to have the "responsive" piece of configuration, but I've removed that.该表曾经具有“响应式”配置,但我已将其删除。


Update:更新:

Here's the style of each cell:这是每个单元格的样式:

#ProjectEntryDataContainer, #ProjectEntryDataContainer > div > table > tbody > tr > td {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    table-layout: fixed;
}

This style is being applied to each cell, according to Chrome's DevTools.根据 Chrome 的 DevTools,这种风格正在应用于每个单元格。 The table element hast the class nowrap .表格元素有 class nowrap

As I've mentioned, the CSS has little to nothing to do with my issue.正如我所提到的,CSS 与我的问题几乎没有关系。 The text is being displayed in one line, but the HTML element has a line break in it.文本显示在一行中,但 HTML 元素中有一个换行符。 I want to get rid of the line break in my HTML so I can search the cells properly, using jQuery code.我想摆脱 HTML 中的换行符,这样我就可以使用 jQuery 代码正确搜索单元格。 I'm currently using the following code to search my cells:我目前正在使用以下代码来搜索我的单元格:

var project = rowData.Project.Name.trim().replace(/(\r\n\t|\n|\r\t)/gm, "");

// Find our required cell that we want to edit. If we can't find one, create a new one.
    var colHeaderTarget = $("#ProjectEntryData").find('th:contains("Arbeitsstunden")')[0];
    var rowHeaderTarget = $("#ProjectEntryData").find('td:contains("' + project + '")')[0];

    var projectRows = $("#ProjectEntryData").find("tr:contains('" + project + "')");
    console.log(project);
    console.log(projectRows);

This code works wonderfully as long as the cell doesn't have linebreaks.只要单元格没有换行符,此代码就可以很好地工作。

应用程序截图


I just found out that my JSON data contains linebreaks.我刚刚发现我的 JSON 数据包含换行符。 I'll update once I find out more.一旦我发现更多,我会更新。

{IdProject: 35, Name: "SAP-IT Projekt
↵ Welle 1", Description: null,

Update: The SQL data contained linebreaks... See answer.更新: SQL 数据包含换行符...查看答案。

Turns out that when your database contains line breaks you're going to get line breaks in the HTML. 事实证明,当数据库包含换行符时,您将在HTML中获得换行符。 If anyone has a solution that causes DataTables to filter out line breaks in the cell data, I'll mark that one as the accepted answer. 如果有人解决方案导致DataTables过滤掉单元格数据中的换行符,我将把它标记为可接受的答案。 Otherwise, I'll just advise everyone to avoid linebreaks in your SQL data. 否则,我将建议大家避免在SQL数据中出现换行符。

try &nbsp;试试&nbsp; it worked for me very well.它对我很有用。

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

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