Forum

November 2nd, 2014
A A A
Avatar

Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

The forums are currently locked and only available for read only access
sp_Feed Topic RSS sp_TopicIcon
Table To Grid Bug
31/08/2009
14:31
Avatar
sstodola
New Member
Members
Forum Posts: 2
Member Since:
15/05/2009
sp_UserOfflineSmall Offline

When using table to grid I noticed that I always get a blank row as the first row in the table. Even when running the examples this would happen to me. I changed the following to check to see if the row has data before adding it to the data. I removed the bold line from the top block and replaced it with the 3 bold lines in the bottom block. This fixed the issue for me.

    $('tbody > tr', $(this)).each(function() {
        var row = {};
        var rowPos = 0;
        data.push(row);
        $('td', $(this)).each(function() {
            if (rowPos == 0 && selectable) {
                var input = $('input', $(this));
                var rowId = input.attr("value");
                rowIds.push(rowId || data.length);
                if (input.attr("checked")) {
                    rowChecked.push(rowId);
                }
                row[colModel[rowPos].name] = input.attr("value");
            } else {
                row[colModel[rowPos].name] = $(this).html();
            }
            rowPos++;
        });
    });

    $('tbody > tr', $(this)).each(function() {
        var row = {};
        var rowPos = 0;
        $('td', $(this)).each(function() {
            if (rowPos == 0 && selectable) {
                var input = $('input', $(this));
                var rowId = input.attr("value");
                rowIds.push(rowId || data.length);
                if (input.attr("checked")) {
                    rowChecked.push(rowId);
                }
                row[colModel[rowPos].name] = input.attr("value");
            } else {
                row[colModel[rowPos].name] = $(this).html();
            }
            rowPos++;
        });
        if(rowPos > 0){
            data.push(row);
        }

    });
01/09/2009
03:09
Avatar
tony
Sofia, Bulgaria
Moderator
Members

Moderators
Forum Posts: 7721
Member Since:
30/10/2007
sp_UserOfflineSmall Offline

Hello,

Thanks. Fixed.

Best Regards

Tony

For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.

22/11/2012
19:24
Avatar
sorin_postelnicu
New Member
Members
Forum Posts: 2
Member Since:
22/11/2012
sp_UserOfflineSmall Offline

I encountered a similar problem (TypeError: colModel[rowPos] is undefined http://myapp/js/jquery.jqGrid.src.js Line 12202) and I made the following changes to the function tableToGrid in order to make it easier to debug (and also faster!):

function tableToGrid(selector, options) {
jQuery(selector).each(function() {
    if(this.grid) {return;} //Added by Tony Tomov
    // This is a small "hack" to make the width of the jqGrid 100%
   var thisTable = jQuery(this);
   thisTable.width("99%");
    var w = thisTable.width();

    // Text whether we have single or multi select
    var inputCheckbox = jQuery('tr td:first-child input[type=checkbox]:first', thisTable);
    var inputRadio = jQuery('tr td:first-child input[type=radio]:first', thisTable);
    var selectMultiple = inputCheckbox.length > 0;
    var selectSingle = !selectMultiple && inputRadio.length > 0;
    var selectable = selectMultiple || selectSingle;
    //var inputName = inputCheckbox.attr("name") || inputRadio.attr("name");

    // Build up the columnModel and the data
    var colModel = [];
    var colNames = [];
    jQuery('th', thisTable).each(function() {
      var thisTh = jQuery(this);
      if (colModel.length === 0 && selectable) {
            colModel.push({
                name: '__selection__',
                index: '__selection__',
                width: 0,
                hidden: true
            });
            colNames.push('__selection__');
        } else {
            colModel.push({
                name: thisTh.attr("id") || jQuery.trim(jQuery.jgrid.stripHtml(thisTh.html())).split(' ').join('_'),
                index: thisTh.attr("id") || jQuery.trim(jQuery.jgrid.stripHtml(thisTh.html())).split(' ').join('_'),
                width: thisTh.width() || 150
            });
            colNames.push(thisTh.html());
        }
    });
    var data = [];
    var rowIds = [];
    var rowChecked = [];
    jQuery('tbody > tr', thisTable).each(function() {
      var thisTr = jQuery(this);
      var row = {};
        var rowPos = 0;
        jQuery('td', thisTr).each(function() {
         var thisTd = jQuery(this);
         if (rowPos === 0 && selectable) {
                var input = jQuery('input', thisTd);
                var rowId = input.attr("value");
                rowIds.push(rowId || data.length);
                if (input.is(":checked")) {
                    rowChecked.push(rowId);
                }
                row[colModel[rowPos].name] = input.attr("value");
            } else {
                row[colModel[rowPos].name] = thisTd.html();
            }
            rowPos++;
        });
        if(rowPos >0) { data.push(row); }
    });

    // Clear the original HTML table
   thisTable.empty();

    // Mark it as jqGrid
   thisTable.addClass("scroll");

   thisTable.jqGrid(jQuery.extend({
        datatype: "local",
        width: w,
        colNames: colNames,
        colModel: colModel,
        multiselect: selectMultiple
        //inputName: inputName,
        //inputValueCol: imputName != null ? "__selection__" : null
    }, options || {}));

    // Add data
    var a;
    for (a = 0; a < data.length; a++) {
        var id = null;
        if (rowIds.length > 0) {
            id = rowIds[a];
            if (id && id.replace) {
                // We have to do this since the value of a checkbox
                // or radio button can be anything
                id = encodeURIComponent(id).replace(/[.-%]/g, "_");
            }
        }
        if (id === null) {
            id = a + 1;
        }
      thisTable.jqGrid("addRowData",id, data[a]);
    }

    // Set the selection
    for (a = 0; a < rowChecked.length; a++) {
      thisTable.jqGrid("setSelection",rowChecked[a]);
    }
});
};

Maybe you can include these improvements into your next jqgrid release…

Thanks!

Sorin

Forum Timezone: Europe/Sofia

Most Users Ever Online: 715

Currently Online:
81 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

OlegK: 1255

markw65: 179

kobruleht: 144

phicarre: 132

YamilBracho: 124

Renso: 118

Member Stats:

Guest Posters: 447

Members: 11373

Moderators: 2

Admins: 1

Forum Stats:

Groups: 1

Forums: 8

Topics: 10592

Posts: 31289

Newest Members:

, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.com

Moderators: tony: 7721, Rumen[Trirand]: 81

Administrators: admin: 66

Comments are closed.
Privacy Policy   Terms and Conditions   Contact Information