2

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;
        }
    });
}
4

1 回答 1

1

您的问题的根源是您试图在同一个 url ( '*') 上设置两个 mockjacks。你不能同时拥有它们。

如果您需要为给定的 URL 替换 mockjack,您需要首先摆脱旧的:

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

但这仍然不能完全解决您的问题。通过在安装第二个模型之前清除第一个模型,我能够显示两个数据网格。但是尝试导航#grid表格会导致一些有趣的错误(当然),因为应该提供数据的 mockjack#grid在我们设置时已被删除#grid2

这是你应该做的:

  • 为每个表使用不同的 URL
  • 为每个 URL 设置不同的 mockjacks

我用一个解决方案叉了你的小提琴。它使用数据说明符来指示要使用的数据集,以及用于数据响应回调的传统 lambda 函数。

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
});
于 2012-11-16T00:48:55.190 回答