2011-12-30 37 views
1

我已經在JSON響應中指定了userdata。根據title屬性的值,jqgrid - 工具欄文本 - 這是做這件事的好方法嗎?

  • 標題將改變以反映title財產
  • 工具欄中的文本(網格標題和數據表頭之間)的值將改變

HTML

<table id="myjqgrid"></table> 
<div id="Pager"></div> 

Ĵ SON

{ 
    "colModel": [ 
     { 
      "name": "ID", 
      "label": "ID", 
      "width": 60, 
      "align": "left", 
      "jsonmap": "cells.0.value", 
      "sortable": true  
     }, 
     { 
      "name": "FirstName", 
      "label": "FirstName", 
      "width": 100, 
      "align": "left", 
      "jsonmap": "cells.1.value", 
      "sortable": false  
     }, 
     { 
      "name": "LastName", 
      "label": "LastName", 
      "width": 100, 
      "align": "left", 
      "jsonmap": "cells.2.value", 
      "sortable": false  
     } 
    ], 
    "colNames": [ 
     "ID", 
     "FirstName", 
     "LastName" 
    ], 
    "mypage": { 
     "outerwrapper": { 
      "page":"1", 
      "total":"1", 
      "records":"20", 
      "innerwrapper": { 
       "rows":[ 
        { 
         "id":"1", 
         "cells": 
         [    
          { 
           "value":"12345", 
           "label": "ID"      
          }, 
          { 
           "value":"David", 
           "label": "FirstName"  
          }, 
          {       
           "value":"Smith", 
           "label": "LastName"       
          }                      
         ]  
        }, 
        { 
         "id":"2", 
         "cells": 
         [    
          { 
           "value":"37546", 
           "label": "ID"      
          }, 
          { 
           "value":"Willy", 
           "label": "FirstName"  
          }, 
          {       
           "value":"Peacock", 
           "label": "LastName"       
          }                      
         ]  
        }, 
        { 
         "id":"3", 
         "cells": 
         [    
          { 
           "value":"62345", 
           "label": "ID"      
          }, 
          { 
           "value":"Kim", 
           "label": "FirstName"  
          }, 
          {       
           "value":"Holmes", 
           "label": "LastName"       
          }                      
         ]  
        }, 
        { 
         "id":"4", 
         "cells": 
         [  
          { 
           "value":"186034", 
           "label": "ID"      
          }, 
          { 
           "value":"Andy", 
           "label": "FirstName"  
          }, 
          {       
           "value":"Wills", 
           "label": "LastName"       
          }                      
         ]  
        }, 
        { 
         "id":"5", 
         "cells": 
         [    
          { 
           "value":"67345", 
           "label": "ID"      
          }, 
          { 
           "value":"Paul", 
           "label": "FirstName"  
          }, 
          {       
           "value":"Lawrence", 
           "label": "LastName"       
          }                      
         ]  
        }, 
        { 
         "id":"6", 
         "cells": 
         [    
          { 
           "value":"12906", 
           "label": "ID"      
          }, 
          { 
           "value":"Andy", 
           "label": "FirstName"  
          }, 
          {       
           "value":"Charlery", 
           "label": "LastName"       
          }                      
         ]  
        }, 
        { 
         "id":"7", 
         "cells": 
         [    
          { 
           "value":"564565", 
           "label": "ID"      
          }, 
          { 
           "value":"Bets", 
           "label": "FirstName"  
          }, 
          {       
           "value":"Josilyn", 
           "label": "LastName"       
          }                      
         ]  
        } 
       ], 
       "userdata": { 
        "title": "My Title 1"  // this can be 'My Title 1' or 'My Title 2'     
       } 
      } 
     } 
    } 
} 

jqGrid的定義

$(document).ready(function() { 
    $.ajax({ 
     type: "GET", 
     url: "myjqgrid.json", 
     data: "", 
     dataType: "json", 
     success: function(response){ 
      var columnData = response.mypage.outerwrapper, 
       columnNames = response.colNames, 
       columnModel = response.colModel; 

      $("#myjqgrid").jqGrid({ 
       datatype: 'jsonstring', 
       datastr: columnData,     
       colNames: columnNames, 
       colModel: columnModel, 
       jsonReader: { 
        root: "innerwrapper.rows", 
        userdata: "innerwrapper.userdata",    
        repeatitems: false 
       }, 
       gridview: true, 
       pager: "#Pager", 
       rowNum: 21, 
       rowList: [21], 
       viewrecords: true,    
       recordpos: 'left', 
       multiboxonly: true, 
       multiselect: true, 
       width: "1406",  
       height: "auto", 
       loadonce: true, 
       toolbar: [true,"top"], 
       loadComplete: function(){ 
        var userdata = $("#myjqgrid").jqGrid('getGridParam', 'userData'); 
        if (userdata) { 
         if (userdata.title) { 
          $("#myjqgrid").jqGrid('setCaption', userdata.title); 
         } 
        } 
        if (userdata.title === "My Title 1") { 
         $("div#t_myjqgrid").append("Viewing the Records."); 
        } else if (userdata.title === "My Title 2") { 
         $("div#t_myjqgrid").append("Editing the Records."); 
        } 
       } 
      }); 
      $("#myjqgrid").jqGrid('navGrid','#Pager', {add:false, edit:false, del:false, position: 'right'}); 
     } 
    }); 
}); 

我的問題是

這是改變div#t_myjqgrid內容的正確方法嗎?或者,jqgrid提供了一個我可以使用的屬性/方法/事件?

回答

1

沒有方法更改jqGrid的toolbar選項添加的頂部或底部工具欄的內容,但可以使用setCaption來設置網格標題(標題)。小修改代碼的demo使用以下loadComplete

loadComplete: function() { 
    var $this = $(this), userdata = $this.jqGrid('getGridParam', 'userData'); 
    if (userdata && userdata.title) { 
     $this.jqGrid('setCaption', userdata.title); 
    } 
    if (userdata.title === "My Title 1") { 
     $this.jqGrid('setCaption', "Viewing the Records."); 
     $('#t_' + $.jgrid.jqID(this.id)) 
      .append('<div style="margin: 0.3em">Viewing the Records.</div>'); 
    } else if (userdata.title === "My Title 2") { 
     $this.jqGrid('setCaption', "Editing the Records."); 
     $('#t_' + $.jgrid.jqID(this.id)) 
      .append('<div style="margin: 0.3em">Editing the Records.</div>'); 
    } 
} 

$.jgrid.jqID(this.id)代替this.id的用法是在如果電網的id(在你的情況下「myjqgrid」)的情況下有用包含一些meta-characters

+0

非常感謝。 – techlead 2012-01-03 07:49:54