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
more flexibility in the xmlReader
06/11/2011
00:48
Avatar
OlegK
Germany
Member
Members
Forum Posts: 1255
Member Since:
10/08/2009
sp_UserOfflineSmall Offline

Hello Tony,

the current implementation of xmlReader and xmlmap is much less flexible as jsonReader and jsonmap. The most important restriction is that xmlReader and xmlmap don't support reading from attributes. Function are also not supported.

I suggest to use getXmlData in the same way like getAccessor are used in jsonReader and jsonmap. The implementation can be for example the following:

getXmlData: function (obj, expr, returnObj) {
    var ret, m = typeof (expr) === 'string' ? expr.match(/^([.]*)[(w+)]$/) : null;
    if (typeof (expr) === 'function') {
        return expr(obj);
    }
    if (m && m[2]) {
        // m[2] is the attribute selector
        // m[1] is an optional element selector
        // examples: "[id]", "rows[page]", "rows>row[id=13]:first[id]"
        return m[1] ? $(m[1], obj).attr(m[2]) : $(obj).attr(m[2]);
    } else {
        ret = $(expr, obj).filter(':last');
        return !returnObj && ret.length > 0 ? $(ret).text() : ret;
    }

The method will allow selectors like getXmlData(data, 'rows[page]') or getXmlData(data, 'rows>row[id=13]:first[id]') to get the data from the following XML

<?xml version='1.0' encoding='utf-8'?>
<rows page="1" total="2" records="13">
    <row id='13' firstName="Oleg" lastName="Kiriljuk" />
</rows> 

Additionally functions will be also supported. For example

getXmlData(getXmlData(data, 'rows', true), function (x) {
    return $(x).children('row:first').attr('firstName');
}); 

will return "Oleg" from the above XML. As the result one will receive close flexibility in reading of XML data like one has now in reading of JSON data

Best regards
Oleg 

07/11/2011
12:04
Avatar
tony
Sofia, Bulgaria
Moderator
Members

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

Hello Oleg,

As usual very good addition from you.

I plan to include it ASAP.

Thank you.

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.

12/11/2011
17:22
Avatar
OlegK
Germany
Member
Members
Forum Posts: 1255
Member Since:
10/08/2009
sp_UserOfflineSmall Offline

Hello Tony!

You can find here modified version of grid.base.js. The methods getXmlData and addData are added and the function reader are modified a little. The method addData can be used instead of both addXmlData and addJSONData. I included some small fixes to some problems which I found. I tested this and it seem to work.

Take a look on my modification. If you want I can place the code in github.

Best regards
Oleg 

15/11/2011
08:57
Avatar
tony
Sofia, Bulgaria
Moderator
Members

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

Hello Oleg,

The idea is very good and I will implement it step by step.

This way we can test it better and I can look deeper into your code

Thanks

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.

15/11/2011
12:13
Avatar
OlegK
Germany
Member
Members
Forum Posts: 1255
Member Since:
10/08/2009
sp_UserOfflineSmall Offline

Hallo Tony!

I tesed the code, but it would be very good if you test it also carefully.

To make youe easy to read the code the following comments can be interesting for you.

I made some changes in code of getXmlData which I originally posted. It's small changes in the RegEx (parameter of expr.match) and the last lines of getXmlData. New version of the code is

getXmlData: function (obj, expr, returnObj) {
    var ret, m = typeof (expr) === 'string' ? expr.match(/^(.*)\[(\w+)\]$/) : null; 
    if (typeof (expr) === 'function') { return expr(obj); }
    if (m && m[2]) {
        // m[2] is the attribute selector
        // m[1] is an optional element selector
        // examples: "[id]", "rows[page]", "rows>row[id=13]:first[id]"
        return m[1] ? $(m[1], obj).attr(m[2]) : $(obj).attr(m[2]);
    } else {
        ret = $(expr, obj);
        if (returnObj) { return ret; }
        $(expr, obj).filter(':last'); // we use ':last' to be more compatible with old version of jqGrid
        return ret.length > 0 ? $(ret).text() : undefined;

    }

The main change in the code of the reader function is the usage of xmlmap instead of jsonmap in case of datatype equal to "xmlstring". The full code is the following:

reader = function (datatype) {
    var field, fieldName, f = [], j = 0, i, l, cm = ts.p.colModel;
    for (i = 0, l = cm.length; i < l; i++) {
        field = cm[i];
        fieldName = field.name;
        if (fieldName !== 'cb' && fieldName !== 'subgrid' && fieldName !== 'rn') {
            f[j] = datatype === "local" ?
                    fieldName :
                    (datatype === "xml" || datatype === "xmlstring" ?
                            field.xmlmap || fieldName :
                            field.jsonmap || fieldName);
            j++;
        }
    }
    return f;

The code of new addData function are based on addJsonData. Some optimization changes and some small bug fixes (improvements) are made. For example

  • assignmant of variables gi, si and ni based on multiselect, subGrid and rownumbers parameters of jqGrid was in the loop. In addData function it is moved to initialization of gisi and ni.
  • ts.p, ts.p.datatype and $(ts) used frequently in the code of addJsonData. The new variables p, datatype and $t was introduced in addData.
  • on the other side some variables which are used only once like altr, cn1 are removed.
  • variable br=ts.p.scroll?$.jgrid.randId():1 used in the loop as idr = br+i; in addJsonData could follow to problem in case of usage scroll option of jqGrid. The problem is that the method $.jgrid.randId() was called only once and then used ids $.jgrid.randId()+1, $.jgrid.randId()+2 and so on without increasing of internal guid property of jqGrid. So at the next call of $.jgrid.randId() the same id which is already used could be generated and be used in the same grid. The bug was fixed and in addData it will be used the code id = (p.scroll ? $.jgrid.randId() : 1) + index; directly.
  • there are some situations where not all required properties are included in the JSON or XML input. For example, if no page property are included the p.page will be set to 0. In the same way the p.lastpage can be set to default value 1. In there are some items in the grid and the rownumbers parameter is true the value p.page will follow to including negative row numbers. Additionally the value 0 in the pager is not good if there are at least one item. In code of addData are included one additional line which change zero value of p.page to 1 if there are at least one item in the grid.
  • in the same way it can be displayed in the pager "Page 1 from 0" in some not full filled empty data. In code of addData are included one additional line which change the value of p.lastpage to the Math.min(p.page, Math.ceil(len / rn));.
  • Sometime the statement ccur = $.jgrid.getAccessor(cur,dReader.cell); will assign undefined value to ccur. The next statement idr = ccur[idn] || idr; produce an exception in the case. In the code of addData are included additional test to of variable's typeof to "undefined" value.
  • The variable F are replaced to another existing variable f having the same value. Moreover the usage F variable like other names with the first capical character are reservated in JavaScript for classes.
  • In addJsonData the calling of afterInsertRow and appending current data from array rowData to the grid body will be done in case ts.p.gridview === false. In addData the same will be done in case of afterInsRow (if afterInsertRow is a function). It can follow theoretically to some compatibility problem, but because the default value of gridview is false it can improve the performance. Probably you know some more seldom cases like usage of subgrids or some other cases where the test should be stay !ts.p.gridview. You can consider to make the changes back to if (!ts.p.gridview) {

It's the most importand changes in the code which I want commented. Some other small reordering of lines should be clear. Moreover I wrote you some time before about the problem of usage custom formatters or cellattr callbacks in case of getting gata from the server and usage loadonce:true option. I wrote long comment before the loop

for (j = 0, rd = {}; j < f.length; j++) {
    v = getData(cur, f[j]);
    rowData.push(addCell(idr, v, j + gi + si + ni, i + rcnt, cur));
    rd[p.colModel[j + gi + si + ni].name] = v;

I think that the loop could be devided in two loops: one which fill rd object with named properties and another which called addCell and appended the array rowData. In the case one could add an aditional rd parameter to addCell which can be forwarded to the custom formatter or the cellattr callback as also additional parameter. I think that additional parameter will be better to hold compatibility to the previous vesrions.

I hope my above comments could help you better understend and probably improve the code of addData which I suggested.

Best regards
Oleg 

Forum Timezone: Europe/Sofia

Most Users Ever Online: 715

Currently Online:
24 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