How to load using string array?

How to load using string array?

rusumatrusumat Posts: 10Questions: 5Answers: 1
edited December 2017 in Free community support

Hi, I living a problem when data load using string array.
My codes;

var agendaSplit = "Row 1[&]Row 2[&]Row 3[&]Row 4[&]Row 5".split('[&]');
$('#AgendaListTable').DataTable({
        "info": true,
        dom: 'Bfrtip',
        buttons: ['excel', 'pdf', 'print'],
        data: agendaSplit,
        language: {
            "url": "//cdn.datatables.net/plug-ins/1.10.15/i18n/English.json",
            buttons: {
            }
        }
    });

Result;

I don't want to this.

I want this but I can't;

Can you help me?

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    Answer ✓

    var agendaSplit = "Row 1[&]Row 2[&]Row 3[&]Row 4[&]Row 5".split('[&]');

    Results in this:
    ["Row 1", "Row 2", "Row 3", "Row 4", "Row 5"]

    But what you really need is for each row to be in its own array. Something more like this:
    [["Row 1"], ["Row 2"], ["Row 3"], ["Row 4"], ["Row 5"]]

    Kevin

  • rusumatrusumat Posts: 10Questions: 5Answers: 1

    @kthorngren thank you for the answer. I solved in following way;

    var agendaSplit = "Row 1[&]Row 2[&]Row 3[&]Row 4[&]Row 5".split('[&]');
        var model = [];
        for (i = 0; i < agendaSplit.length; i++){
            model.push([agendaSplit[i], _removebutton]);
        }
        return $('#AgendaListTable').DataTable({
            "info": true,
            dom: 'Bfrtip',
            buttons: ['excel', 'pdf', 'print'],
            data: model,
            language: {
                "url": "//cdn.datatables.net/plug-ins/1.10.15/i18n/English.json",
                buttons: {
                }
            }
        });
    

    Finally, yes, we solved my problem but I wonder is there another way? I wonder is there a faster solution?

  • rusumatrusumat Posts: 10Questions: 5Answers: 1

    For example 'for' instead of what else can be used?

  • allanallan Posts: 63,461Questions: 1Answers: 10,466 Site admin
    Answer ✓

    You could use Array.prototype.map and a fat arrow function:

    "...".split('[&]').map( x => [x] );
    

    Or old style:

    "...".split('[&]').map( function ( x ) {
      return [ x ];
    } );
    

    Allan

This discussion has been closed.