{"id":13519313,"url":"https://github.com/joequery/Stupid-Table-Plugin","last_synced_at":"2025-03-31T12:31:31.584Z","repository":{"id":46170887,"uuid":"4008167","full_name":"joequery/Stupid-Table-Plugin","owner":"joequery","description":"A stupidly small and simple jQuery table sorter plugin","archived":false,"fork":false,"pushed_at":"2021-08-03T09:01:26.000Z","size":313,"stargazers_count":706,"open_issues_count":22,"forks_count":174,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-03-26T09:05:15.312Z","etag":null,"topics":["javascript","jquery","jquery-plugin","sort"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joequery.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-04-12T18:56:24.000Z","updated_at":"2025-02-20T22:21:06.000Z","dependencies_parsed_at":"2022-09-11T10:50:54.570Z","dependency_job_id":null,"html_url":"https://github.com/joequery/Stupid-Table-Plugin","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joequery%2FStupid-Table-Plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joequery%2FStupid-Table-Plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joequery%2FStupid-Table-Plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joequery%2FStupid-Table-Plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joequery","download_url":"https://codeload.github.com/joequery/Stupid-Table-Plugin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246469257,"owners_count":20782696,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","jquery","jquery-plugin","sort"],"created_at":"2024-08-01T05:01:57.122Z","updated_at":"2025-03-31T12:31:31.177Z","avatar_url":"https://github.com/joequery.png","language":"JavaScript","readme":"Stupid jQuery Table Sort\n========================\n\nThis is a stupid jQuery table sorting plugin. Nothing fancy, nothing really\nimpressive. Overall, stupidly simple. Requires jQuery 1.7 or newer.\n\n[View the demo here][0]\n\nSee the examples directory.\n\nInstallation via [npm][2]\n-------------------------\n\n    $ npm i stupid-table-plugin\n\nInstallation via Bower\n----------------------\n\n    $ bower install jquery-stupid-table\n\n\nExample Usage\n-------------\n\nThe JS:\n\n    $(\"table\").stupidtable();\n\nThe HTML:\n\n    \u003ctable\u003e\n      \u003cthead\u003e\n        \u003ctr\u003e\n          \u003cth data-sort=\"int\"\u003eint\u003c/th\u003e\n          \u003cth data-sort=\"float\"\u003efloat\u003c/th\u003e\n          \u003cth data-sort=\"string\"\u003estring\u003c/th\u003e\n        \u003c/tr\u003e\n      \u003c/thead\u003e\n      \u003ctbody\u003e\n        \u003ctr\u003e\n          \u003ctd\u003e15\u003c/td\u003e\n          \u003ctd\u003e-.18\u003c/td\u003e\n          \u003ctd\u003ebanana\u003c/td\u003e\n        \u003c/tr\u003e\n        ...\n        ...\n        ...\n\nThe thead and tbody tags must be used.\n\nAdd a `data-sort` attribute of \"DATATYPE\" to the th elements to make them sortable\nby that data type. If you don't want that column to be sortable, just omit the\n`data-sort` attribute.\n\n\nPredefined data types\n---------------------\n\nOur aim is to keep this plugin as lightweight as possible. Consequently, the\nonly predefined datatypes that you can pass to the th elements are\n\n* `int`\n* `float`\n* `string` (case-sensitive)\n* `string-ins` (case-insensitive)\n\nThese data types will be sufficient for many simple tables. However, if you need\ndifferent data types for sorting, you can easily create your own!\n\nData with multiple representations/predefined order\n---------------------------------------------------\n\nStupid Table lets you sort a column by computer friendly values while displaying\nhuman friendly values via the `data-sort-value` attribute on a td element. For\nexample, to sort timestamps (computer friendly) but display pretty formated\ndates (human friendly)\n\n    \u003ctable\u003e\n      \u003cthead\u003e\n        \u003ctr\u003e\n          \u003cth data-sort=\"string\"\u003eName\u003c/th\u003e\n          \u003cth data-sort=\"int\"\u003eBirthday\u003c/th\u003e\n        \u003c/tr\u003e\n      \u003c/thead\u003e\n      \u003ctbody\u003e\n        \u003ctr\u003e\n          \u003ctd\u003eJoe McCullough\u003c/td\u003e\n          \u003ctd data-sort-value=\"672537600\"\u003eApril 25, 1991\u003c/td\u003e\n        \u003c/tr\u003e\n        \u003ctr\u003e\n          \u003ctd\u003eClint Dempsey\u003c/td\u003e\n          \u003ctd data-sort-value=\"416016000\"\u003eMarch 9, 1983\u003c/td\u003e\n        \u003c/tr\u003e\n        ...\n        ...\n        ...\n\nIn this example, Stupid Table will sort the Birthday column by the timestamps\nprovided in the `data-sort-value` attributes of the corresponding tds. Since\ntimestamps are integers, and that's what we're sorting the column by, we specify\nthe Birthday column as an `int` column in the `data-sort` value of the column\nheader.\n\n\nDefault sorting direction\n-------------------------\n\nBy default, columns will sort ascending. You can specify a column to sort \"asc\"\nor \"desc\" first.\n\n    \u003ctable\u003e\n      \u003cthead\u003e\n        \u003ctr\u003e\n            \u003cth data-sort=\"float\" data-sort-default=\"desc\"\u003efloat\u003c/th\u003e\n            ...\n        \u003c/tr\u003e\n      \u003c/thead\u003e\n    \u003c/table\u003e\n\nSorting a column on load\n------------------------\n\nIf you want a specific column to be sorted immediately after\n`$table.stupidtable()` is called, you can provide a `data-sort-onload=yes`\nattribute.\n\n    \u003ctable\u003e\n      \u003cthead\u003e\n        \u003ctr\u003e\n            \u003cth data-sort=\"float\" data-sort-onload=yes\u003efloat\u003c/th\u003e\n            ...\n        \u003c/tr\u003e\n      \u003c/thead\u003e\n    \u003c/table\u003e\n\nMulticolumn sorting\n-------------------\n\nA multicolumn sort allows you to define secondary columns to sort by in the\nevent of a tie with two elements in the sorted column. See [examples/multicolumn-sort.html](https://rawgit.com/joequery/Stupid-Table-Plugin/master/examples/multicolumn-sort.html).\nSpecify a comma-separated list of th identifiers in a `data-sort-multicolumn`\nattribute on a `\u003cth\u003e` element. An identifier can be an integer (which represents\nthe index of the th element of the multicolumn target) or a string (which\nrepresents the id of the th element of the multicolumn target).\n\n  \u003ctable\u003e\n    \u003cthead\u003e\n      \u003ctr\u003e\n        \u003cth id=\"int-column\" data-sort=\"int\" data-sort-multicolumn=\"1,string-column\"\u003eint\u003c/th\u003e\n        \u003cth id=\"float-column\" data-sort=\"float\" data-sort-multicolumn=\"string-column,int-column\"\u003efloat\u003c/th\u003e\n        \u003cth id=\"string-column\" data-sort=\"string\" data-sort-multicolumn=\"1,0\"\u003estring\u003c/th\u003e\n      \u003c/tr\u003e\n    \u003c/thead\u003e\n    \u003ctbody\u003e\n      \u003ctr\u003e\n        \u003ctd\u003e1\u003c/td\u003e\n        \u003ctd\u003e10.0\u003c/td\u003e\n        \u003ctd\u003ea\u003c/td\u003e\n      \u003c/tr\u003e\n      \u003ctr\u003e\n        \u003ctd\u003e1\u003c/td\u003e\n        \u003ctd\u003e10.0\u003c/td\u003e\n        \u003ctd\u003ea\u003c/td\u003e\n      \u003c/tr\u003e\n\nSorting a column programatically\n--------------------------------\n\nAfter you have called `$(\"#mytable\").stupidtable()`, if you wish to sort a\ncolumn without requiring the user to click on it, select the column th and call\n\n\n    var $table = $(\"#mytable\").stupidtable();\n    var $th_to_sort = $table.find(\"thead th\").eq(0);\n    $th_to_sort.stupidsort();\n\n    // You can also force a direction.\n    $th_to_sort.stupidsort('asc');\n    $th_to_sort.stupidsort('desc');\n\nUpdating a table cell's value\n-----------------------------\n\nIf you wish for Stupid Table to respond to changes in the table cell values, you\nmust explicitely inform Stupid Table to update its cache with the new values. If\nyou update the table display/sort values without using this mechanism, your\nnewly updated table **will not sort correctly!**\n\n    /*\n     * Suppose $age_td is some td in a table under a column specified as an int\n     * column. stupidtable() must already be called for this table.\n     */\n    $age_td.updateSortVal(23);\n\nNote that this only changes the internal sort value (whether you specified a\n`data-sort-value` or not). Use the standard jQuery `.text()` / `.html()` methods\nif you wish to change the display values.\n\n\nCallbacks\n---------\n\nTo execute a callback function after a table column has been sorted, you can\nbind on `aftertablesort`.\n\n    var table = $(\"table\").stupidtable();\n    table.bind('aftertablesort', function (event, data) {\n        // data.column - the index of the column sorted after a click\n        // data.direction - the sorting direction (either asc or desc)\n        // data.$th - the th element (in jQuery wrapper)\n        // $(this) - this table object\n\n        console.log(\"The sorting direction: \" + data.direction);\n        console.log(\"The column index: \" + data.column);\n    });\n\nSimilarly, to execute a callback before a table column has been sorted, you can\nbind on `beforetablesort`.\n\nSee the complex_example.html file.\n\nCreating your own data types\n----------------------------\n\nSometimes you don't have control over the HTML produced by the backend. In the\nevent you need to sort complex data without a `data-sort-value` attribute, you\ncan create your own data type. Creating your own data type for sorting purposes\nis easy as long as you are comfortable using custom functions for sorting.\nConsult [Mozilla's Docs][1] if you're not.\n\nLet's create an alphanum datatype for a User ID that takes strings in the form\n\"D10\", \"A40\", and sorts the column based on the numbers in the string.\n\n    \u003cthead\u003e\n      \u003ctr\u003e\n        \u003cth data-sort=\"string\"\u003eName\u003c/th\u003e\n        \u003cth data-sort=\"int\"\u003eAge\u003c/th\u003e\n        \u003cth data-sort=\"alphanum\"\u003eUserID\u003c/th\u003e\n      \u003c/tr\u003e\n    \u003c/thead\u003e\n    \u003ctbody\u003e\n      \u003ctr\u003e\n        \u003ctd\u003eJoseph McCullough\u003c/td\u003e\n        \u003ctd\u003e20\u003c/td\u003e\n        \u003ctd\u003eD10\u003c/td\u003e\n      \u003c/tr\u003e\n      \u003ctr\u003e\n        \u003ctd\u003eJustin Edwards\u003c/td\u003e\n        \u003ctd\u003e29\u003c/td\u003e\n        \u003ctd\u003eA40\u003c/td\u003e\n      \u003c/tr\u003e\n      ...\n      ...\n      ...\n\nNow we need to specify how the **alphanum** type will be sorted. To do that,\nwe do the following:\n\n    $(\"table\").stupidtable({\n      \"alphanum\":function(a,b){\n\n        var pattern = \"^[A-Z](\\\\d+)$\";\n        var re = new RegExp(pattern);\n\n        var aNum = re.exec(a).slice(1);\n        var bNum = re.exec(b).slice(1);\n\n        return parseInt(aNum,10) - parseInt(bNum,10);\n      }\n    });\n\nThis extracts the integers from the cell and compares them in the style\nthat sort functions use.\n\nStupidTable Settings\n--------------------\n\nAs of 1.1.0 settings have been introduced. Settings are defined like so:\n\n    var $table = $(\"#mytable\");\n    $table.stupidtable_settings({\n        // Settings for this table specified here\n    });\n    $table.stupidtable();\n\nListed below are the available settings.\n\n### will_manually_build_table\n\n(Introduced in verison 1.1.1)\n\nOptions:\n\n* `true`\n* `false` (default)\n\nBy default, every time a column is sorted, stupidtable reads the DOM to extract\nall the values from the table. For tables that will not change or for very large\ntables, this behavior may be suboptimal.  To modify this behavior, set the\n`will_manually_build_table` setting to `true`. However, you will be responsible\nfor informing stupidtable that the table has been modified by calling\n`$table.stupidtable_build()`.\n\n    var $table = $(\"#mytable\");\n    $table.stupidtable_settings({\n        will_manually_build_table: true\n    });\n    $table.stupidtable();\n\n    // Make some modification to the table, such as deleting a row\n    ...\n    ...\n\n    // Since will_manually_build_table is true, we must build the table in order\n    // for future sorts to properly handle our modifications.\n    $table.stupidtable_build();\n\n### should_redraw\n\n(Introduced in verison 1.1.0)\n\nThe `should_redraw` setting allows you to specify a function that determines\nwhether or not the table should be redrawn after it has been internally sorted.\n\nThe `should_redraw` function takes a `sort_info` object as an argument. The\nobject keys available are:\n\n*  `column` - An array representing the sorted column. Each element of the array is of the form `[sort_val, $tr, index]`\n*  `sort_dir` - `\"asc\"` or `\"desc\"`\n*  `$th` - The jquery object of the `\u003cth\u003e` element that was clicked\n*  `th_index` - The index of the `\u003cth\u003e` element that was cliked\n*  `$table` - The jquery object of the `\u003ctable\u003e` that contains the `\u003cth\u003e` that was clicked\n*  `datatype` - The datatype of the column\n*  `compare_fn` - The sort/compare function associated with the `\u003cth\u003e` clicked.\n\n**Example**: If you want to prevent stupidtable from redrawing the table if the\ncolumn sorted has all identical values, you would do the following:\n\n    var $table = $(\"#mytable\");\n    $table.stupidtable_settings({\n        should_redraw: function(sort_info){\n          var sorted_column = sort_info.column;\n          var first_val = sorted_column[0];\n          var last_val = sorted_column[sorted_column.length - 1][0];\n\n          // If first and last element of the sorted column are the same, we\n          // can assume all elements are the same.\n          return sort_info.compare_fn(first_val, last_val) !== 0;\n        }\n    });\n    $table.stupidtable();\n\nLicense\n-------\n\nThe Stupid jQuery Plugin is licensed under the MIT license. See the LICENSE\nfile for full details.\n\nTests\n-----\n\nVisit `tests/test.html` in your browser to run the QUnit tests.\n\n\n[0]: http://joequery.github.io/Stupid-Table-Plugin/\n[1]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort\n[2]: https://www.npmjs.com/package/stupid-table-plugin\n\n","funding_links":[],"categories":["JavaScript","Table of contents","Animation"],"sub_categories":["Data Table","jQuery plugins","Tables"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoequery%2FStupid-Table-Plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoequery%2FStupid-Table-Plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoequery%2FStupid-Table-Plugin/lists"}