最近在开发过程中遇到这个问题,在使用easyui datagrid中的formatter时遇到的,起初是这样传递参数的,openDir()函数在后面
//这样传递参数是错误的
function rowformater(value,row,index)
{
return "<a href='#' onclick='javascript:openDir("+row+");'" + ">打开</a>";
}
以为是括号不匹配的错误,检查了半天也没发现不匹配,结果花费了好长时间进行研究调试,再改成这样
//结果报row undefined,这样传递参数也是错误的
function rowformater(value,row,index)
{
return "<a href='#' onclick='javascript:openDir(row);'" + ">打开</a>";
}
所以就通过一个变量,先将row赋值给这个变量,再传递过去,正确传参代码如下
<pre name="code" class="javascript">function rowformater(value,row,index)
{
parm = row;
if(row.isDirOrFile == "file"){
return "<a href='<%=request.getContextPath() %>/filedownload/download.htm?filePath=" + row.filePath + "&fileName=" + row.fileName + "&fileType=" + row.fileType + "'" + "\">下载</a>";
} else{
return "<a href='#' onclick='javascript:openDir(parm);'" + ">打开</a>"; //
}
}
openDir()函数如下:
function openDir(row){
$('#downloadDatagrid').datagrid({
url:$WEB_ROOT_PATH + '/crawle/fileList.htm?fileDto.filePath=' + row.filePath,
});
$('#downloadDatagrid').datagrid('hideColumn', 'isDirOrFile');
}
对应的jsp中代码如下:
<table id="downloadDatagrid" class="easyui-datagrid" style="width: 500px;height: 300px;"
data-options="fit:true,rownumbers:true,singleSelect:true,fitColumns:true,sortName:'fileLength',onDblClickRow:function(row){findFiles(row);}">
<thead>
<tr>
<th data-options="field:'filePath',checkbox:true">文件路径</th>
<th data-options="field:'fileName',width:100">文件名</th>
<th data-options="field:'fileLength',width:100">文件大小</th>
<th data-options="field:'fileType',width:50,<span style="color:#FF0000;">formatter:rowformater</span>">操作</th>
<th data-options="field:'isDirOrFile',width:100"></th>
</tr>
</thead>
</table>