質問

I've got the following code:

$('#sample_1').dataTable({
    "aoColumns": [
      { "bSortable": true },
      null,
      null,
      null,
      { "bSortable": false }
    ],
    "aLengthMenu": [
        [10, 30, 100, -1],
        [10, 30, 100, "Alle"] // change per page values here
    ],
    // set the initial value
    "iDisplayLength": 30,
    "sPaginationType": "bootstrap",
    "oLanguage": {
        "sLengthMenu": "_MENU_ Einträge",
        "sSearch": "Suche: ",
        "sInfo": "Einträge _START_ bis _END_ von insgesamt _TOTAL_ angezeigt",
        "oPaginate": {
            "sPrevious": "Vorherige",
            "sNext": "Nächste"
        }
    }
});

And I want to reuse some of the parameters in other tables, and thus assign it to a variable. I tried:

myVar = {// set the initial value
    "iDisplayLength": 30,
    "sPaginationType": "bootstrap",
    "oLanguage": {
        "sLengthMenu": "_MENU_ Einträge",
        "sSearch": "Suche: ",
        "sInfo": "Einträge _START_ bis _END_ von insgesamt _TOTAL_ angezeigt",
        "oPaginate": {
            "sPrevious": "Vorherige",
            "sNext": "Nächste"
        }
    }};
$('#sample_1').dataTable({
    "aoColumns": [
      { "bSortable": true },
      null,
      null,
      null,
      { "bSortable": false }
    ],
    "aLengthMenu": [
        [10, 30, 100, -1],
        [10, 30, 100, "Alle"] // change per page values here
    ],
    myVar
});

but this doesnt work. Can you please give me some advise? Tried googleing, but don't know how this contruct is being called, aren't those simple arrays are they? EDIT: Seem to be some sort of objects, but how to actrually pass the attributes of this object then as a parameter to dataTable?

Thanks a lot

役に立ちましたか?

解決

You're almost there.

You are trying to assign 3 parameters using one object.

Something like this will do the trick.

var myVar = {
    "iDisplayLength": 30,
    "sPaginationType": "bootstrap",
    "oLanguage": {
        "sLengthMenu": "_MENU_ Einträge",
        "sSearch": "Suche: ",
        "sInfo": "Einträge _START_ bis _END_ von insgesamt _TOTAL_ angezeigt",
        "oPaginate": {
            "sPrevious": "Vorherige",
            "sNext": "Nächste"
        }
    }
};

$('#sample_1').dataTable({
    "aoColumns": [
      { "bSortable": true },
      null,
      null,
      null,
      { "bSortable": false }
    ],
    "aLengthMenu": [
        [10, 30, 100, -1],
        [10, 30, 100, "Alle"] // change per page values here
    ],
    "iDisplayLength": myVar.iDisplayLength,
    "sPaginationType" myVar.sPaginationType,
    "oLanguage": myVar.oLanguage
});

他のヒント

You will need 3 variables for this as there are 3 separate values (parallel):

1// "iDisplayLength": 30,
   2// "sPaginationType": "bootstrap",
   3// "oLanguage": {
        "sLengthMenu": "_MENU_ Einträge",
        "sSearch": "Suche: ",
        "sInfo": "Einträge _START_ bis _END_ von insgesamt _TOTAL_ angezeigt",
        "oPaginate": {
            "sPrevious": "Vorherige",
            "sNext": "Nächste"
        }

I think you are confused by all those curly braces, but you are on the right track.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top