简体   繁体   English

jQuery数据表:标头自定义,动态列隐藏

[英]jquery Datatables: Header customization, dynamic column hiding

I've got some (probably) minor questions with regards to some user experience in DataTables. 关于DataTables中的某些用户体验,我有(可能)有一些小问题。 I've asked this on the datatables support forum but my bumping the question probably broke some rule... or my request just isn't interesting enough to answer ^_^ 我在datatables支持论坛上曾问过这个问题,但我碰到的问题可能打破了某些规则……或者我的请求不够有趣,无法回答^ _ ^

(also, this is mostly a copy/paste from the post there. i hope you'll forgive me.) (另外,这主要是那里帖子的副本/粘贴。我希望您能原谅我。)

I'll start off by saying that I'm not an experienced programmer. 首先,我说我不是一个有经验的程序员。 I've started on this journey pretty much from scratch a few weeks ago and therefore there is a very big chance that I've been looking the solution straight in the face while not realising that I've been doing so. 几周前,我几乎从头开始了这个旅程,因此很有可能我一直在直面寻找解决方案,却没有意识到自己一直在这样做。 I've read through the documentation a few times but I don't think i'm quite able to complete these final bits without some guidance. 我已经阅读了几次文档,但是如果没有一些指导,我认为我无法完全完成这些最后的部分。


Here's a JSBin link so you can read along as I try to explain: DataTables JSBin 这是一个JSBin链接,以便您尝试阅读时继续阅读: DataTables JSBin

The contents of the JSON that is called in the jsbin above: 上面的jsbin中调用的JSON的内容:

{
    "data":[
            {
            "Name":"Sinatra, Frank",
            "A Application1":"X",
            "SomeGroup":"X",
            "A Application2":null,
            "A Application3":"X",
            "R Rights to service":"X",
            "A Application4":null
            },
            {
            "Name":"Davis, Sammy",
            "A Application1":"X",
            "SomeGroup":"X",
            "A Application2":null,
            "A Application3":"X",
            "R Rights to service":"X",
            "A Application4":null
            },
            {
            "Name":"Martin, Dean",
            "A Application1":"X",
            "SomeGroup":"X",
            "A Application2":null,
            "A Application3":"X",
            "R Rights to service":"X",
            "A Application4":"X"
            },
            {
            "Name":"Bishop, Joey",
            "A Application1":"X",
            "SomeGroup":"X",
            "A Application2":"X",
            "A Application3":null,
            "R Rights to service":"X",
            "A Application4":null
            }
    ],
    "columns":[
            {
            "title":"Name","data":"Name"
            },
            {
            "title":"A Application1","data":"A Application1"
            },
            {
            "title":"SomeGroup","data":"SomeGroup"
            },
            {
            "title":"A Application2","data":"A Application2"
            },
            {
            "title":"A Application3","data":"A Application3"
            },
            {
            "title":"R Rights to service","data":"R Rights to service"
            },
            {
            "title":"A Application4","data":"A Application4"
            }
        ]

} }

My use of DataTables is perhaps slightly unorthodox, but I don't believe that this should cause an issue. 我对DataTables的使用可能有点不合常规,但是我不认为这会引起问题。 As you can see I'm building a grid which displays access rights for a group of users, so that a user (admin / manager) has an overview of an entire team's access rights and is able to compare users on the fly. 如您所见,我正在构建一个显示一组用户访问权限的网格,以便用户(管理员/经理)可以概览整个团队的访问权限,并能够即时比较用户。 It looks somewhat fine for this example but the json file that I would generate internally is of course a bit larger (average of around 200kb filesize, uncompressed). 在这个示例中看起来有些不错,但是我内部生成的json文件当然要大一些(平均大约200kb文件大小,未压缩)。 Therein lies my problem. 这就是我的问题。 I'd like to rotate the headers by 45 degrees to achieve a nicer looking grid. 我想将页眉旋转45度,以获得更漂亮的网格。 I can do it fine in static html based DataTables but I can't seem to be able to get it done in the dynamically generated version. 我可以在基于静态html的DataTables中做得很好,但是我似乎无法在动态生成的版本中完成它。 I'm probably staring myself blind on how to achieve what I want. 我可能对如何实现自己想要的东西视而不见。


My first question: How can I dynamically add a div , and a span inside the headers, as well as add a class to the th . 我的第一个问题:如何动态添加div和标题内的span ,以及如何向th添加类。 So that the end result would be something like: 这样最终结果将类似于:

<th class="rotate-45"><div><span>Header</span></div></th>

(edit) I have solved this by calling a wrapInner as shown in the following jsbin: Datatables JSBin (编辑)我已经通过调用wrapInner来解决此问题,如以下jsbin所示: Datatables JSBin

An additional question which would apply after succesfully rotating: When the headers get rotated in the static HTML, the "clickable area" does not correspond with the text position and orientation (understandable). 成功旋转后将出现的另一个问题:当标题在静态HTML中旋转时,“可单击区域”与文本的位置和方向(可理解的)不对应。 Is it possible to constrict the sorting to the divs created, or perhaps to a small area in front of the header text. 是否可以将排序限制为创建的div,或者限制为标题文本前面的一小部分。 For example: only sort columns by clicking the the sort icon (if they were to be positioned properly in front of the text) 例如:仅通过单击排序图标对列进行排序(如果要正确放置在文本的前面)


My second question is with regards to displaying filtered data. 我的第二个问题是关于显示过滤的数据。 I'd like to hide a column whenever a search result returns a column that is empty for all rows. 每当搜索结果返回所有行都为空的列时,我想隐藏一列。 For example: if the user searches for frank;dean , the column of A Application2 would not be displayed, as neither Frank nor Dean are linked to that application. 例如:如果用户搜索frank;dean ,则不会显示A Application2的列,因为Frank和Dean均未链接到该应用程序。

How can I achieve this cleanly and dynamically? 我怎样才能干净,动态地做到这一点?


Some additional info about my local solution which may or may not be important: My HTML pages and the JSON files are generated automatically for many different groups via a powershell script. 有关本地解决方案的一些其他信息可能并不重要:我的HTML页面和JSON文件是通过Powershell脚本为许多不同的组自动生成的。 Additionally, I am not running the DataTables code inline, i've got a separate buildcontent.js file which has all the DataTables code. 另外,我没有内联运行DataTables代码,我有一个单独的buildcontent.js文件,其中包含所有DataTables代码。 a variable gets created in the html file so that buildcontent.js knows which json file to use. 在html文件中创建一个变量,以便buildcontent.js知道要使用哪个json文件。

I'm assuming that this will not have a big impact on a possible solution. 我假设这不会对可能的解决方案产生太大影响。 If it does, please let me know. 如果有,请告诉我。

I hope that the questions are clear enough, and that the giant wall of text was not too exhausting. 我希望这些问题足够清楚,并且文字的巨墙不要太累人。

Thanks in advance for any advice or pointers in the right direction. 在此先感谢您提供任何建议或正确方向的指点。

I'm not sure if this helps, but I faced a similar problem in my project. 我不确定这是否有帮助,但是我在项目中遇到了类似的问题。 I was working with JSP pages and I could use JSTL libraries and this is how I did it. 我正在使用JSP页面,可以使用JSTL库,这就是我的方法。 My column number was fixed and I had to apply a class to each row of the table data. 我的列号是固定的,我必须对表数据的每一行应用一个类。

<tbody>
<c:forEach var="dataObject" items="${dataList}" varStatus="listIndex">
<c:choose>
<c:when test="${(listIndex.index mod 2) eq 0}">
<tr class="even pointer">
</c:when>
<c:otherwise>
<tr class="odd pointer">
</c:otherwise>
</c:choose>
<td> ${dataObject.attribute} </td>
........
Add as many columns you have ...
</tr>
</c:forEach>
</tbody>

If you have dynamic columns, I think you can still use a nested for loop to loop through the column data. 如果您有动态列,我认为您仍然可以使用嵌套的for循环遍历列数据。 I usually have a map created in runtime and use this map as a reference to loop through. 我通常在运行时创建一个映射,并使用该映射作为循环参考。 Hope this helps. 希望这可以帮助。

Since you have a js file which controls the data, I think you can use if else conditions and use element ids. 由于您有一个控制数据的js文件,我认为您可以使用if条件和元素ID。

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

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