Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. function sortTable1() {
  2.  
  3. $('th').click(function () {
  4. var table = $(this).parents('table').eq(0)
  5. var rows = table.find("tr:not(:has('th'))").toArray().sort(comparer($(this).index()))
  6. this.asc = !this.asc
  7. if (!this.asc) { rows = rows.reverse() }
  8. for (var i = 0; i < rows.length; i++) { table.append(rows[i]) }
  9. })
  10. function comparer(index) {
  11. return function (a, b) {
  12. var valA = getCellValue(a, index), valB = getCellValue(b, index)
  13. return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : valA.localeCompare(valB)
  14. }
  15. }
  16. function getCellValue(row, index) { return $(row).children('td').eq(index).html() }
  17.  
  18. // additional code to apply a filter
  19. $('table').each(function () {
  20. var table = $(this)
  21. var headers = table.find('th').length
  22. var filterrow = $('<tr>').insertAfter($(this).find('th:last()').parent())
  23. for (var i = 0; i < headers; i++) {
  24. filterrow.append($('<th>').append($('<input>').attr('type', 'text').keyup(function () {
  25. table.find('tr').show()
  26. filterrow.find('input[type=text]').each(function () {
  27. var index = $(this).parent().index() + 1
  28. var filter = $(this).val() != ''
  29. $(this).toggleClass('filtered', filter)
  30. if (filter) {
  31. var el = 'td:nth-child(' + index + ')'
  32. var criteria = ":contains('" + $(this).val() + "')"
  33. table.find(el + ':not(' + criteria + ')').parent().hide()
  34. }
  35. })
  36. })))
  37. }
  38. filterrow.append($('<th>').append($('<input>').attr('type', 'button').val('Clear Filter').click(function () {
  39. $(this).parent().parent().find('input[type=text]').val('').toggleClass('filtered', false)
  40. table.find('tr').show()
  41. })))
  42. })
  43. }
  44.  
  45. Here is the table structure
  46. <table id="test" class="master_table test_1 table no-border hover">
  47. <thead class="no-border">
  48. <tr>
  49. <th>
  50. </th>
  51. <th>
  52. </th>
  53. <th>
  54. </th>
  55. <th>
  56. </th>
  57. <th class="sortable1">
  58. Name
  59. </th>
  60. <th class="text-center ">
  61. Status
  62. </th>
  63. <th>
  64. Date
  65. </th>
  66. <th id="nm" class="text-center">
  67. Hrs
  68. </th>
  69. </tr>
  70. </thead>
  71.  
  72. </table>
  73. but Error is : Cannot read property 'localeCompare' of undefined ,please help me solve this Ascending and Descending order.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement