2012-05-10 48 views
17

可以有人幫助我,我有一個jqgrid,我想突出顯示該行,如果複選框爲真,謝謝!突出顯示行當複選框爲真

enter image description here

這是我想在這個項目做什麼...

function loadjqGrid(jsonGridData){ 
    var xaxis=1300 
    var yaxis = $(document).height(); 
    yaxis = yaxis-500; 
    getGrids();  
    $("#maingrid").jqGrid({ 
     url:'models/mod.quoservicetypedetails.php?ACTION=view', 
     mtype: 'POST', 
     datatype: 'xml', 
     colNames:getColumnNames(jsonGridData), 
     colModel :[ 
      {name:'TypeID', index:'TypeID', width:350,hidden:true, align:'center',sortable:false,editable:true, 
      edittype:"select",editoptions:{value:getTypeID()},editrules: { edithidden: true}}, 
      {name:'Order1', index:'Order1', width:80, align:'center',sortable:false,editable:true,edittype:"textarea",editoptions:{size:"30",maxlength:"30"}},     
      {name:'Order2', index:'Order2', width:80, align:'center',sortable:false,editable:true,edittype:"textarea",editoptions:{size:"30",maxlength:"30"}}, 
      {name:'Order3', index:'Order3', width:80, align:'center',sortable:false,editable:true,edittype:"textarea",editoptions:{size:"30",maxlength:"30"}},      
      {name:'Description', index:'Description', width:140, align:'center',sortable:false,editable:true, 
      edittype:"textarea",editoptions:{size:"30",maxlength:"30"}},      
      {name:'Notes', index:'Notes', width:120, align:'center',sortable:false,editable:true,edittype:"textarea",editoptions:{size:"30",maxlength:"30"}}, 
      {name:'Measure', index:'Measure', width:80, align:'center',sortable:false,editable:true, edittype:"textarea", editoptions:{size:"30",maxlength:"30"}},     
      {name:'UnitPrice', index:'UnitPrice', width:100, align:'center',sortable:false,editable:false,edittype:"textarea",editoptions:{size:"30",maxlength:"30"}}, 
      {name:'Remarks', index:'Remarks', width:140, align:'center',sortable:false,editable:true,edittype:"textarea",editoptions:{size:"30",maxlength:"30"}}, 
      {name:'UnitCost', index:'UnitCost', width:100, align:'center',sortable:false,editable:true,edittype:"textarea",editoptions:{size:"30",maxlength:"30"}},  
      {name:'Service', index:'Service', width:120, align:'center',sortable:false,editable:true,edittype:"textarea",editoptions:{size:"30",maxlength:"30"}}, 
      //If the GroupHeader is true the row background is yellow 
      {name:'GroupHeader', index:'GroupHeader', width:100, align:'center',sortable:false,editable:true,formatter:'checkbox', edittype:'checkbox', type:'select', editoptions:{value:"1:0"}}, 
      {name:'IsGroup', index:'IsGroup', width:80, align:'center',sortable:false,editable:true,formatter:'checkbox', edittype:'checkbox', type:'select', editoptions:{value:"1:0"}}, 
     ], 
     viewrecords: true, 
     rowNum:20, 
     sortname: 'id', 
     viewrecords: true, 
     sortorder: "desc", 
     height: yaxis, 
     pager : '#gridpager', 
     recordtext: "View {0} - {1} of {2}", 
     emptyrecords: "No records to view", 
     loadtext: "Loading...", 
     pgtext : "Page {0} of {1}",   
     height: yaxis, 
     width: xaxis, 
     shrinkToFit: false, 
     multiselect: true, 
     editurl:'models/mod.quoservicetypedetails.php?ACTION=edit' 
    }); 
} 

我怎麼能這樣做?有人能幫我嗎?

+0

http://stackoverflow.com/ questions/6466750/how-to-select-jqgrid-row-on-checkbox-click –

+0

[link] https://lh5.googleusercontent.com/-Gda0KxFtUiM/T6uDOgi_YjI/AAAAAAAAAGw/Cdn74czGJ7A/w1519-h449-k/sample。 JPG –

回答

42

我在the answer中描述瞭如何實現突出顯示的一個好方法。

jqGrid 4.3.2版本新增功能 - rowattr回調(請參閱my original suggestion),這是特別爲您的情況引入的。它允許您在填充網格的期間突出顯示一些網格行。爲了獲得最佳的性能優勢,您應該另外使用gridview: true。順便說一句我建議你在所有jqGrids中使用gridview: true

rowattr回調的使用方法十分簡單:

gridview: true, 
rowattr: function (rd) { 
    if (rd.GroupHeader === "1") { // verify that the testing is correct in your case 
     return {"class": "myAltRowClass"}; 
    } 
} 

的CSS類myAltRowClass應該定義高亮行的背景色。

相應的演示中,你可以找到here

enter image description here

因爲在你的演示中,你只需要突出顯示並沒有選擇我沒有在我的演示中使用multiselect: true行。在multiselect: true的情況下,其工作方式完全相同。

在我的回答結束時,我想建議您使用column templates。該功能將使您的代碼更短,更易讀並易於維護。你需要做的是以下幾點:

  • 可以包括普通或列定義在cmTemplete最常用的設置。你的情況可能是
cmTemplate: {align: 'center', sortable: false, editable: true, width: 80} 
  • 那麼你可以定義一些變量,描述你在某些列經常使用公共屬性。例如:
var myCheckboxTemplate = {formatter: 'checkbox', edittype: 'checkbox', type: 'select', 
     editoptions: {value: "1:0"}}, 
    myTextareaTemplate = {edittype: "textarea", 
     editoptions: {size: "30", maxlength: "30"}}; 
  • 後,您可以使用myCheckboxTemplatemyTextareaTemplatecolModel內將你的情況歸納爲以下
colModel: [ 
    {name: 'TypeID', index: 'TypeID', width: 350, hidden: true, edittype: "select", 
     editoptions: {value: getTypeID()}, editrules: { edithidden: true}}, 
    {name: 'Order1', index: 'Order1', template: myTextareaTemplate}, 
    {name: 'Order2', index: 'Order2', template: myTextareaTemplate}, 
    {name: 'Order3', index: 'Order3', template: myTextareaTemplate}, 
    {name: 'Description', index: 'Description', width: 140, template: myTextareaTemplate}, 
    {name: 'Notes', index: 'Notes', width: 120, template: myTextareaTemplate}, 
    {name: 'Measure', index: 'Measure', template: myTextareaTemplate}, 
    {name: 'UnitPrice', index: 'UnitPrice', width: 100, editable: false, template: myTextareaTemplate}, 
    {name: 'Remarks', index: 'Remarks', width: 140, template: myTextareaTemplate}, 
    {name: 'UnitCost', index: 'UnitCost', width: 100, template: myTextareaTemplate}, 
    {name: 'Service', index: 'Service', width: 120, template: myTextareaTemplate}, 
    //If the GroupHeader is true the row background is yellow 
    {name: 'GroupHeader', index: 'GroupHeader', width: 100, template: myCheckboxTemplate}, 
    {name: 'IsGroup', index: 'IsGroup', template: myCheckboxTemplate} 
], 
cmTemplate: {align: 'center', sortable: false, editable: true, width: 80}, 
+0

它的工作原理!謝謝你,更多的權力給你@ Oleg。 –

+4

@JacxToi:不客氣!如果問題解決了,您可以[「接受」](http://meta.stackexchange.com/a/5235/147495)答案。 – Oleg

+0

太棒了!再次感謝您的幫助奧列格!我確實有一個我認爲與這個問題有關的「子問題」。你可以請[**檢查出來這裏**](http://stackoverflow.com/questions/19841588/jqgrid-change-background-color-of-grouping-header)? – FastTrack

相關問題