1.formatter能多數據進行格式化后輸出,formatter必須返回一個字符串,第一個用法:當一個單元格的數據很多,不能顯示完全時,鼠標放上去能動態顯示出所有內容。
formatter:function(value,rowData,rowIndex){ //value是當前字段的值,rowData是該行的所有數據,rowIndex是該行的索引 return '<span title="'+value+'">'+value+'</span>'; }
2.在表格中定義帶按鈕的一行操作列
columns:[[ { title:'控制', field:'id', width:150, formatter:function(value,rowData,rowIndex){ //value是當前字段的值,rowData是該行的所有數據,rowIndex是該行的索引 return '<button onclick="show('+rowIndex+');">編輯</button><button onclick="show('+rowIndex+');">刪除</button>'; } } ]] functon show(index){ var rows= datagrid.datagrid('getRows');//得到所有的行,是一個數組 console.info(rows[index]);//通過rows[index]就能得到該行的數據了 }
傳數據時要傳索引,因為rowData是一個對象,不能傳過去。
3.格式化日期:
先擴展jquery的日期格式
Data.prototype.format=function(format){ if(isNaN(this.getMonth)){ return ''; } if(!format){ format="yyyy-MM-dd hh:mm:ss"; } var o={ /* month*/ "M+":this.getMonth+1, "d+":this.getDate(), "h+":this.getHours(), "m+":this.getMinutes(), "s+":this.getSeconds+1, "q+":Math.floor((this.getMonth()+3/3)), "S+":this.getMilliseconds() }; if(/(y+)/.test(format)){ format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length)); } for(var k in 0){ if(new RegExp("("+k+")").test(format)){ format=format.replace(RegExp.$1,RegExp.$1.length==1?0[k]:("00"+0[k]).substr((""+o[k]).length)); } } return format; }
調用方式:
formatter:function(value){ //value是當前字段的值 return new Date(value).format(); }
