我正在使用版本1.4.1的w2ui Grid。我正在嘗試執行inline edit,同時使用urls property從服務器加載數據。W2UI Grid數據源聯機編輯
$(function() {
$('#grid').w2grid({
name: 'grid',
// begin block that causes grid to be uneditable
// url: {
// get : '<?php echo site_url('sections')?>/all',
// save : '<?php echo site_url('sections')?>/save'
// },
// end block that causes grid to be eneditable
show: {
toolbar: true,
footer: true,
toolbarSave: true,
toolbarEdit: true
},
columns: [
{
field: 'code',
caption: 'Code',
size: '120px',
sortable: true,
resizable: true,
editable: {
type: 'text'
}
}
],
// this records array can be removed once the urls are added back
records: [
{ recid: 1, code: '100' }
]
});
});
如果我取消註釋「url」塊,網格上的「代碼」字段不能再雙擊編輯。如果我刪除它,它是。有沒有人有一個從服務器動態加載數據的工作示例,同時還允許內聯編輯?
ANSWER 如下所述,我的收益結構是不正確的。我在後端使用CodeIgniter(CI)和我的控制器方法是這樣的:
public function all() {
$data = $this->myModel->findAll();
$gridData = new W2GridData ($data);
echo $gridData->toJson(); //important to put "echo" here and not "return"
}
凡在我的模型類的findAll()方法是:
function findAll() {
$query = $this->db->get (TABLE_NAME);
return $query->result();
}
和我的實用工具類包裝一個CI DB結果現在是:
<?php
class W2GridData {
var $total = "-1";
var $records = "-1";
function __construct($items) {
$this->records = $items;
$this->total = count ($this->records);
}
function toJson() {
$strValue = json_encode ($this);
return str_replace ("\"id\":", "\"recid\":", $strValue); // this line was missing
}
}
正如你所看到的,我正在恢復「ID」直接從數據庫中,並沒有轉化它w2ui的首選「recid」,因此電網沒有正確渲染。
感謝維塔利,僅此而已。我會用解決方案更新上面的問題 – dev 2014-10-07 04:15:59