{"id":14974229,"url":"https://github.com/MongoDataTables/mongo-datatables","last_synced_at":"2025-10-27T06:32:06.514Z","repository":{"id":57442936,"uuid":"103536868","full_name":"pjosols/mongo-datatables","owner":"pjosols","description":"A package for using the jQuery plug-in DataTables server-side processing (and DataTables Editor) with MongoDB.","archived":false,"fork":false,"pushed_at":"2019-11-19T05:17:37.000Z","size":28,"stargazers_count":14,"open_issues_count":1,"forks_count":7,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-01T03:22:43.595Z","etag":null,"topics":["datatables","django","flask","mongodb","python"],"latest_commit_sha":null,"homepage":"","language":"Python","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/pjosols.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-14T13:39:22.000Z","updated_at":"2023-05-29T21:45:00.000Z","dependencies_parsed_at":"2022-09-04T22:21:05.060Z","dependency_job_id":null,"html_url":"https://github.com/pjosols/mongo-datatables","commit_stats":null,"previous_names":["pauljolsen/mongo-datatables"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pjosols%2Fmongo-datatables","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pjosols%2Fmongo-datatables/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pjosols%2Fmongo-datatables/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pjosols%2Fmongo-datatables/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pjosols","download_url":"https://codeload.github.com/pjosols/mongo-datatables/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238455104,"owners_count":19475380,"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":["datatables","django","flask","mongodb","python"],"created_at":"2024-09-24T13:50:15.152Z","updated_at":"2025-10-27T06:32:01.228Z","avatar_url":"https://github.com/pjosols.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"================\nmongo-datatables\n================\nA script for using the jQuery plug-in DataTables server-side processing (and DataTables Editor) with MongoDB.\n\nWorks with Flask and Django. Supports column sorting and filtering by multiple search terms and/or column specific\nsearches like column:keyword.\n\n\n|Downloads|\n\n.. |Downloads| image:: http://pepy.tech/badge/mongo-datatables\n   :target: http://pepy.tech/project/mongo-datatables\n\n----\n\nExamples\n========\n- https://github.com/pauljolsen/django-and-mongo-datatables\n- https://github.com/pauljolsen/flask-and-mongo-datatables\n\n\nInstall\n=======\nYou can install with pip::\n\n    pip install mongo-datatables\n\n..\n\nBasic Usage (Flask)\n===================\n\nIn your ``views.py``::\n\n    import json\n    from flask import request, render_template\n    from mongo_datatables import DataTables\n    from app import mongo\n    from . import main\n\n\n    @main.route('/table-view')\n    def table_view():\n        return render_template('main/table_view.html')\n\n\n    @main.route('/mongo/\u003ccollection\u003e')\n    def api_db(collection):\n        request_args = json.loads(request.values.get(\"args\"))\n        results = DataTables(mongo, collection, request_args).get_rows()\n        return json.dumps(results)\n\n\n..\n\nIn your ``table_view.html``::\n\n    {% extends \"base.html\" %}\n\n\n    {% block content %}\n        {{ super() }}\n\n        \u003cdiv class=\"container\"\u003e\n\n            \u003ch1\u003e\n                Contracts\n            \u003c/h1\u003e\n\n            \u003ctable id=\"dt_table\" class=\"table table-striped table-responsive\"\u003e\n                \u003cthead\u003e\n                \u003ctr\u003e\n                    \u003cth\u003eExpiryDate\u003c/th\u003e\n                    \u003cth\u003eContractId\u003c/th\u003e\n                    \u003cth\u003eVendor\u003c/th\u003e\n                    \u003cth\u003eNote\u003c/th\u003e\n                \u003c/tr\u003e\n                \u003c/thead\u003e\n            \u003c/table\u003e\n\n\n        \u003c/div\u003e\n    {% endblock %}\n\n    {% block scripts %}\n        {{ super() }} // DataTables, jQuery, Bootstrap loaded here\n\n        \u003cscript\u003e\n            $(function () {\n                $('#dt_table').DataTable({\n                    serverSide: true,\n                    ajax: {\n                        url: '{{ url_for('main.api_db', collection='contracts') }}',\n                        dataSrc: 'data',\n                        type: 'GET',\n                        data: function (args) {\n                            //args.qString = getQuerystring(); //add in querystring args, or anything else you want\n                            return {\n                                \"args\": JSON.stringify(args)\n                            };\n                        }\n                    },\n                    columns: [\n                        {data: 'ExpiryDate'},\n                        {data: 'ContractId'},\n                        {data: 'Vendor'},\n                        {data: 'Note'}\n                    ]\n                });\n\n            });\n\n            // in case you want to pass the querystring along with the request\n            function getQuerystring() {\n                var $qItems = $('#qItems');\n                $qItems.empty();\n                var hash;\n                var filters = {};\n                var q = document.URL.split('?')[1];\n                if (q != undefined) {\n                    q = q.split('\u0026');\n                    for (var i = 0; i \u003c q.length; i++) {\n                        hash = q[i].split('=');\n                        filters[hash[0]] = hash[1];\n                    }\n                }\n                return filters\n            }\n        \u003c/script\u003e\n\n    {% endblock %}\n\n..\n\nAdvanced Usage, With A Custom Filter (Flask)\n============================================\n\nIn your ``views.py``::\n\n    import json\n    from datetime import datetime, timedelta\n    from mongo_datatables import Editor, DataTables\n    from flask import request\n    from app import mongo\n    from . import main\n\n\n    @main.route('/support-expiry', methods=['GET'])\n    def support_expiry():\n        \"\"\"This examples receives a 'daysToExpiry' value and translates it to an Expiration Date, which can be looked\n        up in the Mongo collection.\n        \"\"\"\n\n        request_args = json.loads(request.values.get(\"args\"))\n        custom_filter = {}\n\n        # translate daysToExpiry into a filter for the ExpiryDate Mongo key\n        if 'daysToExpiry' in request_args['qString']:\n            days_to_expiry = request_args['qString'].pop('daysToExpiry', None)  # remove daysToExpiry, leave the rest\n            t = datetime.utcnow()\n            ts = t.strftime(\"%Y-%m-%d\")\n            if days_to_expiry == 'Expired':\n                custom_filter.update({\n                    'ExpiryDate': {'$lt': ts, '$ne': ''}  # ExpiryDate is before today but not equal to ''\n                })\n            else:\n                d = t + timedelta(days=int(days_to_expiry))\n                ds = d.strftime(\"%Y-%m-%d\")\n                custom_filter.update({\n                    'ExpiryDate': {'$gt': ts, '$lt': ds}  # ExpiryDate is between now and daysToExpiry from now\n                })\n\n        # add the rest of the query string to the custom filter\n        custom_filter.update(request_args['qString'])\n\n        collection = 'HardwareInventory'\n        results = DataTables(mongo, collection, request_args, **custom_filter).get_rows()\n        return json.dumps(results)\n\n..\n\n\nDataTables Editor Usage (Flask)\n===============================\n\nIn your ``views.py``::\n\n    import json\n    from flask import request\n    from mongo_datatables import DataTables, Editor\n    from . import main\n    from app import mongo\n\n    # include the table_view and api_db views from above\n\n    @main.route('/mongo/edit/\u003ccollection\u003e/\u003cdoc_id\u003e', methods=['POST'])\n    def api_editor(collection, doc_id):\n        request_args = json.loads(request.values.get(\"args\"))\n        results = Editor(mongo, collection, request_args, doc_id).update_rows()\n        return json.dumps(results)\n\n..\n\nIn your ``table-view.html``::\n\n    {% extends \"base.html\" %}\n\n\n    {% block content %}\n        {{ super() }}\n\n        \u003cdiv class=\"container\"\u003e\n\n            \u003ctable id=\"dt_table\" class=\"table table-striped table-responsive\"\u003e\n                \u003cthead\u003e\n                \u003ctr\u003e\n                    \u003cth\u003eExpiryDate\u003c/th\u003e\n                    \u003cth\u003eContractId\u003c/th\u003e\n                    \u003cth\u003eVendor\u003c/th\u003e\n                    \u003cth\u003eNote\u003c/th\u003e\n                \u003c/tr\u003e\n                \u003c/thead\u003e\n            \u003c/table\u003e\n\n\n        \u003c/div\u003e\n    {% endblock %}\n\n    {% block scripts %}\n        {{ super() }}  // DataTables, Editor, jQuery, Bootstrap, Buttons loaded here\n\n        \u003cscript\u003e\n\n            $(function () {\n\n                // DataTables\n                var table = $('#dt_table').DataTable({\n                    serverSide: true,\n                    ajax: {\n                        url: '{{ url_for('main.api_db', collection='contracts') }}',\n                        dataSrc: 'data',\n                        type: 'GET',\n                        data: function (args) {\n                            return {\n                                \"args\": JSON.stringify(args)\n                            };\n                        }\n                    },\n                    select: true,\n                    columns: [\n                        {data: 'ExpiryDate'},\n                        {data: 'ContractId'},\n                        {data: 'Vendor'},\n                        {data: 'Note'}\n                    ]\n                });\n\n                // Editor\n                var editor = new $.fn.dataTable.Editor({\n                    ajax: {\n                        //Editor replaces _id_ with the row ID(s) (the Mongo _id(s))\n                        url: '{{ url_for('main.api_editor', collection='contracts', doc_id='_id_') }}',\n                        type: 'POST',\n                        data: function (args) {\n                            return {\n                                \"args\": JSON.stringify(args)\n                            };\n                        }\n                    },\n                    table: \"#dt_table\",\n                    fields: [\n                        {name: 'ExpiryDate', value: 'Expiry Date'},\n                        {name: 'ContractId', value: 'Contract ID'},\n                        {name: 'Vendor', value: 'Vendor'},\n                        {name: 'Note', value: 'Note'}\n                    ]\n                });\n\n                // Buttons\n                new $.fn.dataTable.Buttons(table, [\n                    {extend: \"create\", editor: editor},\n                    {extend: \"edit\", editor: editor},\n                    {extend: \"remove\", editor: editor}\n                ]);\n\n                table.buttons().container()\n                        .appendTo($(table.table().container(), '.col-sm-6:eq(0)'));\n\n            });\n        \u003c/script\u003e\n\n    {% endblock %}\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMongoDataTables%2Fmongo-datatables","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMongoDataTables%2Fmongo-datatables","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMongoDataTables%2Fmongo-datatables/lists"}