API里是这么写的
// 在第二行的位置插入一个新行
$('#dg').datagrid('insertRow',{
index: 1, // 索引从0开始
row: {
name: '新名称',
age: 30,
note: '新消息'
}
});
但如果数据是动态获取的怎么添加到一行里面,比如是通过ajax动态获取的数据
success:function(result){
$('#dg').datagrid('insertRow',{
index: 1, // 索引从0开始
row: {
name: result.name,
age: result.age,
note: result.note
}
});
}
这样的话行是能出来,但是里面一片空白,数据无法显示,这个要怎么弄才能显示数据?
没记错的话,result需要解析吧。var obj = eval('(' + result + ')');
alert( obj.name);
//新增行数据
function addRow(target){
var parentIndex = getRowIndex(target);
//保存父行数据,用于新增数据。
$j('#car_base_rule_table').datagrid('endEdit', parentIndex);
$j('#car_base_rule_table').datagrid('updateRow',{index: parentIndex,row:{}});
//获取父行数据,进行新增操作。
var newIndex = parentIndex+1;
$j('#car_base_rule_table').datagrid('selectRow',parentIndex);
var rowParent = $j('#car_base_rule_table').datagrid('getSelected');
var newRow = jQuery.extend(true, {}, rowParent);
$j('#car_base_rule_table').datagrid('insertRow',{
index:newIndex,
row:newRow
});
}
ajax指定dataType为json,你应该没有指定所以success中result为字符串而不是json对象
dataType:'json',///////
success:function(result){
两种方法:
1:调完insertRow之后调用 refreshRow方法
2:使用getData方法获取grid的数据data,然后将ajax请求的数据push到data中,最后在进行loadData
首先确定你的返回数据是json格式(dataType:'json') 然后就是确保你的返回数据的内容和你需要显示的内容要保持一致,只能多不能少。
先添加进集合里,从集合作为数据源,这样页面内就可以保证显示了。