Author Topic: Sorting not happening correctly  (Read 3482 times)

Ajay

  • Pro Ultimate
  • Newbie
  • *
  • Posts: 31
    • View Profile
Sorting not happening correctly
« on: May 19, 2016, 07:41:15 pm »
Scenario:
In Search screen,enter search criteria and press "Search" button.
search results are sorted by column "Revelence" in descending order.
I clicked on another column("Date") and data got sorted accordingly.Now in search results screen,i hit back to search criteria screen and clicked on search button.
The search results are returned with previous result sort order "Date" instead of default sort order "Relevence" column.
Can some one suggest how can I make the search results sort by "Relevence" column everytime i click on search button.

Code:

 var columnModel = [{
                                       title: nmc.platform.strings.LogStartDate, dataIndx: "DateLogStart", editable: false, width: 150, align: "left",
                                       render: function (ui) {
                                           var rowData = ui.rowData,
                                               dataIndx = ui.dataIndx,
                                               cellData = rowData[dataIndx];

                                           if (typeof cellData === "undefined")
                                               return "<span ></span>";
                                           else {
                                               var startDateTime = new Date(parseInt(cellData.substring(6, cellData.length - 2)));
                                               return "<span >" + NMCApp.toLocalDateTime(startDateTime,false) + "</span>";
                                           }
                                       }
                                   },
                                  {
                                       title: nmc.platform.strings.FileName, dataIndx: "OriginalFilename", width: 200, align: "left",
                                       dataType: function (val1, val2) {
                                           return NMCApp.stringSort(val1, val2);
                                       }
                                   },

                                   {
                                       title: nmc.platform.strings.ProductName, dataIndx: "ProductName", width: 200, align: "left",
                                       dataType: function (val1, val2) {
                                           return NMCApp.stringSort(val1, val2);
                                       }
                                   },
                                   {
                                      title: nmc.platform.strings.Relevance, dataIndx: "Relevence", width: 100, align: "left", hidden: false
                                   },
                                   {
                                       title: nmc.platform.strings.TaskID, dataIndx: "TaskID", width: 200, align: "left", hidden: true,

                                       dataType: function (val1, val2) {
                                           return NMCApp.stringSort(val1, val2);
                                       }
                                   },
                                   { title: 'LogGuid', dataIndx: 'LogGuid', hidden: true },
                                   
                            ];

                    serverLogsGrid.pqGrid({
                        width: NMCApp.getSearchGridWidth(),
                        height: NMCApp.getSearchGridHeight(),
                        editable: false,
                        showTop: false,
                        showBottom: false,
                        hoverMode: 'row',
                        selectionModel: { type: 'row', mode: 'block' },
                        numberCell: false,
                        roundCorners: false,
                        wrap: false,
                        colModel: columnModel,
                        virtualY: true,
                        virtualX: true,
                        dataModel: {

                            //sortIndx: "Relevence",
                            //sortDir: "down",
                            data: []
                        },
                        sortModel: {
                            cancel: false,
                            type: "local",
                            sorter: [{ dataIndx: "Relevence", dir: "down" }]
                        },
});

function searchLogsAjax() {
            $.ajax({
                type: 'GET',
                url: searchUrl + '&skip=' + startIndex,
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                processdata: false,
                cache: false,

                success: function (data, textStatus, request) {
                    for (var i = 0; i < data.length; i++) {
                        searchedSvrLogs.push(data);
                    }
                    if (data.length == NMCApp.getMaxSearchNumber() && NMCApp.isSearchCancelled() == false) {
                        serverLogsGrid.pqGrid("option", "dataModel.data", searchedSvrLogs);
                       
                        serverLogsGrid.pqGrid("option", "dataModel.sortIndx", 5);
                        serverLogsGrid.pqGrid("refreshDataAndView");
                        searchServerLogsViewModel.numberOfServerLogs(searchedSvrLogs.length);
                        startIndex = startIndex + NMCApp.getMaxSearchNumber();
                        searchServerLogsAjax();
                    }
                    else {

                        var colM = serverLogsGrid.pqGrid("option", "colModel");
                                               
                        if (searchServerLogsViewModel.showSearchCriteriaTaskId()){

                            for (var i = 0; i < colM.length; i++) {
                                if (colM.dataIndx == "TaskID") {
                                    colM.hidden = false;
                                    break;
                                }
                            }
                        }
                        else {
                            for (var i = 0; i < colM.length; i++) {
                                if (colM.dataIndx == "TaskID") {
                                    colM.hidden = true;
                                    break;
                                }
                            }
                        }

                        serverLogsGrid.pqGrid("option", "colModel", colM);

                        serverLogsGrid.pqGrid("option", "dataModel.data", searchedSvrLogs);
                        serverLogsGrid.pqGrid("option", "dataModel.sortIndx", 5);
                        //serverLogsGrid.pqGrid("refreshView");
                        serverLogsGrid.pqGrid("refreshDataAndView");
                       

                        if (searchedSvrLogs.length > 0)
                            serverLogsGrid.pqGrid("setSelection", { rowIndx: 0 });

                        searchServerLogsViewModel.numberOfServerLogs(searchedSvrLogs.length);

                        if (NMCApp.isSearchCancelled() == false) {
                            NMCApp.hideLoadingWindow();
                            searchServerLogsViewModel.numberOfServerLogs(searchedSvrLogs.length);
                        }
                        else
                            searchServerLogsViewModel.numberOfServerLogs(searchedSvrLogs.length + ' (cancelled)');
                    }
                },
                error: function (request, status, error) {
                    NMCApp.hideLoadingWindow();
                    NMCApp.handleNMCException(request.responseText);
                }

            });
        }

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6210
    • View Profile
Re: Sorting not happening correctly
« Reply #1 on: May 19, 2016, 09:40:02 pm »
Since you have dataIndx as strings in colModel definition, numeric dataIndx can't be assigned; this is incorrect. Morever dataModel.sortIndx is deprecated and not supported.

serverLogsGrid.pqGrid("option", "dataModel.sortIndx", 5);
serverLogsGrid.pqGrid("refreshDataAndView");


Instead use this at the beginning of your method:

Code: [Select]
function searchLogsAjax() {
   serverLogsGrid.pqGrid("option", "sortModel.sorter", [{ dataIndx: "Relevence", dir: "down" }] );
   
   $.ajax({
   ....rest of the code........

Ajay

  • Pro Ultimate
  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Sorting not happening correctly
« Reply #2 on: May 20, 2016, 08:51:03 am »
Thanks it worked