Is it possible to get Associative arrays by "rows().data().toArray()" ?

Is it possible to get Associative arrays by "rows().data().toArray()" ?

BarbarosBarbaros Posts: 3Questions: 1Answers: 0

Hi,
i inserted rows manually via

 $('#AddRow').on( 'click', function () {
        t.row.add( [
            $('#customer_name').val(),
            $('#customer_tel').val(),
            $('#customer_mail').val(),
        ] ).draw( false );
    }); 

and send table to server and use ajax.

data: {datax :t.rows().data().toArray()},

i got indexed array- its ok . but i want named one- for example with column names .

is it possible ? i tried to set column names at the beginning . i also used title option
but it s not working for me .

Thanks.

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,239Questions: 1Answers: 2,599

    Hi @Barbaros ,

    It depends on whether you're using arrays or objects. You're using an array with you add the row (row.add()), so you need to use the index for the position of each field. If you were using objects, as in the first example on that page, then you could use the element name.

    Cheers,

    Colin

  • BarbarosBarbaros Posts: 3Questions: 1Answers: 0

    Hi @colin ,thanks for your reply.
    I got it .
    one more question- (without ajax): this datatable is inside form and when i submit the form i want to post all table data with hidden input. is it possible or is there any better way to pass all data ?
    Thanks .

    javascript:
    $(this).submit( function() {
     
            var table = $('#example').DataTable();
            var data = t.rows().data().toArray();
            $('#dt_value_1').val(data);
    
        } );
    

    and html :

    <input type="hidden" name="dt_value[]" id="dt_value_1" class="form-control" autocomplete="off">
    

    and php:

    $_POST['dt_value']
    

    it works but it merge all data in one row .
    "[\"messi,123456789,messi@messi.com,ronaldo,111444777,ronaldo@ronaldo.com\"]"

  • colincolin Posts: 15,239Questions: 1Answers: 2,599
    Answer ✓

    Hi @Barbaros ,

    You could iterate over the rows one at a time, and add it to an array, something like this pseudo code:

    var data = [];
    table.rows().every(function() {
      data.push(this.data().toArray())
    }
    

    That would make a 2d array which would be tidier.

    Cheers,

    Colin

  • bindridbindrid Posts: 730Questions: 0Answers: 119
    Answer ✓

    Try JSON.stringify to serialized the data before you put it in the hidden. Then deserialize on the back end. That should give you your array to work with.

  • BarbarosBarbaros Posts: 3Questions: 1Answers: 0

    @bindrid , @colin thanks for your replies,
    after i changed

    name="dt_value[] to name="dt_value
    

    and add using JSON.stringify to serialized the data

     var arry=t.rows().data().toArray();
            var data = JSON.stringify(arry);
            $('#dt_value_1').val(data);
    

    and for backend

     $response = $this->input->post('dt_value');
     $myjson = json_decode($response);
    
    

    is worked for me, thanks for your helps .

This discussion has been closed.