2014-09-27 166 views
2

我正在使用版本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」,因此電網沒有正確渲染。

回答

2

我把你的代碼到底是怎麼回事,未註釋的url和刪除的記錄。另外,我將它鏈接到一個靜態JSON文件(如果將它鏈接到返回JSON的php,應該沒有區別)。但是,從服務器填充網格後,內聯編輯工作得很好。我使用了1.4.1版本。我最好的猜測是(1)你在控制檯中有一個javascript錯誤,或者(2)你的服務器沒有返回正確的結構。這裏是我的JSON文件:

{ 
    "total": "3", 
    "records": [{ 
    "recid": 1, 
    "code": "Some" 
    }, { 
    "recid": 2, 
    "code": "Another" 
    }, { 
    "recid": 3, 
    "code": "More" 
    }] 
} 
+0

感謝維塔利,僅此而已。我會用解決方案更新上面的問題 – dev 2014-10-07 04:15:59

-1

我簡單的方法,添加屬性recid: '身份證'

$('#grid').w2grid({ 
    name: 'grid', 
    recid : 'id' 
    }); 
+0

歡迎來到SO,代碼似乎沒有完成,肯定缺少)} – 2015-11-10 17:00:12

+0

這也是值得的,包括一個簡短的解釋,爲什麼你這樣做,爲什麼它的作品,以幫助避免反對票。 – Klors 2015-11-10 19:06:58