2010-11-09 19 views
6

我一般不喜欢使用Excel和Microsoft产品,但Excel 2007/2010有一些非常好的条件格式化功能,可惜,迄今为止我还没有在其他许多地方看到过。其中一个我在商业报告中广泛使用的是数据栏。 http://blogs.msdn.com/b/excel/archive/2009/08/07/data-bar-improvements-in-excel-2010.aspxjqGrid中的单元格内数据条 - 可能与否?

在我看来,这些数据条对理解超出数字的数据的含义非常有帮助。虽然200和2000用户之间的差异只是人眼难以掌握的数字,但是长10倍的酒吧更直观。

我的问题:是否有任何方法来包含jqGrid中列的每个值的单元格内条件数据栏,镜像Excel功能?这将是我看到摆脱我们的Excel表格并在线报告系统中实施报告的唯一方法。一旦习惯了数据条,数据条就不可或缺,而且他们是我们仍然使用Excel进行报表的唯一原因。

如果我假设在jqGrid中没有像这样的内置功能,那么您是否认为自定义构建它会有很多工作?你有什么想法是最好的办法来解决这个问题吗?

回答

9

它是关于你在你的问题写了Excel的一个有趣的功能。我以前不知道这件事。

你需要的是实现一个custom formater函数。一般来说很容易。你应该编写一个小函数来显示基于值的单元格(颜色栏上的文本)。此外,你应该定义Unformatting custom function这将是非常容易的在你的情况。可以在排序和其他jqGrid操作期间使用unformating函数,其中需要从网格单元获取值。

所以你的问题可以减少到在颜色栏上显示数字。

已更新:我一次又一次地谈论你的问题,因为我发现使用颜色格式化数字可能会非常有帮助。所以我花了一些时间,创建了产生以下结果

alt text

,并可以现场here可以看到相应的代码示例。

对代码的小意见。我不得不创建其产生的任何现有的浏览器以外歌剧渐变栏是格栅被视为

alt text

的CSS类被定义为以下一些CSS类:

.cellDiv 
{ 
    left: 0px; top:5px; height:22px; 
    position:relative;padding:0;margin-right:-4px;border:0; 
} 
.cellTextRight 
{ 
    position:relative; 
    margin-right:4px; 
    text-align:right; 
    float:right; 
} 
.gradient1{ 
    /* fallback (Opera) */ 
    background: #008AEF; 
    /* Mozilla: https://developer.mozilla.org/en/CSS/-moz-linear-gradient */ 
    background: -moz-linear-gradient(left, #008AEF, white); 
    /* Chrome, Safari: http://webkit.org/blog/175/introducing-css-gradients/ */ 
    background: -webkit-gradient(linear, left top, right top, from(#008AEF), to(white)); 
    /* MSIE http://msdn.microsoft.com/en-us/library/ms532997(VS.85).aspx */ 
    filter: progid:DXImageTransform.Microsoft.Gradient(StartColorStr='#008AEF', EndColorStr='white', GradientType=1); 
    /*ie8*/ 
    -ms-filter: "progid:DXImageTransform.Microsoft.Gradient(StartColorStr='#008AEF', EndColorStr='white', GradientType=1)"; 
    position: absolute; left: -2px; top:-5px; right: 2px; height:22px; float:left; 
} 
.gradient2{ 
    background: #63C384; 
    background: -moz-linear-gradient(left, #63C384 0%, white 100%); 
    background: -webkit-gradient(linear, left top, right top, from(#63C384), to(white)); 
    filter: progid:DXImageTransform.Microsoft.Gradient(StartColorStr='#63C384', EndColorStr='white', GradientType=1); 
    -ms-filter: "progid:DXImageTransform.Microsoft.Gradient(StartColorStr='#63C384', EndColorStr='white', GradientType=1)"; 
    position: absolute; left: -2px; top:-5px; right: 2px; height:22px; float:left; 
} 

和的jqGrid的$(document).ready(function() {/*code*/});内部代码:

var grid = $("#list"); 
var gradientNumberFormat = function (cellvalue, gradientClass, minDataValue, 
           maxDataValue, minDisplayValue, maxDisplayValue) { 
    var dataAsNumber = parseFloat(cellvalue); /* parseInt(cellvalue, 10);*/ 
    if (dataAsNumber > maxDataValue) { 
     dataAsNumber = maxDataValue; 
    } 
    if (dataAsNumber < minDataValue) { 
     dataAsNumber = minDataValue; 
    } 
    var prozentVal = minDisplayValue+(dataAsNumber-minDataValue)*(maxDisplayValue- 
             minDisplayValue)/(maxDataValue-minDataValue); 
    return '<div class="cellDiv"><div class="'+gradientClass+'" style="width:'+ 
      prozentVal+'%;"></div><div class="cellTextRight">'+cellvalue + 
      '</div></div>'; 
}; 
var mydata = [ 
    { id: "1", invdate: "2007-10-01", name: "test", note: "note", 
     amount: "200.00", tax: "10.00", total: "210.00" }, 
    { id: "2", invdate: "2007-10-02", name: "test2", note: "note2", 
     amount: "300.00", tax: "20.00", total: "320.00" }, 
    { id: "3", invdate: "2007-09-01", name: "test3", note: "note3", 
     amount: "400.00", tax: "30.00", total: "430.00" }, 
    { id: "4", invdate: "2007-10-04", name: "test", note: "note", 
     amount: "200.00", tax: "10.00", total: "210.00" }, 
    { id: "5", invdate: "2007-10-05", name: "test2", note: "note2", 
     amount: "300.00", tax: "20.00", total: "320.00" }, 
    { id: "6", invdate: "2007-09-06", name: "test3", note: "note3", 
     amount: "400.00", tax: "30.00", total: "430.00" }, 
    { id: "7", invdate: "2007-10-04", name: "test", note: "note", 
     amount: "200.00", tax: "10.00", total: "210.00" }, 
    { id: "8", invdate: "2007-10-03", name: "test2", note: "note2", 
     amount: "300.00", tax: "20.00", total: "320.00" }, 
    { id: "9", invdate: "2007-09-01", name: "test3", note: "note3", 
     amount: "400.00", tax: "30.00", total: "430.00" }, 
    { id: "10", invdate: "2007-10-01", name: "test", note: "note", 
     amount: "200.00", tax: "10.00", total: "210.00" }, 
    { id: "11", invdate: "2007-10-02", name: "test2", note: "note2", 
     amount: "300.00", tax: "20.00", total: "320.00" }, 
    { id: "12", invdate: "2007-09-01", name: "test3", note: "note3", 
     amount: "400.00", tax: "30.00", total: "430.00" }, 
    { id: "13", invdate: "2007-10-04", name: "test", note: "note", 
     amount: "200.00", tax: "10.00", total: "210.00" }, 
    { id: "14", invdate: "2007-10-05", name: "test2", note: "note2", 
     amount: "300.00", tax: "20.00", total: "320.00" }, 
    { id: "15", invdate: "2007-09-06", name: "test3", note: "note3", 
     amount: "400.00", tax: "30.00", total: "430.00" }, 
    { id: "16", invdate: "2007-10-04", name: "test", note: "note", 
     amount: "200.00", tax: "10.00", total: "210.00" }, 
    { id: "17", invdate: "2007-10-03", name: "test2", note: "note2", 
     amount: "300.00", tax: "20.00", total: "320.00" }, 
    { id: "18", invdate: "2007-09-01", name: "test3", note: "note3", 
     amount: "400.00", tax: "30.00", total: "430.00" } 
]; 
grid.jqGrid({ 
    data: mydata, 
    datatype: "local", 
    colNames: ['Inv No', 'Date', 'Client', 'Amount', 'Tax', 'Total', 'Notes'], 
    colModel: [ 
     { name:'id', index:'id', key:true, width:70, align:"right", sorttype:"int", 
      formatter: function (cellvalue) { 
       // the number 1 will be mapped to no color bar 
       // the number 18 will be mapped to the color bar with 100% filled 
       return gradientNumberFormat(cellvalue, "gradient1", 1, 18, 0, 100); 
      } 
     }, 
     { name:'invdate', index:'invdate', width:90, sorttype:"date" }, 
     { name:'name', index:'name', width:100 }, 
     { name:'amount', index:'amount', width:80, align:"right", sorttype:"float", 
      formatter: function (cellvalue) { 
       // the number 200 will be mapped to the 10% filled color bar 
       // the number 400 will be mapped to the 90% filled color bar 
       return gradientNumberFormat(cellvalue,"gradient2",200,400,10,90); 
      } 
     }, 
     { name:'tax', index:'tax', width:80, align:"right", sorttype:"float" }, 
     { name:'total', index:'total', width:80, align:"right", sorttype:"float" }, 
     { name:'note', index:'note', width:150, sortable:false } 
    ], 
    pager: '#pager', 
    rowNum: 10, 
    rowList: [5, 10, 20, 50], 
    sortname: 'id', 
    sortorder: 'desc', 
    viewrecords: true, 
    height: "100%", 
    caption: "Numbers with gradient color" 
}); 
grid.jqGrid('navGrid', '#pager', 
      { add:false, edit:false, del:false, search:false, refresh:true }); 

修订:演示的实际版本是here

+0

谢谢,这很有帮助。我仍然不确定格式化函数的作用是什么来生成数据栏。 – 2010-11-09 11:39:14

+0

@M。 Cypher:可能我会稍后为你创建一个演示示例。 – Oleg 2010-11-09 19:19:53

+0

@M。 Cypher:我对你很新。看看我更新的答案。 – Oleg 2010-11-13 16:24:44

1

我认为这是可能的,但有一点规划和一些假设。

假设:

如果你有一个数字与列的宽度,可以说100像素,然后做出预确定决定,有10米可能的数据条的宽度。 (0,10px,20px,... 100px)。这些每一个都可以被保存为图片,你可以有你漂亮的渐变结束有点太:)

让我们称之为0.png,10.png,20.png,.... 100.png

现在的做法是沿着这些路线的东西:

  1. 的jqGrid让我们做它的东西,使电网等
  2. 消防jQuery的一些一旦其最终挑选出你想要的数据条
  3. 对于列每列
  4. 对于上面列中的每个单元格
  5. 查看数值,并通过乘以因子(可能需要基于列中的最大值)将其缩小/向上,以便在0和0之间得到10的倍数100
  6. 取这个缩放值,让我们说20并设置20.png作为这个单元格的背景。
  7. 冲洗和重复:)
+0

这无疑是一个好的开始。不过,我认为我更喜欢基于CSS或JS的解决方案,因为我希望使用数据栏(1)来显示不同长度的列,(2)使用像素完美宽度,而不是10px-steps。能够调整列大小并使数据栏自动调整其宽度也是很好的。 – 2010-11-09 11:37:26

+0

我认为这也可以通过使用类似的逻辑来实现,但是将实际图像插入到单元格中,并且通过百分比而不是单位来改变其宽度。需要绝对定位/ z-index来确保文本位于数据栏图像的顶部。 – 2010-11-09 22:35:36

相关问题