edatagrid扩展,仿kettle形式的表格实现

来源:互联网 发布:台达plc编程实例47 编辑:程序博客网 时间:2024/06/05 02:34

edatagrid的api提供了增删改的方法,但并没有界面形式的例子。这里仿写了kettle表输出形式的可输入表格。

具体功能有:

1.点击最后一个空白行时新增一行,点击其他空白行时添加该行行号

2.点击勾时,取消本行编辑

3.点击x时,删除本行,同时之后的行号上移


代码如下:

[javascript] view plain copy
 print?
  1. //初始化表输出属性面板  
  2. function init(){  
  3.     $("#outputTableGrid").edatagrid({  
  4.         data:{"rows":[  
  5.                       {"rowIndex":1,"sourceValue":"""targetValue":"""value":"",operation:'<a href="#" onclick="saveRowData(1);" style="width:16px;height:16px;padding: 5px;"><img title="save" src="js/themes/icons/ok.png" ></img></a> <a href="#" onclick="deleteRowData(1);" style="width:16px;height:16px;padding: 5px;"><img  title="remove" src="js/themes/icons/cancel.png" ></img></a>'},  
  6.                       {"rowIndex":"","sourceValue":"""targetValue":"""value":"",operation:""},  
  7.                       {"rowIndex":"","sourceValue":"""targetValue":"""value":"",operation:""},  
  8.                       {"rowIndex":"","sourceValue":"""targetValue":"""value":"",operation:""},  
  9.                       {"rowIndex":"","sourceValue":"""targetValue":"""value":"",operation:""},  
  10.                       {"rowIndex":"","sourceValue":"""targetValue":"""value":"",operation:""},  
  11.                       {"rowIndex":"","sourceValue":"""targetValue":"""value":"",operation:""},  
  12.                       {"rowIndex":"","sourceValue":"""targetValue":"""value":"",operation:""}  
  13.               ]},  
  14.               fit:true,  
  15.               fitColumns:true,  
  16.               singleSelect:true,  
  17.               columns:[[  
  18.                   {field:'rowIndex',title:'#',width:10,align:'center'},  
  19.                   {field:'sourceValue',title:'源值',width:100,align:'center',editor:'text'},  
  20.                   {field:'targetValue',title:'目标值',width:100,align:'center',editor:'text'},  
  21.                   {field:'value',title:'',width:100,align:'center',editor:'text'},  
  22.                   {field:'operation',title:'',width:40,align:'center'}  
  23.               ]],  
  24.              onClickRow:function(index,row){  
  25.                   var rows = $("#outputTableGrid").edatagrid("getRows");  
  26.                   var rowNum = rows.length;  
  27.                   if(row.rowIndex == ""){  
  28.                     //查找当前页面显示的最大行号,未显示的行号默认为0  
  29.                     var maxRowIndex = 0;  
  30.                     for(var i=0;i<rowNum-1;i++){  
  31.                         if(rows[i].rowIndex > maxRowIndex){  
  32.                             maxRowIndex = rows[i].rowIndex;  
  33.                         }  
  34.                     }  
  35.                       
  36.                     if(maxRowIndex < rowNum-1){  
  37.                             //修改页面显示的最大行号的下一行  
  38.                             $("#outputTableGrid").edatagrid("updateRow",{  
  39.                                   index:maxRowIndex,  
  40.                                   row:{  
  41.                                       rowIndex:maxRowIndex+1,  
  42.                                       sourceValue:"",  
  43.                                       targetValue:"",  
  44.                                       value:"",  
  45.                                       operation:'<a href="#" onclick="saveRowData('+maxRowIndex+');" style="width:16px;height:16px;padding: 5px;"><img title="save" src="js/themes/icons/ok.png" ></img></a> '+  
  46.                                           '<a href="#" onclick="deleteRowData('+maxRowIndex+');" style="width:16px;height:16px;padding: 5px;"><img  title="remove" src="js/themes/icons/cancel.png" ></img></a>'  
  47.                                   }  
  48.                               });  
  49.                     }else if(maxRowIndex ==  rowNum-1){  
  50.                             row.rowIndex = "";      //设置最后一行的行号为空  
  51.                             $("#outputTableGrid").edatagrid("endEdit",index);  
  52.                             $("#outputTableGrid").edatagrid("insertRow",{  
  53.                                   index:index,  
  54.                                   row: {  
  55.                                       rowIndex:index+1,  
  56.                                       sourceValue:"",  
  57.                                       targetValue:"",  
  58.                                       value:"",  
  59.                                       operation:'<a href="#" onclick="saveRowData('+index+');" style="width:16px;height:16px;padding: 5px;"><img title="save" src="js/themes/icons/ok.png" ></img></a> '+  
  60.                                       '<a href="#" onclick="deleteRowData('+index+');" style="width:16px;height:16px;padding: 5px;"><img  title="remove" src="js/themes/icons/cancel.png" ></img></a>'  
  61.                                     }  
  62.                              });  
  63.                     }  
  64.                 }  
  65.             }  
  66.     });  
  67. }  
  68.   
  69. //结束选中行的编辑  
  70. function saveRowData(index){  
  71.       $("#outputTableGrid").edatagrid("endEdit",index);  
  72. }  
  73. //删除选中的行  
  74. function deleteRowData(index){  
  75.     $("#outputTableGrid").edatagrid("deleteRow",index);  
  76.     var data = $("#outputTableGrid").edatagrid("getData");  
  77.     //遍历列表数据  
  78.     for(var i=0;i<data.rows.length-1;i++){  
  79.         //查找删除行之后的行数据  
  80.         if(data.rows[i].rowIndex>index){  
  81.             //修改删除行之后的行索引  
  82.             data.rows[i].rowIndex = data.rows[i].rowIndex -1;  
  83.             data.rows[i].operation = '<a href="#" onclick="saveRowData('+(data.rows[i].rowIndex-1)+');" style="width:16px;height:16px;padding: 5px;"><img title="save" src="js/themes/icons/ok.png" ></img></a> '+  
  84.               '<a href="#" onclick="deleteRowData('+(data.rows[i].rowIndex-1)+');" style="width:16px;height:16px;padding: 5px;"><img  title="remove" src="js/themes/icons/cancel.png" ></img></a>';  
  85.         }  
  86.     }  
  87.     //重新加载数据  
  88.     $("#outputTableGrid").edatagrid("loadData",data);  
  89.       
  90.       
  91. }  


实现时的注意事项:

a.只有点击最后一行时才添加行,点击其他空白行时只修改行号,这就要求最大的行号需要与行索引对应,行号-1=行索引。同时新增和修改需要以当前最大行号来决定。

b.不能使用formatter属性,因为该属性将和updateRow,deleteRow,insertRow等相冲突,将导致你调用这些方法时不生效。

c.注意onClickCell事件是优于行内的点击事件的,而浏览器默认事件是优于其他事件执行的。所以如果将onclick事件写到img中是不会触发的。<a>是浏览器默认事件会最先执行。

d.删除一行时需将其之后的行号上移一位,否则跟之前的逻辑冲突,达不到预期效果。

e.由于<a>标签事件最先执行所以必须传入行索引,否则删除或保存操作无法实现。


效果如下:



原创粉丝点击