skip to Main Content

`As I was trying to get data from Datatable which is inside a Form I can’t Get all data.

Datatable has pagination and only gets data of visible/current page.

This is my Html Code:`

`<form id="frm1">
            <table class="table" id="ctable">
                <thead>
                    <tr class="">
                        <th class="">Cusromer</th>
                        <th class="">C-Name</th>
                        <th class="">Contact</th>
                        <th class="">DELETE</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>

        </form>
        <button class="submitthis" id="submitthis" type="submit" value="Submit">submit</button>`

`This is my Jquery code`
<script>
                    var num = 3;
                    var num3 = 0;
                    num=3;
                    num3=0;
                    var y = document.forms["frm1"];
                    for (let i = 0; i < y.length; i++) {
                        newtexty.push(y.elements[num3].value);
                        num3 = num3 + 1;
                        if (num == i) {
                            arrayb.push(newtexty);
                            newtexty = [];
                            num = num + 4;
                        }
                   }
    <script>

"var y = document.forms["frm1"];" only give me the data of datatble current page and ignores data of other pages. I can't get all that data directly from Datatable.row().data() because its not yet set into datatable. I only want to store this data from inputs into an array.

2

Answers


  1. Chosen as BEST ANSWER

    I posted this question but didn't received any answer but I have founded another way to store all data into an array or Datatable directly.I gave input some Id="1" and accessed data within it using on change

    columnDefs: [       {
                            targets: [0],
                            "render": function (fakedata) {
                                return '<input class="" id="1" type="text" placeholder="Product Seller" value="" />';
                            }
                        },
                    ],
    
    $('#ctable').on('change', '#1', function () {
          var rownum;
          rownum = $(this).closest('tr');
          $('#ctable').DataTable().row(rownum).data()[0] =(this).val();
    });
    

    By using this logic I got an array it works fine with ajax but still in some cases like remaining on same page just using jQuery you can get an array with only entered values while regarding other empty cell it will say its empty or undefined.

    Array(1)
    0
    : 
    (4) ['HAMZA', '', '', '']
    length
    : 
    1
    [[Prototype]]
    : 
    Array(0)
    

  2. Yes this problem can be easily resolved if you put checks like "if table row(number)index(number) is null, than add table row(number)index(number) = ” this logic will avoid sending null or unidentified to other indexes of same row if they are not entered and if table row(number)index(number) is not null and has a value then this problem is resolved.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search