0

我想获得 w2ui 网格的“id”值。记录来自数据库

列:[
{字段:'id',标题:'ID',大小:'50px'},{字段:'名称',标题:'名称',大小:'300px'},]

我的功能

onDblClick: function (event) {

           var grid = this;
           event.onComplete = function () {
           var sel = grid.getSelection();
           console.log('my id value ' + sel.id);
      }

但什么也没有出现。我做错了吗?

4

1 回答 1

4

grid.getSelection()返回一个选定的 recids 数组,请参阅文档

你应该改变你的代码如下:

$(function() {
  $('#grid').w2grid({
    name: 'grid',
    columns: [
      { field: 'id', caption: 'ID', size: '50px' }, 
      { field: 'name', caption: 'Name', size: '300px' }
    ],
    records: [
      { recid: 1, id: '1', name: 'Name 1' },
      { recid: 2, id: '2', name: 'Name 2' } 
    ],
    onDblClick: function(event) {
      var grid = this;
      event.onComplete = function() {
        var sel_rec_ids = grid.getSelection();
        if (sel_rec_ids.length) {
          var sel_record = grid.get(sel_rec_ids[0]);
          console.log('selected ID:', sel_record.id, '/ selected Name:', sel_record.name);
        } else {
          console.log("Nothing selected!");
        }
      }
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://rawgit.com/vitmalina/w2ui/master/dist/w2ui.js"></script>
<link href="https://rawgit.com/vitmalina/w2ui/master/dist/w2ui.css" rel="stylesheet" />

<div id="grid" style="width: 100%; height: 150px;"></div>


还让我引用一些其他人在 2013 年对您的一个问题发表的评论:

我看到你没有接受你的问题的任何答案。这有点违背了 Stack Overflow 的目标。如果您可以查看您提出的所有问题,接受正确的答案并就不起作用的建议解决方案提供反馈,那就太好了

于 2016-07-11T21:27:16.963 回答