jQgrid tableToGrid() is ignoring colModel and ColNames in options

Go To StackoverFlow.com

2

I was going through the documentation and source code of jqGrid 4.3.1 when it comes for the tableToGrid() function and I found that the ColModel and ColNames are ignored when included in the options object as they are constructed from HTML table layout.

My question is there a way to force tableToGrid() to accept those two arrays (ColModel,ColNames) rather than construct them out of the HTML table, especially if the the table columns are known in advance.

When I went through the code, I found this portion in the code for TableToGrid

function tableToGrid(selector, options) {
...
...
// Build up the columnModel and the data
var colModel = [];
var colNames = [];
jQuery('th', jQuery(this)).each(function() {
    if (colModel.length === 0 && selectable) {
        colModel.push({
            name: '__selection__',
            index: '__selection__',
            width: 0,
            hidden: true
        });
        colNames.push('__selection__');
    } else {
        colModel.push({
            name: jQuery(this).attr("id") || jQuery.trim(jQuery.jgrid.stripHtml(jQuery(this).html())).split(' ').join('_'),
            index: jQuery(this).attr("id") || jQuery.trim(jQuery.jgrid.stripHtml(jQuery(this).html())).split(' ').join('_'),
            width: jQuery(this).width() || 150
        });
        colNames.push(jQuery(this).html());
    }
});

However, my hack would consider these changes

    function tableToGrid(selector, options) {
...
...
// Build up the columnModel and the data
if(options.hasOwnProperty("colModel") && options.hasOwnProperty("colNames")) {
    var colModel = options.colModel;
    var colNames = options.colNames;
} else {
    var colModel = [];
    var colNames = [];
    jQuery('th', jQuery(this)).each(function() {
                    if (colModel.length === 0 && selectable) {
                    colModel.push({
                    name: '__selection__',
                    index: '__selection__',
                    width: 0,
                    hidden: true
                    });
                    colNames.push('__selection__');
                    } else {
                    colModel.push({
                    name: jQuery(this).attr("id") || jQuery.trim(jQuery.jgrid.stripHtml(jQuery(this).html())).split(' ').join('_'),
                    index: jQuery(this).attr("id") || jQuery.trim(jQuery.jgrid.stripHtml(jQuery(this).html())).split(' ').join('_'),
                    width: jQuery(this).width() || 150
                    });
                    colNames.push(jQuery(this).html());
                    }
    });
}

I just would like to know if there's an easier way (may be an option I missed) that would enforce such behaviour without the need to adjust the source code.

The reason I am doing this in the first place is to force the datefmt option for some of the fields as they are ignored by the jQgrid causing issues with the search functionality. If @Oleg can give some insight about this, it will highly appreciated.

Cheers, N.

2012-04-04 19:37
by nawar
They aren't into consideration when constructing the jqGrid from the existing HTML table - nawar 2012-04-04 20:11


0

I think the main your problem is that you use tableToGrid at all. I find the question about the common usage or architecture as technical question.

If you have typed data like numbers, dates, currency and so on you want probably that the data will be correctly sorted, by jqGrid for example. Parsing of localized data from the HTML table and unformatting of the data is not the best way. You save your time if you would create jqGrid directly. You should provide the input data in data parameter. In the way the data will be easy and safe read, sorted and paged. You can easy include toolbar filtering or advanced searching or some other searching of filtering. All will good work only if you will provide jqGrid pure data.

See the answer for more information.

2012-04-04 20:12
by Oleg
Thanks @Oleg. So I take that I have two options: either I should extract the data from the table and pass it into the data parameter, say in a form of function that reads the table <td> so I have the freedom to change the formatting in advance or the second option is to disregard tableToGrid() and just use jQgrid with information with other dataType, other than local || clientside like JSON moving the task of data formatting to the server side, did I get that correctly - nawar 2012-04-04 20:34
@nawar: Even if you would use datatype: 'json' you should not format the data on the server. You should send pure numbers, dates in language independent ISO 8601 and so on. You should use either predefined or custom formatters and format the data on the client side. I recommend you to use tableToGrid only with pure text data inside or don't use it at all - Oleg 2012-04-04 20:45
@nawar: The code of tableToGrid is not really complex. You can debug it to see all possibilities which you has. In the practice it will be used only by people how starts working with jqGrid and try to hold the existing schema. The more features of jqGrid you will be need the more clear will be to change the input which you use to pure data either as local JavaScript data or the data loaded from the server per Ajax - Oleg 2012-04-04 20:51
Okay, I will take your suggestions into consideration. Thanks @Oleg for your quick help - nawar 2012-04-04 20:51
@nawar: You are welcome! I gave you my personal point of view on the subject - Oleg 2012-04-04 20:53
Ads