2014-09-27 159 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