Gridview Getchecked

When calling getChecked if “column_id” isn’t specified default to searching the first column of the table.




getChecked: function (column_id) {

  var settings = gridSettings[this.attr('id')],

    keys = this.find('.keys span'),

    checked = [];


  var input = undefined !== column_id

    ? 'td input[name="' + (column_id.match(/.+\[\]$/) ? column_id : column_id + '[]') + '"]'

    : 'td:first-child input';


  this.find('.' + settings.tableClass).find('tbody tr ' + input).each(function(i) {

    if (this.checked) {

       checked.push(keys.eq(i).text());

    }

  });

  return checked;

}



It’s an interesting idea, but it can have possible problems if users do not read source code so if someone would call this method without a parameter and his first column is not a checkbox column he would not get the result he intended.

Info1: you changed substring with match but this method is slower then substring - http://jsperf.com/substring-vs-match-2

Info2: you changed to find() instead of children(), note that find is searching all nodes while children is searching only the first level child nodes, so children() is used because any grid cell can in addition contain another TABLE

I didn’t know about these functions until having a look at the JS, are they documented anywhere?

Couldn’t see anything in the class references.

Updated to more closely follow original.




getChecked: function (column_id) {

  var settings = gridSettings[this.attr('id')],

    keys = this.find('.keys span'),

    checked = [];


  var nth = '', input = 'input';

  if (undefined !== column_id) {

    if (column_id.substring(column_id.length - 2) !== '[]') {

      column_id = column_id + '[]';

    }

    input += '[name="' + column_id + '"]';

  } else

    nth = ':first-child';


  this.find('.' + settings.tableClass).children('tbody').children('tr')

    .children('td' + nth).children(input).each(function (i) {

      if (this.checked) {

        checked.push(keys.eq(i).text());

      }

    });

                        

  return checked;

}



There is no special documentation for Yii JS methods… getChecked() is mentioned in the doucmentation for selectableRows - http://www.yiiframework.com/doc/api/1.1/CCheckBoxColumn#selectableRows-detail

You don’t need to update the function, that was not the point, I just wanted to explain to you why those where used :D

The point is (as I wrote before) if this change is needed at all. The developer needs to know his column ID, and if a column ID is not sent (the developer forgot to put it as a parameter) the return value could be wrong like first column checked instead of the desired one, or nothing if the first column is not a checkbox column.

I want to make the code as efficient as possible so felt the need to update :)

This was mainly for convenience, I have a CCheckBoxColumn column with no name used for bulk actions.

btw, The documentation references the depreciated function.

Yes I know for the documentation, there is already an issue on github for this - https://github.com/yiisoft/yii/issues/1441

GutHub issue: https://github.com/yiisoft/yii/pull/1585