0

I have problem with jqGrid, when i wish to sort caron character(š,ž,č,ć,đ,...). I use "caronCharacters" function to replace those caron with code... When caronCharacters(data, sortableColumns[sortIndex], false); is in coment it sort's ok..but in that case we have no carons inside word but some "zxxx" code instead...but when i wish to replace this code back to caron character then i have same result as if i didnt do anything...i suppose that trigger "reloadGrid" sorts data again...hope u understand me...

$('#sortAZ,#sortZA').click(function(){
    var order='asc';
    if ($(this).attr('id')=='sortZA'){
        var order='desc';
    }
    if (sortIndex==''){
        return;
    }
    // change caron character into some code..like zž or zxxx
    var data = $('#gridTable').jqGrid('getGridParam').data;

    caronCharacters(data, sortableColumns[sortIndex], true);
    jQuery("#gridTable").jqGrid('setGridParam',{sortname:sortableColumns[sortIndex],sortorder:order});

    // change code back to caron...if next line is in comment it works sorting, but no carons...
    caronCharacters(data, sortableColumns[sortIndex], false);
    jQuery("#gridTable").trigger('reloadGrid');
    $('#dropMenuFilter').hide();

    function caronCharacters(data, sortableColumn, direction){
        if(direction){
            for(var i in data){
                data[i][sortableColumn] = replaceCaron(data[i][sortableColumn]);
                $('#gridTable').jqGrid('getLocalRow', i)[sortableColumn] = data[i][sortableColumn];
            }
        }
        else{
            for(var i in data){
                data[i][sortableColumn] = replaceCaronCode(data[i][sortableColumn]);
                $('#gridTable').jqGrid('getLocalRow', i)[sortableColumn] = data[i][sortableColumn];
            }
        }
    }
});

function replaceCaron(word){
    word = word.replace(/Č/g, "Cxxx");
    word = word.replace(/Ć/g, "Cxxx");
    word = word.replace(/Đ/g, "Dxxx");
    word = word.replace(/Š/g, "Sxxx");
    word = word.replace(/Ž/g, "Zxxx");
    word = word.replace(/č/g, "cxxx");
    word = word.replace(/ć/g, "cxxx");
    word = word.replace(/đ/g, "dxxx");
    word = word.replace(/š/g, "sxxx");
    word = word.replace(/ž/g, "zxxx");
    return word;
}

function replaceCaronCode(word){
    word = word.replace(/Cxxx/g, "Č");
    word = word.replace(/Cxxx/g, "Ć");
    word = word.replace(/Dxxx/g, "Đ");
    word = word.replace(/Sxxx/g, "Š");
    word = word.replace(/Zxxx/g, "Ž");
    word = word.replace(/cxxx/g, "č");
    word = word.replace(/cxxx/g, "ć");
    word = word.replace(/dxxx/g, "đ");
    word = word.replace(/sxxx/g, "š");
    word = word.replace(/zxxx/g, "ž");
    return word;
}
Clem
  • 11,334
  • 8
  • 34
  • 48
  • Do you use jqGrid with `datatype: "local"` or use some remote datatype ("json" or "xml") with `loadonce: true`? What is `'#sortAZ'` and `'#sortZA'`? Is it not better that the standard sorting by click on the column header will sort corresponds to your custom sorting rule? – Oleg Oct 29 '12 at 14:59
  • i use remote datatype json. Yes, probably is better but i cant use souch sorting...its not in design that i have to achieve... – Clem Oct 29 '12 at 17:33
  • If you use `datatype: "json"` *without* `loadonce: true` then all sorting will be done *on the server side*. So one don't need write any additional JavaScript code, but just change your server code or use COLLATE part in the ORDER BY part of the corresponding SQL statement (see [here](http://msdn.microsoft.com/en-us/library/ms188385.aspx#Collation) for example) – Oleg Oct 29 '12 at 17:50

1 Answers1

1

It I correct understand your requirements you can solve the problem in one from two ways:

  • implement custom sorting by usage sorttype defined as function. See the answer which provide an example.
  • "subclass" internal methods used by jqGrid during compare or strings. See another answer for the corresponding example.
Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798