0

I am facing problem in moving selected row up or down in JQgrid, I need to move rows up and down within same grid, but because of transactions( grouping) its not working. Without grouping it works fine for JQgrid :

grouping:true, 
        groupingView : { 
                    groupField : ['address'],
                    groupColumnShow : [false], 
                    groupText : ['<b>Address: {0} - {1} Transaction(s)</b>'],
                    groupCollapse : false,
                    groupOrder: ['asc'], 
                    groupDataSorted : true 
         },

is it possible to move selected row up or down when grouping is true. can anyone share sample code to do it?

Code for move up/Down

 function move(direction){ 

 var selRowIds = getRowIDs('list4');
 if(selRowIds.length == 0)
    {
    alert('Select Units to be moved '+direction);

    }

    else
    {


        if($('#list4').getGridParam('selrow')){
             var ids = $('#list4').getDataIDs();
             var temp = 0;
             var currRow = $('#list4').getGridParam('selrow');
             if(direction === 'up' && currRow > 1)
             {  
                    var r1 = $('#list4').getRowData(currRow-1);
                     var r2 = $('#list4').getRowData(currRow);
                     $('#list4').delRowData(currRow-1);
                     $('#list4').delRowData(currRow);
                     temp = r1.id; //16
                     r1.id = r2.id; // 17
                     r2.id = temp;//  16
                      $('#list4').addRowData(r1.id, r1);    // 17
                     $('#list4').addRowData(r2.id, r2); // 16



             }
             var recordCount = jQuery("#list4").getGridParam("records");
             if(direction === 'down' && currRow < recordCount)
            {
                 var r1 = $('#list4').getRowData(currRow);
                 var r2 = $('#list4').getRowData(parseInt(currRow)+1);
                 $('#list4').delRowData(currRow);
                 $('#list4').delRowData(parseInt(currRow)+1);
                 temp = r1.id;
                 r1.id = r2.id;
                 r2.id = temp;
                 $('#list4').addRowData(r1.id, r1);
                 $('#list4').addRowData(r2.id, r2);
                //  $('#list4').setGridParam({sortname:'id'}).trigger('reloadGrid');
            }
             // Sort the table   

              $('#list4').setGridParam({sortname:'id'}).trigger('reloadGrid');

         }
 }
 }

jqgrid code:

var mandatoryFiled;
grid.jqGrid({
datastr: <%=jsonGridData%>,
datatype: "jsonstring",
height: 400,
    colNames:['S No','Block/House No','Floor No', 'Unit No', 'Street Name',' 6- Digit Postal Code' ,'Trans Id','Address','Property Type','Area (sqm)','Date of Option Issued','Nett Price ($)','Gross Price ($)','Unit Price ($psf)','Nationality Of Buyer','Interested Party Transaction','Date of Option Aborted','Unit Status','listPriceFilter','salePriceFilter','sale_Date_Filter','aborted_Date_Filter','outlierFlag','row_Index'],

    colModel:[
           {name:'id',index:'id', width:40,align:"center", sorttype:"int"},
           {name:'blkNo',index:'blkNo', width:100, align:"center",sorttype:"text",sortable:true,editable:true,editrules:{custom:true, custom_func:mandatoryChk}},
           {name:'storey',index:'storey',align:"center",width:40,sorttype:"text",sortable:true,editable:true,
                editrules:{custom:true, custom_func:mandatoryChk}}, 
           {name:'unit_No',index:'unit_No', width:40,align:"center",sorttype:"text",editable:true,editrules:{custom:true,custom_func:mandatoryChk},onblur: 'submit'},
           {name:'street_Name',index:'street_Name', width:60, align:"center",sorttype:"text",editable:true,editrules:{required:true}},  
           {name:'postal_code',index:'postal_code', width:60, align:"center",sorttype:"text",editable:true,editrules:{custom:true, custom_func:postalCode}},  
          // {name:'postal_code',index:'postal_code', width:60, align:"center",sorttype:"text",editable:true,editrules:{custom:true}},  

           {name:'trans_Id',index:'trans_Id', width:60, align:"center",sorttype:"text",editable:false},  
           {name:'address',index:'address', width:60, align:"center",sorttype:"text",editable:false},  

           {name:'property_Type',index:'property_Type', align:"center",width:80,sortable:true,
            editable:true,edittype:"select",editoptions:{value:pptyTypeStr},formatter:'select',stype: 'select'
              },                    
           {name:'area',index:'area', width:50, align:"center",sorttype:"text",editable:true,
                            editrules:{custom:true, custom_func:myAreaCheck,number:true,required:true}},
           {name:'sale_Date',index:'sale_Date',align:"center", width:75, sorttype:"date",sortable:true,editable:true,
            editoptions: {
                        dataInit: function (element) {
                            $(element).datepicker({
                                dateFormat:"dd/mm/yy",
                                onSelect: function(dateText, inst) { 
                                     var $input = inst.input; // the datepicker input
                                     var $row = $input.parents("tr"); 
                                     $("#list4").jqGrid('saveRow',$row.attr("id"), false); 
                                     }
                                });

                        }
                    }},  

          //  {name:'nett_Price',index:'nett_Price', width:80,align:"center",sorttype:"currency", formatter: 'currency',formatoptions: {  thousandsSeparator: ", " , decimalPlaces: 0, defaultValue: ''  }, editable:true ,editrules:{custom:true, custom_func:mynettpricecheck , number:true} },      
            {name:'nett_Price',index:'nett_Price', width:80,align:"center",sorttype:"text", editable:true, editrules:{custom:true, custom_func:mynettpricecheck }},
          // {name:'sale_Price',index:'sale_Price', width:80,align:"center",sorttype:"currency", formatter: 'currency',formatoptions: {  thousandsSeparator: ", " , decimalPlaces: 0},editable:true ,editrules:{custom:true, custom_func:mypricecheck,number:true}},
           {name:'sale_Price',index:'sale_Price', width:80,align:"center",sorttype:"text", editable:true ,editrules:{custom:true, custom_func:mypricecheck} },
            {name:'pricePSF',index:'pricePSF', width:80,align:"center",sorttype:"text", editable:false},
           {name:'nationality_Code',index:'nationality_Code', width:80,align:"center",editable:true,sortable:true,
            edittype:"select",formatter:'select',stype: 'select',
            searchoptions:{
                          sopt: ['eq'],
                          dataInit:function(el){
                               var defOption = $("option:contains("+defaultCategory+")",el);
                               defOption.attr("selected", "selected");
                               setTimeout(function(){
                                   $(el).trigger('change');
                               },500);
                          }
                      },
                      sorttype: function(cell, obj) {
                          return purchaserDtail[cell];
                      },
            editoptions:{value:purchaserDtailStr}
              },            
            {name:'interested',index:'interested', align:"center",width:70, sortable:true,editable:true,edittype:"select",formatter:'select',stype: 'select',editoptions:{value:interestedStr}},
            {name:'dateAborted',index:'dateAborted',align:"center", width:75, sorttype:"date",sortable:true,editable:true,
             editoptions: {
                        dataInit: function (element) {
                            $(element).datepicker({
                                dateFormat:"dd/mm/yy",
                                onSelect: function(dateText, inst) { 
                                     var $input = inst.input; // the datepicker input
                                     var $row = $input.parents("tr"); 
                                     $("#list4").jqGrid('saveRow',$row.attr("id"), false);
                                     }
                                });

                        }
                    }},
            {name:'unitStatus',index:'unitStatus', align:"center",width:60, sortable:true,editable:false},
            {name:'listPriceFilter',index:'listPriceFilter', align:"center",width:40,editable:false},
            {name:'salePriceFilter',index:'salePriceFilter', align:"center",width:40,editable:false},
            {name:'sale_Date_Filter',index:'sale_Date_Filter', align:"center",width:40,editable:false},
            {name:'aborted_Date_Filter',index:'aborted_Date_Filter', align:"center",width:40,editable:false},
            {name:'outlierFlag',index:'outlierFlag', width:60, align:"center",sorttype:"text",editable:false} ,
            {name:'row_Index',index:'row_Index', align:"center",width:40,editable:false,key:true}

    ],
        jsonReader : {
          root: "rows",
          page: "page",
          total: "total",
          records: "records",
          repeatitems: true,
          cell: "cell",
          id: "id"
    },
        editurl: "clientArray",
        multiselect: true,
        enableSearch: true,
        pagination:true,
        pager: '#search',
        rowNum: 30,
        rowList: [5, 10,20,30,40,50],
        sortname: 'id',   
        sortorder: 'asc',
        sortable:true,
        viewrecords: true,
        //page: 4,
        loadonce: true,  
        //pgtext : "Page {0} of {1}",
        emptyrecords:'No Records',
        // loadtext:'Loading ...',
        showpage:true,   
        caption: "Details of individual units",
        headertitles: true,
        cellEdit: true,
        cellsubmit: 'clientArray',
        grouping:true, 
        groupingView : { 
                    groupField : ['address'],
                    groupColumnShow : [false], 
                    groupText : ['<b>Address: {0} - {1} Transaction(s)</b>'],
                    groupCollapse : false,
                    groupOrder: ['asc'], 
                    groupDataSorted : true 
         }

thanks

enter image description here

After Move up new grid should look like this : Data is coming from database, using JSON for that

enter image description here

checkgdata
  • 313
  • 5
  • 13
  • could you provide an example which demonstrate what you need? – Oleg May 09 '13 at 09:20
  • Hi Oleg, please check i have added image in my question. Now if i select this row and press move up button i want it to move 1 level up in grid and the row above it will come at last position. This functionality works fine when there is no grouping with address which i have highlighted in red, i have also added code of move up/down i am using which was working fine without grouping. Inside this also if a group has multiple rows inside, that code works but not for different groups. I am using JSON for data – checkgdata May 09 '13 at 10:00
  • Sorry, but I still can't follow you. I would be better if you includes more detailed description and more full JavaScript code in your question. For example it's unclear the sense of moving of the selected row. Should `'address'` of selected row from the picture probably be just changed from 35 to 34? After `reloadGrid` all should be displayed correctly. Some common important questions: which `datatype` has the grid? If `datatype` is not `"local"` then: do you use `loadonce: true`? – Oleg May 09 '13 at 11:22
  • Hi Oleg, I am so sorry i couldn't explain you properly. 1. Address is just a grouping element for grouping rows in jqGrid. Address is combination of 1st four columns of jqgrid. 2. Move up/down functionality should work as follows a. As shown in screenshot selected row is last row its address is (35,11-12,Pagoda street) i.e combination of 1st 4 columns of jq grid. b. its data is say complete row with all columns c. Now when this row is selected and i press move up button this row complete row along with grouping address should shift up. i have tried to show it with new image – checkgdata May 09 '13 at 14:47
  • though i can't provide more code details, i've added link to another question which is for exactly same code, answered by you :) Data is displayed based on row id's – checkgdata May 09 '13 at 14:51
  • @Oleg is this question clear to you now? – checkgdata May 13 '13 at 12:18
  • I still don't sure what you need to implement. I wrote my answer how I understood your question. – Oleg May 13 '13 at 13:42
  • @Oleg : I am not able to paste complete code as it exceeds limit, is there any way i can paste or share complete code with you? – checkgdata May 13 '13 at 15:21
  • @Oleg I have included complete jqgrid code. – checkgdata May 14 '13 at 01:18
  • @Oleg I have included complete jqgrid code. 1. On loading i am sorting jqgrid by id, which is row num. 2. Address column is just group of Block/House No','Floor No', 'Unit No', 'Street Name' columns 3. Actually there can be more than 1 transactions for same address so i need to group transactions by address 4. Move up and move down is just for arranging grid rows by user, i dont have to save that sorting. 5. Address for any row is fixed, so i dont want to interchange, the rows check new image for explaination, based on which unit is above or below i just have to move that row up – checkgdata May 14 '13 at 01:25

2 Answers2

0

You don't posted the code which create the grid. It's unclear whether you hold local data or load the data from the server. Moreover I don't understand why one need move address 35 between 33 and 34. I don't understand the meaning of the address column.

I would suggest you to reduce the code of moving of the line to just reloading of the grid. If the value of address column will be changed (on the server or on the client side) then order of the lines will be changed too.

If the order of lines is not the same as the value of address column then one could use just custom sorting of rows. For example in the answer are shown, that the order of grouping lines can be changed by implementing sorttype defined as function. So if you just would use

sorttype: function (cellvalue, rowObject) {
    if (String(cellvalue) === "35") {
        return 34;
    } else if (String(cellvalue) === "34") {
        return 35;
    }
    return cellvalue;
}

then reloading of the grid will follows to swapping of rows with address 34 and 35.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • No i dont have to swap rows as there can be grouping. I am sorry if i am not able to make you clear, i've edited question is it ok now? – checkgdata May 21 '13 at 10:01
  • @checkgdata: You don't explained the meaning of `'address'` column. You don't explain which information you display and which sense have "moving" of the row by the user. In any way moved row should corresponds the roles of grouping which you use. The easiest way is to change `'address'` value and reload the grid. If you have to hold original `'address'` value then you can add new column ( `'addressNew'`) and group by the column. Just change the value of `'addressNew'` and reload the grid (locally). – Oleg May 21 '13 at 10:28
0

This code works for move up/down:

function move(direction){ 

 var selRowIds = getRowIDs('list4');
 if(selRowIds.length == 0)
    {
    alert('Select Units to be moved '+direction);

    }

    else
    {


        if($('#list4').getGridParam('selrow')){
             var ids = $('#list4').getDataIDs();
             var temp = 0;
             var currRow = $('#list4').getGridParam('selrow');
             if(direction === 'up' && currRow > 1)
             {  
                    var r1 = $('#list4').getRowData(currRow-1);
                     var r2 = $('#list4').getRowData(currRow);
                     $('#list4').delRowData(currRow-1);
                     $('#list4').delRowData(currRow);
                     temp = r1.id; //16
                     r1.id = r2.id; // 17
                     r2.id = temp;//  16
                      $('#list4').addRowData(r1.id, r1);    // 17
                     $('#list4').addRowData(r2.id, r2); // 16



             }
             var recordCount = jQuery("#list4").getGridParam("records");
             if(direction === 'down' && currRow < recordCount)
            {
                 var r1 = $('#list4').getRowData(currRow);
                 var r2 = $('#list4').getRowData(parseInt(currRow)+1);
                 $('#list4').delRowData(currRow);
                 $('#list4').delRowData(parseInt(currRow)+1);
                 temp = r1.id;
                 r1.id = r2.id;
                 r2.id = temp;
                 $('#list4').addRowData(r1.id, r1);
                 $('#list4').addRowData(r2.id, r2);
                //  $('#list4').setGridParam({sortname:'id'}).trigger('reloadGrid');
            }
             // Sort the table   

              $('#list4').setGridParam({sortname:'id'}).trigger('reloadGrid');

         }
 }
 }
checkgdata
  • 313
  • 5
  • 13