tablesorter not working in page but works in jfiddle

Discussion in 'jQuery' started by illectronic, Oct 3, 2013.

  1. #1
    Jfiddle works: http://jsfiddle.net/BKgue/89/

    It's supposed to sort my table. Here is my code:
    <html>
    <head>
    <link rel="stylesheet" href="css/console.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="css/jq.css" type="text/css" media="screen" />
    <script src="js/jquery-latest.js"></script>
    <script type="text/javascript" src="js/jquery.tablesorter.min.js"></script>
    <script type="text/javascript">
    $('.tablesorter').tablesorter({
        // *** Appearance ***
        // fix the column widths
        widthFixed : true,
        // include zebra and any other widgets, options:
        // 'uitheme', 'filter', 'stickyHeaders' & 'resizable'
        // the 'columns' widget will require custom css for the
        // primary, secondary and tertiary columns
        widgets    : [ 'uitheme', 'zebra' ],
        // other options: "ddmmyyyy" & "yyyymmdd"
        dateFormat : "mmddyyyy",
    
        // *** Functionality ***
        // starting sort direction "asc" or "desc"
        sortInitialOrder : "asc",
        // These are detected by default,
        // but you can change or disable them
        headers : {
            // set "sorter : false" (no quotes) to disable the column
            0: { sorter: "text" },
            1: { sorter: "digit" },
            2: { sorter: "text" },
            3: { sorter: "url" }
        },
        // extract text from the table - this is how is
        // it done by default
        textExtraction : {
            0: function(node) { return $(node).text(); },
            1: function(node) { return $(node).text(); }
        },
        // forces the user to have this/these column(s) sorted first
        sortForce : null,
        // initial sort order of the columns
        // [[columnIndex, sortDirection], ... ]
        sortList : [],
        // default sort that is added to the end of the users sort
        // selection.
        sortAppend : null,
        // Use built-in javascript sort - may be faster, but does not
        // sort alphanumerically
        sortLocaleCompare : false,
        // Setting this option to true will allow you to click on the
        // table header a third time to reset the sort direction.
        sortReset: false,
        // Setting this option to true will start the sort with the
        // sortInitialOrder when clicking on a previously unsorted column.
        sortRestart: false,
        // The key used to select more than one column for multi-column
        // sorting.
        sortMultiSortKey : "shiftKey",
    
        // *** Customize header ***
        onRenderHeader  : function() {
            // the span wrapper is added by default
            $(this).find('span').addClass('headerSpan');
        },
        // jQuery selectors used to find the header cells.
        selectorHeaders : 'thead th',
    
        // *** css classes to use ***
        cssAsc        : "headerSortUp",
        cssChildRow  : "expand-child",
        cssDesc      : "headerSortDown",
        cssHeader    : "header",
        tableClass    : 'tablesorter',
    
        // *** widget css class settings ***
        // column classes applied, and defined in the skin
        widgetColumns : { css: ["primary", "secondary", "tertiary"] },
        // find these jQuery UI class names by hovering over the
        // Framework icons on this page:
        // http://jqueryui.com/themeroller/
        widgetUitheme : { css: [
            "ui-icon-arrowthick-2-n-s", // Unsorted icon
            "ui-icon-arrowthick-1-s",  // Sort up (down arrow)
            "ui-icon-arrowthick-1-n"    // Sort down (up arrow)
            ]
        },
        // pick rows colors to match ui theme
        widgetZebra: { css: ["ui-widget-content", "ui-state-default"] },
    
        // *** prevent text selection in header ***
        cancelSelection : true,
    
        // *** send messages to console ***
        debug : true
    });
            </script>
           
            </head>
            <body>
            <table id='top-table' class='tablesorter'><thead><tr>
                <th class='top-header-cell col1'>CONTROL_ID</th>
                <th class='top-header-cell col2'>STATUS</th>
                <th class='top-header-cell col3'>OWNER</th>
                <th class='top-header-cell col4'>FILE_NAME</th>
                <th class='top-header-cell col5'>LINE_CNT</th>
                <th class='top-header-cell col6'>BYTE_CNT</th>
                <th class='top-header-cell col7'>CREATE_DT</th>
                <th class='top-header-cell col8'>CHKSUM</th>
                <th class='top-header-cell col9'>TXN</th>
                <th class='top-header-cell col10'>ORG FName</th>
                <th class='top-header-cell col11'>File Dir</th>
                <th class='top-header-cell col12'>INT_EXT_FLG</th>
                <th class='top-header-cell col13'>PID</th>
                <th class='top-header-cell col14'>PRIORITY</th>
                <th class='top-header-cell col15'>OSTIMESTAMP</th>
                <th class='top-header-cell col16'>FINAL_TBL_NAME</th>
                <th class='top-header-cell col17'>CNSL_FILE_MV_LU_ID</th>
                <th class='top-header-cell col18'>Last Name</th>
                <th class='top-header-cell col19'>Delimiter</th>
                <th class='top-header-cell col20'>NO_TOKENS</th>
                <th class='top-header-cell col21'>Min Length</th>
                <th class='top-header-cell col22'>Max Length</th>
                <th class='top-header-cell col23'>Head Data</th>
                <th class='top-header-cell col24'>Tail Data</th>
                </tr></thead><tbody id='scroll_me'>
    <tr>
        <td class='top-body-cell col1'><div>55017</div></td>
        <td class='top-body-cell col2'><div>30</div></td>
        <td class='top-body-cell col3'><div>P</div></td>
        <td class='top-body-cell col4'><div>P.xxx.rcb.tda.out</div></td>
        <td class='top-body-cell col5'><div>9345</div></td>
        <td class='top-body-cell col6'><div>9167445</div></td>
        <td class='top-body-cell col7'><div>27-SEP-13</div></td>
        <td class='top-body-cell col8'><div>3032901441</div></td>
        <td class='top-body-cell col9'><div>TDA</div></td>
        <td class='top-body-cell col10'><div>rcb.tda.out.pgp</div></td>
        <td class='top-body-cell col11'><div></div></td>
        <td class='top-body-cell col12'><div></div></td>
        <td class='top-body-cell col13'><div>30767</div></td>
        <td class='top-body-cell col14'><div></div></td>
        <td class='top-body-cell col15'><div>Sep 27 14:10</div></td>
        <td class='top-body-cell col16'><div></div></td>
        <td class='top-body-cell col17'><div>1016</div></td>
        <td class='top-body-cell col18'><div>aLoader</div></td>
        <td class='top-body-cell col19'><div></div></td>
        <td class='top-body-cell col20'><div>1</div></td>
        <td class='top-body-cell col21'><div>980</div></td>
        <td class='top-body-cell col22'><div>980</div></td>
        <td class='top-body-cell col23'><div>02/07/201302</div></td>
        <td class='top-body-cell col24'><div>02/07/201302</div></td>
    </tr>
    <tr>
        <td class='top-body-cell col1'><div>59384</div></td>
        <td class='top-body-cell col2'><div>90</div></td>
        <td class='top-body-cell col3'><div>BO006</div></td>
        <td class='top-body-cell col4'><div>BOT6.59384.xxx.HHtoRM201102.csv</div></td>
        <td class='top-body-cell col5'><div>2649531</div></td>
        <td class='top-body-cell col6'><div>145838345</div></td>
        <td class='top-body-cell col7'><div>18-SEP-13</div></td>
        <td class='top-body-cell col8'><div>2054991495</div></td>
        <td class='top-body-cell col9'><div></div></td>
        <td class='top-body-cell col10'><div></div></td>
        <td class='top-body-cell col11'><div></div></td>
        <td class='top-body-cell col12'><div></div></td>
        <td class='top-body-cell col13'><div></div></td>
        <td class='top-body-cell col14'><div></div></td>
        <td class='top-body-cell col15'><div></div></td>
        <td class='top-body-cell col16'><div></div></td>
        <td class='top-body-cell col17'><div></div></td>
        <td class='top-body-cell col18'><div></div></td>
        <td class='top-body-cell col19'><div></div></td>
        <td class='top-body-cell col20'><div></div></td>
        <td class='top-body-cell col21'><div></div></td>
        <td class='top-body-cell col22'><div></div></td>
        <td class='top-body-cell col23'><div></div></td>
        <td class='top-body-cell col24'><div></div></td>
    </tr>
    <tr>
        <td class='top-body-cell col1'><div>59385</div></td>
        <td class='top-body-cell col2'><div>90</div></td>
        <td class='top-body-cell col3'><div>BO</div></td>
        <td class='top-body-cell col4'><div>BO938v</div></td>
        <td class='top-body-cell col5'><div>2655733</div></td>
        <td class='top-body-cell col6'><div>146178874</div></td>
        <td class='top-body-cell col7'><div>18-SEP-13</div></td>
        <td class='top-body-cell col8'><div>2165512106</div></td>
        <td class='top-body-cell col9'><div></div></td>
        <td class='top-body-cell col10'><div></div></td>
        <td class='top-body-cell col11'><div></div></td>
        <td class='top-body-cell col12'><div></div></td>
        <td class='top-body-cell col13'><div></div></td>
        <td class='top-body-cell col14'><div></div></td>
        <td class='top-body-cell col15'><div></div></td>
        <td class='top-body-cell col16'><div></div></td>
        <td class='top-body-cell col17'><div></div></td>
        <td class='top-body-cell col18'><div></div></td>
        <td class='top-body-cell col19'><div></div></td>
        <td class='top-body-cell col20'><div></div></td>
        <td class='top-body-cell col21'><div></div></td>
        <td class='top-body-cell col22'><div></div></td>
        <td class='top-body-cell col23'><div></div></td>
        <td class='top-body-cell col24'><div></div></td>
    </tr>
    
           
        </tbody></table>   
            </body></html>
    HTML:
     
    illectronic, Oct 3, 2013 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    You're lacking the following in front of, and after your tablesorter script:
    <script>
    $(document).ready(function() {
    //code goes here
    });
    </script>

    You need to "start" jQuery for it to work.
     
    PoPSiCLe, Oct 3, 2013 IP
  3. illectronic

    illectronic Greenhorn

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    Thanks,

    DO you happen to know how to get this to work with 2 tables on the same page seamlessly. I tried specifying different id's but the header for the second table uses the first tables header.

    Here is is the js:

    <script type="text/javascript">
    $(function(){
    
      // **********************************
      //  Description of ALL pager options
      // **********************************
      var pagerOptions = {
    
        // target the pager markup - see the HTML block below
        container: $(".pager"),
    
        // use this url format "http:/mydatabase.com?page={page}&size={size}&{sortList:col}"
        ajaxUrl: null,
    
        // modify the url after all processing has been applied
        customAjaxUrl: function(table, url) { return url; },
    
        // process ajax so that the data object is returned along with the total number of rows
        // example: { "data" : [{ "ID": 1, "Name": "Foo", "Last": "Bar" }], "total_rows" : 100 }
        ajaxProcessing: function(ajax){
          if (ajax && ajax.hasOwnProperty('data')) {
            // return [ "data", "total_rows" ];
            return [ ajax.total_rows, ajax.data ];
          }
        },
    
        // output string - default is '{page}/{totalPages}'
        // possible variables: {page}, {totalPages}, {filteredPages}, {startRow}, {endRow}, {filteredRows} and {totalRows}
        output: '{startRow} to {endRow} ({totalRows})',
    
        // apply disabled classname to the pager arrows when the rows at either extreme is visible - default is true
        updateArrows: true,
    
        // starting page of the pager (zero based index)
        page: 0,
    
        // Number of visible rows - default is 10
        size: 10,
    
        // if true, the table will remain the same height no matter how many records are displayed. The space is made up by an empty
        // table row set to a height to compensate; default is false
        fixedHeight: true,
    
        // remove rows from the table to speed up the sort of large tables.
        // setting this to false, only hides the non-visible rows; needed if you plan to add/remove rows with the pager enabled.
        removeRows: false,
    
        // css class names of pager arrows
        cssNext: '.next', // next page arrow
        cssPrev: '.prev', // previous page arrow
        cssFirst: '.first', // go to first page arrow
        cssLast: '.last', // go to last page arrow
        cssGoto: '.gotoPage', // select dropdown to allow choosing a page
    
        cssPageDisplay: '.pagedisplay', // location of where the "output" is displayed
        cssPageSize: '.pagesize', // page size selector - select dropdown that sets the "size" option
    
        // class added to arrows when at the extremes (i.e. prev/first arrows are "disabled" when on the first page)
        cssDisabled: 'disabled', // Note there is no period "." in front of this class name
        cssErrorRow: 'tablesorter-errorRow' // ajax error information row
    
      };
    
      $("#top-table")
    
        // Initialize tablesorter
        // ***********************
        .tablesorter({
          theme: 'blue',
          widthFixed: true,
          widgets: ['zebra','stickyHeaders'],
          widgetOptions: {
    
          // css class name applied to the sticky header row (tr)
          stickyHeaders : 'tablesorter-stickyHeader',
    
          // adding zebra striping, using content and default styles - the ui css removes the background from default
          // even and odd class names included for this demo to allow switching themes
          zebra  : ["ui-widget-content even", "ui-state-default odd"],
    
          // use uitheme widget to apply defauly jquery ui (jui) class names
          // see the uitheme demo for more details on how to change the class names
          uitheme : 'jui',
         
          /* scroller_height : 300,
          scroller_barWidth : 17,
          scroller_jumpToHeader: true,
          scroller_idPrefix : 's_' */
    
        }
         
        })
    
        // bind to pager events
        // *********************
        .bind('pagerChange pagerComplete pagerInitialized pageMoved', function(e, c){
          var msg = '"</span> event triggered, ' + (e.type === 'pagerChange' ? 'going to' : 'now on') +
            ' page <span class="typ">' + (c.page + 1) + '/' + c.totalPages + '</span>';
          $('#display')
            .append('<li><span class="str">"' + e.type + msg + '</li>')
            .find('li:first').remove();
        })
    
        // initialize the pager plugin
        // ****************************
        .tablesorterPager(pagerOptions);
    
        // Add two new rows using the "addRows" method
        // the "update" method doesn't work here because not all rows are
        // present in the table when the pager is applied ("removeRows" is false)
        // ***********************************************************************
        var r, $row, num = 50,
          row = '<tr><td>Student{i}</td><td>{m}</td><td>{g}</td><td>{r}</td><td>{r}</td><td>{r}</td><td>{r}</td><td><button type="button" class="remove" title="Remove this row">X</button></td></tr>' +
            '<tr><td>Student{j}</td><td>{m}</td><td>{g}</td><td>{r}</td><td>{r}</td><td>{r}</td><td>{r}</td><td><button type="button" class="remove" title="Remove this row">X</button></td></tr>';
        $('button:contains(Add)').click(function(){
          // add two rows of random data!
          r = row.replace(/\{[gijmr]\}/g, function(m){
            return {
              '{i}' : num + 1,
              '{j}' : num + 2,
              '{r}' : Math.round(Math.random() * 100),
              '{g}' : Math.random() > 0.5 ? 'male' : 'female',
              '{m}' : Math.random() > 0.5 ? 'Mathematics' : 'Languages'
            }[m];
          });
          num = num + 2;
          $row = $(r);
          $('table')
            .find('tbody').append($row)
            .trigger('addRows', [$row]);
          return false;
        });
    
        // Delete a row
        // *************
        $('table').delegate('button.remove', 'click' ,function(){
          var t = $('table');
          // disabling the pager will restore all table rows
          // t.trigger('disable.pager');
          // remove chosen row
          $(this).closest('tr').remove();
          // restore pager
          // t.trigger('enable.pager');
          t.trigger('update');
          return false;
        });
    
        // Destroy pager / Restore pager
        // **************
        $('button:contains(Destroy)').click(function(){
          // Exterminate, annhilate, destroy! http://www.youtube.com/watch?v=LOqn8FxuyFs
          var $t = $(this);
          if (/Destroy/.test( $t.text() )){
            $('table').trigger('destroy.pager');
            $t.text('Restore Pager');
          } else {
            $('table').tablesorterPager(pagerOptions);
            $t.text('Destroy Pager');
          }
          return false;
        });
    
        // Disable / Enable
        // **************
        $('.toggle').click(function(){
          var mode = /Disable/.test( $(this).text() );
          $('table').trigger( (mode ? 'disable' : 'enable') + '.pager');
          $(this).text( (mode ? 'Enable' : 'Disable') + 'Pager');
          return false;
        });
        $('table').bind('pagerChange', function(){
          // pager automatically enables when table is sorted.
          $('.toggle').text('Disable Pager');
        });
    
    });
    
    
    //$( "#pager" ).pager({ top:10px});
            </script>
           
           
    <script type="text/javascript">       
        $(function(){   
            $("#log-table").tablesorter({
          theme: 'blue',
          widthFixed: true,
          widgets: ['scroller'],
          widgetOptions: {
    
          // css class name applied to the sticky header row (tr)
    
          // adding zebra striping, using content and default styles - the ui css removes the background from default
          // even and odd class names included for this demo to allow switching themes
          // use uitheme widget to apply defauly jquery ui (jui) class names
          // see the uitheme demo for more details on how to change the class names
         
          scroller_height : 300,
          scroller_barWidth : 17,
          scroller_jumpToHeader: true,
          scroller_idPrefix : 's_'
    
        }
         
        })
       
        });
           
        </script>   
            
    Code (markup):
     
    illectronic, Oct 4, 2013 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    I'm a little unsure on how you're reusing the same header for both tables - could you post the complete HTML as well? Normally, specifying IDs should be enough for the tablesorter-function.
     
    PoPSiCLe, Oct 4, 2013 IP
  5. illectronic

    illectronic Greenhorn

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    I ended up changing scripts to datatables which works very well!
     
    illectronic, Oct 5, 2013 IP