neil860208
neil860208

Reputation: 167

Need help getting multiple tables working on a single page using simple data grid

I am attempting to use "simple data grid" to display multiple unique (tables) grids on a single page. In the code below I am unclear how to pass unique data to the function below instead of var data = ExampleData.fruits. Every attempt I have made so far to add a second table with unique data results in all data being broken. (I do need all the existing functionality sorting highlighting on click, pagination etc).

I have created this jsfiddle that is working with a single grid and the sample data. I have commented out a bit of code for a second set of data that I was trying to work with.

ExampleData = {};
ExampleData.handleMockjaxResponse = function(settings) {
var uri = new Uri(settings.url);
var page = uri.getQueryParamValue('page') || 1;
var order_by = uri.getQueryParamValue('order_by');
var sortorder = uri.getQueryParamValue('sortorder');
var rows_per_page = 6;
var start_index = (page - 1) * rows_per_page;
var total_pages = 1;
var data = ExampleData.fruits; //How can I pass this data so that multiple tables can exist with different data
if (data.length != 0) {
    total_pages = parseInt((data.length - 1) / rows_per_page) + 1;
}
if (order_by) {
    data.sort(function(left, right) {
        var a = left[order_by];
        var b = right[order_by];

        if (sortorder == 'desc') {
            var c = b;
            b = a;
            a = c;
        }
        if (a < b) {
            return -1;
        }
        else if (a > b) {
            return 1;
        }
        else {
            return 0;
        }
    });
}

Upvotes: 2

Views: 212

Answers (1)

slashingweapon
slashingweapon

Reputation: 11317

The root of your problem is that you are trying to set up two mockjacks on the same url ('*'). You can't have them both at the same time.

If you need to replace a mockjack for a given URL, you need to get rid of the old one first:

$.mockjaxClear();
$.mockjax({
    url: '*',
    response: ... // whatever
});

But that still won't solve your problem completely. By clearing the first mockjack before installing the second one, I was able to get both of the data grids to display. But trying to navigate the #grid table resulted in some interesting errors (of course) because the mockjack that is supposed to provide the data for that #grid had been removed when we set up #grid2.

Here is what you should do:

  • Use a different URL for each table
  • Set up different mockjacks for each URL

I forked your fiddle with a solution. It uses a data specifier to indicate which data set to use, and a traditional lambda function for the data response callback.

function generateMockjackResponse(dataIn) {
    return function(settings) {
        var uri = new Uri(settings.url);
        var page = uri.getQueryParamValue('page') || 1;
        var order_by = uri.getQueryParamValue('order_by');
        var sortorder = uri.getQueryParamValue('sortorder');

        var rows_per_page = 6;
        var start_index = (page - 1) * rows_per_page;

        var total_pages = 1;
        var data = dataIn;

        if (data.length != 0) {
            total_pages = parseInt((data.length - 1) / rows_per_page) + 1;
        }

        if (order_by) {
            data.sort(function(left, right) {
                var a = left[order_by];
                var b = right[order_by];

                if (sortorder == 'desc') {
                    var c = b;
                    b = a;
                    a = c;
                }

                if (a < b) {
                    return -1;
                }
                else if (a > b) {
                    return 1;
                }
                else {
                    return 0;
                }
            });
        }
        var result = {
            total_pages: total_pages,
            rows: data.slice(start_index, start_index + rows_per_page)
        };
        this.responseText = result;
    };
}

var data1 = [
    // some data
];

var data2 = [ 
    // other data 
];

$.mockjax({
    url: '*',
    data: 1,
    response: generateMockjackResponse(data1)
});

$('#grid1').simple_datagrid({
    order_by: true,
    data: 1
});

$.mockjax({
    url: '*',
    data: 2,
    response: generateMockjackResponse(data1)
});

$('#grid2').simple_datagrid({
    order_by: true,
    data: 2
});

Upvotes: 1

Related Questions