I am trying to sort table where my first row is disable from sorting and should remain on top.

I am trying to sort table where my first row is disable from sorting and should remain on top.

tariqnawaztariqnawaz Posts: 5Questions: 2Answers: 0

I have sucessfully achieved my row first row should not sort and remain on top , however my sorting in acsending and desending is not happening properly. I now this question is asked before also but I am unable to achieve it. Request you to please help me.

```
//var oTable;
var onTopValues = new Array("Weightages");
alert(onTopValues);

/*  disable asc sorting for specific rows */
$.fn.dataTableExt.oSort["custom-asc"]  = function(x,y) {

        if( isOnTop(x) ){
            return -1; // keep this row at top
        }

        //if (isOnTop(y)) return 1; // keep this row at top
        if( isOnTop(y) ){
            return 1; // keep this row at top
        }

        if((x < y)){
            return -1;
        }else if((x > y)){
            return 1;
        }else{
            return 0;
        }
       // return ((x < y) ? -1 : ((x > y) ?  1 : 0));
};

$.fn.dataTableExt.oSort['custom-desc'] = function(x,y) {
     if( isOnTop(x) ){
            return -1; // keep this row at top
        }

        //if (isOnTop(y)) return 1; // keep this row at top
        if( isOnTop(y) ){
            return 1; // keep this row at top
        }

        if((x < y)){
            return 1;
        }else if((x > y)){
            return -1;
        }else{
            return 0;
        }
};

function isOnTop(s) {
    // search for on top values
        for (var i = 0; i < onTopValues.length; i++) {
          if (s === onTopValues[i]) {
            return true; 
          }
        }

        return false;
}

$.fn.dataTableExt.afnSortData['custom-ontop'] = function  ( oSettings, iColumn )
{
    console.log(iColumn);
  var aData = [];

  $('td:eq('+iColumn+')',  _fnGetTrNodes1(oSettings)).each(function(){
     if(isOnTop($(this).closest('tr').find('td:eq(0)').text())){
        aData.push($(this).closest('tr').find('td:eq(0)').text());
    }else{
         aData.push($(this).text());
    }
  });

  if(aData==null || aData==''){
      $(_fnGetTrNodes1(oSettings)).each(function(){
          if(isOnTop($(this).closest('tr').find('td:eq(0)').text())){
                aData.push($(this).closest('tr').find('td:eq(0)').text());
            }else{
                 aData.push($(this).text());
            }
      });
  }
  console.log(aData);
  return aData;
};

function _fnGetTrNodes1 ( oSettings )
{
    //alert("3 method to click on sort");
        var aNodes = [];
        var aoData = oSettings.aoData;
        console.log(aoData);
        console.log(aoData.length);
        for ( var i=0, iLen=aoData.length ; i<iLen ; i++ )
        {
                if ( aoData[i].nTr !== null )
                {
                        aNodes.push( aoData[i].nTr );
                }
        }
        return aNodes;
}


$(document).ready(function() {
    var typeName ='';
    /* Apply custom classes to table headers */
    $.fn.dataTableExt.oStdClasses.sSortAsc = "custom_asc";
    $.fn.dataTableExt.oStdClasses.sSortDesc = "custom_desc";


    var oTable = $('#table_id1').dataTable({
    "iDisplayLength": 100,
    "lengthMenu": [ 100, 250, 500, 750, 1000 ],
    "order": [[ 1, "asc" ]],
    "aoColumnDefs": [ { aTargets: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14], sType: "custom"}] ,
    "aoColumns": [
                    null,
                    { "sSortDataType": "custom-ontop" , "bSortable": true},
                    { "sSortDataType": "custom-ontop" , "bSortable": true},
                    { "sSortDataType": "custom-ontop" , "bSortable": true},
                    { "sSortDataType": "custom-ontop" , "bSortable": true},
                    { "sSortDataType": "custom-ontop" , "bSortable": true},
                    { "sSortDataType": "custom-ontop" , "bSortable": true},
                    { "sSortDataType": "custom-ontop" , "bSortable": true},
                    { "sSortDataType": "custom-ontop" , "bSortable": true},
                    { "sSortDataType": "custom-ontop" , "bSortable": true},
                    { "sSortDataType": "custom-ontop" , "bSortable": true},
                    { "sSortDataType": "custom-ontop" , "bSortable": true},
                    { "sSortDataType": "custom-ontop" , "bSortable": true},
                    { "sSortDataType": "custom-ontop" , "bSortable": true},
                    { "sSortDataType": "custom-ontop" , "bSortable": true},
                ],
    //"aaSorting": [],



     initComplete: function () {
            this.api().columns(0).every( function (index) {
                   var column = this;
                   var select = $('<select id="filterSelect"></select>')
                       .appendTo( $('#type').empty() )
                       .on( 'change', function () {
                           var val = $(this).val();
                           typeName = $(this).val();
                           //console.log(val);
                           fnShowHide( typeName );
                           column.search( val ? '^'+val+'$' : '', true, false ).draw();
                       } );

                   column.data().unique().sort().each( function ( d, j ) {
                       if(d == 'Consistent Performers - Debt funds'){
                                select.append( '<option selected value="'+d+'">'+d+'</option>' )
                                column.search( d ? '^'+d+'$' : '', true, false ).draw();
                                fnShowHide(d);
                           }else{
                                select.append( '<option value="'+d+'">'+d+'</option>' )
                           }
                   } );
               } );
        }
    }); 

    $('#type').change(function() {
        oTable.fnDraw();
    });

});

Answers

  • tariqnawaztariqnawaz Posts: 5Questions: 2Answers: 0

    for hiding column dynamically I have used this logic.

    function fnShowHide( val )
    {

        /* Get the DataTables object again - this is not a recreation, just a get of the object */
        var oTable = $('#table_id1').dataTable();
    
        if(val == 'Large Cap-oriented Equity funds' || val == 'Diversified Equity funds' || val == 'Small and Mid-cap Equity funds' ||
           val == 'Thematic - Infrastructure funds' || val == 'Equity Linked Savings Scheme (ELSS)'){
            //oTable.fnSetColumnVis( 0, false );   
            oTable.fnSetColumnVis( 1, true );
            oTable.fnSetColumnVis( 2, true );
            oTable.fnSetColumnVis( 3, true );
            oTable.fnSetColumnVis( 4, true );
            oTable.fnSetColumnVis( 5, true );
            oTable.fnSetColumnVis( 6, true );
            oTable.fnSetColumnVis( 7, true );
            oTable.fnSetColumnVis( 8, false );
            oTable.fnSetColumnVis( 9, false );
            oTable.fnSetColumnVis( 10, false );
            oTable.fnSetColumnVis( 11, false );
            oTable.fnSetColumnVis( 12, false );
            oTable.fnSetColumnVis( 13, false );
            oTable.fnSetColumnVis( 14, false );
        }else if(val == 'Index funds'){
            oTable.fnSetColumnVis( 0, false );   
            oTable.fnSetColumnVis( 1, true );
            oTable.fnSetColumnVis( 2, true );
            oTable.fnSetColumnVis( 3, false );
            oTable.fnSetColumnVis( 4, false );
            oTable.fnSetColumnVis( 5, false );
            oTable.fnSetColumnVis( 6, false );
            oTable.fnSetColumnVis( 7, false );
            oTable.fnSetColumnVis( 8, false );
            oTable.fnSetColumnVis( 9, false );
            oTable.fnSetColumnVis( 10, false );
            oTable.fnSetColumnVis( 11, false );
            oTable.fnSetColumnVis( 12, false );
            oTable.fnSetColumnVis( 13, false );
            oTable.fnSetColumnVis( 14, false );
        }else if(val == 'Balanced funds'){
            oTable.fnSetColumnVis( 0, false );   
            oTable.fnSetColumnVis( 1, true );
            oTable.fnSetColumnVis( 2, true );
            oTable.fnSetColumnVis( 3, false );
            oTable.fnSetColumnVis( 4, false );
            oTable.fnSetColumnVis( 5, false );
            oTable.fnSetColumnVis( 6, false );
            oTable.fnSetColumnVis( 7, false );
            oTable.fnSetColumnVis( 8, true );
            oTable.fnSetColumnVis( 9, true );
            oTable.fnSetColumnVis( 10, true );
            oTable.fnSetColumnVis( 11, true );
            oTable.fnSetColumnVis( 12, false );
            oTable.fnSetColumnVis( 13, false );
            oTable.fnSetColumnVis( 14, false );
        }else if(val == 'Monthly Income Plan - Aggressive'){
            oTable.fnSetColumnVis( 0, false );   
            oTable.fnSetColumnVis( 1, true );
            oTable.fnSetColumnVis( 2, true );
            oTable.fnSetColumnVis( 3, false );
            oTable.fnSetColumnVis( 4, false );
            oTable.fnSetColumnVis( 5, false );
            oTable.fnSetColumnVis( 6, false );
            oTable.fnSetColumnVis( 7, false );
            oTable.fnSetColumnVis( 8, true );
            oTable.fnSetColumnVis( 9, true );
            oTable.fnSetColumnVis( 10, true );
            oTable.fnSetColumnVis( 11, true );
            oTable.fnSetColumnVis( 12, true );
            oTable.fnSetColumnVis( 13, false );
            oTable.fnSetColumnVis( 14, false );
        }else if(val == 'Long Term Gilt funds'){
            oTable.fnSetColumnVis( 0, false );   
            oTable.fnSetColumnVis( 1, true );
            oTable.fnSetColumnVis( 2, true );
            oTable.fnSetColumnVis( 3, false );
            oTable.fnSetColumnVis( 4, false );
            oTable.fnSetColumnVis( 5, false );
            oTable.fnSetColumnVis( 6, false );
            oTable.fnSetColumnVis( 7, true );
            oTable.fnSetColumnVis( 8, true );
            oTable.fnSetColumnVis( 9, false );
            oTable.fnSetColumnVis( 10, false );
            oTable.fnSetColumnVis( 11, false );
            oTable.fnSetColumnVis( 12, true );
            oTable.fnSetColumnVis( 13, false );
            oTable.fnSetColumnVis( 14, false );
        }else if(val == 'Long Term Income funds'){
            oTable.fnSetColumnVis( 0, false );   
            oTable.fnSetColumnVis( 1, true );
            oTable.fnSetColumnVis( 2, true );
            oTable.fnSetColumnVis( 3, false );
            oTable.fnSetColumnVis( 4, false );
            oTable.fnSetColumnVis( 5, true );
            oTable.fnSetColumnVis( 6, true );
            oTable.fnSetColumnVis( 7, true );
            oTable.fnSetColumnVis( 8, true );
            oTable.fnSetColumnVis( 9, false );
            oTable.fnSetColumnVis( 10, false );
            oTable.fnSetColumnVis( 11, false );
            oTable.fnSetColumnVis( 12, true );
            oTable.fnSetColumnVis( 13, true );
            oTable.fnSetColumnVis( 14, false );
        }else if(val == 'Short Term Income Funds' || val == 'Credit Opportunities Funds' || val == 'Ultra Short-term Debt funds'){
            oTable.fnSetColumnVis( 0, false );   
            oTable.fnSetColumnVis( 1, true );
            oTable.fnSetColumnVis( 2, true );
            oTable.fnSetColumnVis( 3, true );
            oTable.fnSetColumnVis( 4, true );
            oTable.fnSetColumnVis( 5, true );
            oTable.fnSetColumnVis( 6, true );
            oTable.fnSetColumnVis( 7, true );
            oTable.fnSetColumnVis( 8, false );
            oTable.fnSetColumnVis( 9, false );
            oTable.fnSetColumnVis( 10, false );
            oTable.fnSetColumnVis( 11, false );
            oTable.fnSetColumnVis( 12, true );
            oTable.fnSetColumnVis( 13, true );
            oTable.fnSetColumnVis( 14, false );
        }else if(val == 'Liquid funds'){
            //oTable.fnSetColumnVis( 0, false );   
            oTable.fnSetColumnVis( 1, true );
            oTable.fnSetColumnVis( 2, true );
            oTable.fnSetColumnVis( 3, true );
            oTable.fnSetColumnVis( 4, true );
            oTable.fnSetColumnVis( 5, true );
            oTable.fnSetColumnVis( 6, true );
            oTable.fnSetColumnVis( 7, true );
            oTable.fnSetColumnVis( 8, false );
            oTable.fnSetColumnVis( 9, false );
            oTable.fnSetColumnVis( 10, false );
            oTable.fnSetColumnVis( 11, false );
            oTable.fnSetColumnVis( 12, false );
            oTable.fnSetColumnVis( 13, true );
            oTable.fnSetColumnVis( 14, false );
        }else if(val == 'Consistent Performers - Equity funds'){
            oTable.fnSetColumnVis( 0, false );   
            oTable.fnSetColumnVis( 1, true );
            oTable.fnSetColumnVis( 2, true );
            oTable.fnSetColumnVis( 3, true );
            oTable.fnSetColumnVis( 4, true );
            oTable.fnSetColumnVis( 5, false );
            oTable.fnSetColumnVis( 6, false );
            oTable.fnSetColumnVis( 7, false );
            oTable.fnSetColumnVis( 8, false );
            oTable.fnSetColumnVis( 9, false );
            oTable.fnSetColumnVis( 10, false );
            oTable.fnSetColumnVis( 11, false );
            oTable.fnSetColumnVis( 12, false );
            oTable.fnSetColumnVis( 13, false );
            oTable.fnSetColumnVis( 14, true );
        }else if(val == 'Consistent Performers - Debt funds' || val == 'Consistent Performers - Balanced funds'){
            oTable.fnSetColumnVis( 0, false );   
            oTable.fnSetColumnVis( 1, true );
            oTable.fnSetColumnVis( 2, true );
            oTable.fnSetColumnVis( 3, false );
            oTable.fnSetColumnVis( 4, false );
            oTable.fnSetColumnVis( 5, false );
            oTable.fnSetColumnVis( 6, false );
            oTable.fnSetColumnVis( 7, false );
            oTable.fnSetColumnVis( 8, true );
            oTable.fnSetColumnVis( 9, false );
            oTable.fnSetColumnVis( 10, false );
            oTable.fnSetColumnVis( 11, false );
            oTable.fnSetColumnVis( 12, false );
            oTable.fnSetColumnVis( 13, false );
            oTable.fnSetColumnVis( 14, true );
        }
    }
    

    ```
    I know I have to put some logic in (custom-asc,custom-desc) , but how to do I am not getting it , since I am new in jquery.
    Request you to please help me. please

  • tariqnawaztariqnawaz Posts: 5Questions: 2Answers: 0

    can any one help me on this.. please urgent

  • tariqnawaztariqnawaz Posts: 5Questions: 2Answers: 0

    hi every one.. I really need a help , can any please tell weather it can be done..!

This discussion has been closed.