简体   繁体   English

JQgrid如何根据服务器数据更改行的背景色?

[英]JQgrid how do I change background color of row based on server data?

I'm trying to change the background color of multiple rows based on data returned from a SQL query. 我试图根据SQL查询返回的数据更改多行的背景颜色。 I'm using JSON as my data type and have tried using loadComplete to iterate through the grid and color the rows by adding a css class after the grid is loaded. 我使用JSON作为数据类型,并尝试使用loadComplete遍历网格并通过在加载网格后添加css类为行着色。 This works but I have 1000's of rows returned and this method greatly slows down the loading of the grid. 这行得通,但是我返回了1000的行,这种方法大大减慢了网格的加载速度。 The user has to wait a long time before the grid is finally loaded. 用户必须等待很长时间才能最终加载网格。

I haven't tried using a customer formatter to color the rows because I'm told the grid will not be available yet when the customer formatter is triggered so there would be an error? 我没有尝试使用客户格式化程序为行着色,因为我被告知,当触发客户格式化程序时,网格将仍然不可用,因此会出现错误吗? I have seen documentation on setting a timeout period so the grid is available in the DOM for the customer formatter but this sounds like it would slow the loading of the grid which is what I'm trying to avoid. 我看过有关设置超时期限的文档,因此网格可以在DOM中用于客户格式化程序,但这听起来会减慢网格的加载速度,而这正是我要避免的事情。

Ideally I would like to assign a class to the row during the server call (PHP) based on table data and then when the grid is loaded on the client side it colors the row based on a css style. 理想情况下,我想在服务器调用(PHP)期间基于表数据为该行分配一个类,然后在客户端加载网格时,它将根据css样式为行着色。 It seems that assigning a class on the server side would be the most efficient way so I don't have to cycle through the data more than once or draw the grid more than once? 似乎在服务器端分配一个类是最有效的方法,因此我不必多次遍历数据或绘制网格不止一次? What other techniques are available for doing this? 还有哪些其他技术可以做到这一点? Thanks! 谢谢!

The best way to do what you want is to use rowattr . 进行所需操作的最佳方法是使用rowattr I described in the answer exactly what you need. 我在答案中准确描述了您的需求。 In more old versions you will have to iterate like it's described in more old answer and set class to some rows. 在更旧的版本中,您将不得不像更旧的答案中所描述的那样进行迭代,并将类设置为某些行。

The most important to understand that every change of one elements on the page follows to recalculating of position of all other elements existing on the page. 最重要的是要理解,页面上一个元素的每次更改都是在重新计算页面上所有其他元素的位置之后进行的。 The minimum what need be done is reflow (see here ). 要做的最小工作是回流焊(请参阅此处 )。 In case of usage gridview: true option of jqGrid all rows of the grid body will be created as string first and then will be placed on the page with one operation (something like assign innerHTML property). 在使用gridview: true jqGrid的gridview: true选项的情况下,将首先将网格主体的所有行创建为字符串,然后通过一个操作将其放置在页面上(类似于innerHTML属性之类)。 It improve the performance dramatically in case of usage large number of rows without paging. 在不分页使用大量行的情况下,它可以显着提高性能。 If you use custom formatter, cellattr or rowattr you can customize default process of building of grid body without any performance disadvantages. 如果使用自定义格式程序, cellattrrowattr ,则可以自定义默认的网格主体构建过程,而不会带来任何性能劣势。

The usage of custom formatter is correct in general, but the problem that you have to defines formatter for every column. 自定义格式化程序的用法通常是正确的,但是您必须为每一列定义格式化程序的问题。 Moreover if you want use many columns of different types (checkboxes, numbers, currency) the usage of custom formatter is not good, because you will have to implement one more time all the formatters or call predefined formatters from the custom formattres, which make much unneeded code. 此外,如果要使用许多不同类型的列(复选框,数字,货币),那么自定义格式器的使用效果不好,因为您将不得不再一次实现所有格式器或从自定义格式器中调用预定义的格式器,因此不需要的代码。

The usage of cellattr is better (see here , here or here ) but you will be have to set class attribute of all cells (columns) instead of just setting the class attribute only of the rows ( <tr> ). cellattr的用法更好(请参见此处此处此处 ),但是您将不得不设置所有单元格(列)的class属性,而不仅仅是仅设置行的class属性( <tr> )。

So I find the best to use rowattr like I suggested at the beginning of my answer. 因此,我找到了最好的方法,就像我在回答开始时建议的那样,使用rowattr In the answer you will find exactly what you need. 答案中,您将找到您真正需要的东西。

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

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