Search code examples
w2ui

how do i get a w2ui grid cell value?


i want to get my "id" value of w2ui grid. The record came from a database

columns: [
{ field: 'id', caption: 'ID', size: '50px' }, { field: 'name', caption: 'Name', size: '300px'},]

my function

onDblClick: function (event) {

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

but nothing appear. i do it wrong?


Solution

  • grid.getSelection() returns an array of selected recids, see the documentation.

    You should change your code as follows:

    $(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>


    Also let me quote something, that some else commented on one of your questions in 2013:

    I see you've not accepted any answer to your questions. That kinda defeats the goal of Stack Overflow. It would be great if you could review all the questions you've asked, accept correct answers and give feedback on proposed solutions that don't work