0

Below is snippet of table-ajax.js file from DataTable module. My question is what will be Ajax url instead of table_ajax.php if I use IndexAction{} in my Controller. I want to display data from database.

    var handleRecords = function () {

    var grid = new Datatable();

    grid.init({
        src: $("#datatable_ajax"),
        onSuccess: function (grid) {
        },
        onError: function (grid) {
        },
        onDataLoad: function(grid) {
        },
        loadingMessage: 'Loading...',
        dataTable: { 

            "bStateSave": true, // save datatable state(pagination, sort, etc) in cookie.

            "lengthMenu": [
                [10, 20, 50, 100, 150, -1],
                [10, 20, 50, 100, 150, "All"] // change per page values here
            ],
            "pageLength": 10, // default record count per page
            "ajax": {
                "url": "table_ajax.php", // ajax source
            },
            "order": [
                [1, "asc"]
            ]// set first column as a default sort by asc
        }
    });

1 Answer 1

1

Firstly you'll need to add a strategy to your viewmanager so you can return json. This is done within your module.config like so:

'view_manager' => array(
...
   'strategies' => array(
       'ViewJsonStrategy'
   ),
...
),

I'll just assume you have this DataTableModule imported via your Autoloader. So you can extend or implement the "table_ajax.php" within your controller and override/add a return value as a zf2 JsonModel.

In your Action within your Controller that extends/implements ajax_table.php

...
return new JsonModel($whateverTheResultIs);
...

Now that you have a controller that returns your JSON you'll require to set up a route. This is pretty straight forward it only links to your controller action and since you defined a strategy in your Viewmanager ZF2 does the rest of the pesky ajax stuff for you.

Edit: Rather then extending the table_ajax.php in your controller it probably is a good idea to write a Service which retrieves the information from table_ajax.php.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.