I want to populate data in DataTable on button click, by default it should display no data available












1















I am using NodeJs




  • I want to populate datatable on a button click.

  • I am reading the data from a txt file.



home.ejs




<button id="SearchButton" class="btn btn-info">Search</button>

<table class="table table-bordered" id="ctsdataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Check No.</th>
<th>Tran Code</th>
<th>ISN</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Check No.</th>
<th>Tran Code</th>
<th>ISN</th>
</tr>
</tfoot>
</table>



home.js




$(document).ready(function() {
Table = $('#ctsdataTable').DataTable({
"ajax": "js/data.txt",
"columns": [{
"data": "check_number"
},
{
"data": "tran_code"
},
{
"data": "isn"
}
]
});
});









share|improve this question

























  • doesn't matter if the table has no <tbody></tbody> tag ?

    – simonecosci
    Nov 16 '18 at 8:08











  • @simonecosci not required, that will be taken care by datatable itself.

    – Adarsh Singh
    Nov 16 '18 at 8:18
















1















I am using NodeJs




  • I want to populate datatable on a button click.

  • I am reading the data from a txt file.



home.ejs




<button id="SearchButton" class="btn btn-info">Search</button>

<table class="table table-bordered" id="ctsdataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Check No.</th>
<th>Tran Code</th>
<th>ISN</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Check No.</th>
<th>Tran Code</th>
<th>ISN</th>
</tr>
</tfoot>
</table>



home.js




$(document).ready(function() {
Table = $('#ctsdataTable').DataTable({
"ajax": "js/data.txt",
"columns": [{
"data": "check_number"
},
{
"data": "tran_code"
},
{
"data": "isn"
}
]
});
});









share|improve this question

























  • doesn't matter if the table has no <tbody></tbody> tag ?

    – simonecosci
    Nov 16 '18 at 8:08











  • @simonecosci not required, that will be taken care by datatable itself.

    – Adarsh Singh
    Nov 16 '18 at 8:18














1












1








1








I am using NodeJs




  • I want to populate datatable on a button click.

  • I am reading the data from a txt file.



home.ejs




<button id="SearchButton" class="btn btn-info">Search</button>

<table class="table table-bordered" id="ctsdataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Check No.</th>
<th>Tran Code</th>
<th>ISN</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Check No.</th>
<th>Tran Code</th>
<th>ISN</th>
</tr>
</tfoot>
</table>



home.js




$(document).ready(function() {
Table = $('#ctsdataTable').DataTable({
"ajax": "js/data.txt",
"columns": [{
"data": "check_number"
},
{
"data": "tran_code"
},
{
"data": "isn"
}
]
});
});









share|improve this question
















I am using NodeJs




  • I want to populate datatable on a button click.

  • I am reading the data from a txt file.



home.ejs




<button id="SearchButton" class="btn btn-info">Search</button>

<table class="table table-bordered" id="ctsdataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Check No.</th>
<th>Tran Code</th>
<th>ISN</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Check No.</th>
<th>Tran Code</th>
<th>ISN</th>
</tr>
</tfoot>
</table>



home.js




$(document).ready(function() {
Table = $('#ctsdataTable').DataTable({
"ajax": "js/data.txt",
"columns": [{
"data": "check_number"
},
{
"data": "tran_code"
},
{
"data": "isn"
}
]
});
});






jquery node.js ajax datatable datatables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 8:13









Krupesh Kotecha

2,06011134




2,06011134










asked Nov 16 '18 at 8:06









Adarsh SinghAdarsh Singh

468




468













  • doesn't matter if the table has no <tbody></tbody> tag ?

    – simonecosci
    Nov 16 '18 at 8:08











  • @simonecosci not required, that will be taken care by datatable itself.

    – Adarsh Singh
    Nov 16 '18 at 8:18



















  • doesn't matter if the table has no <tbody></tbody> tag ?

    – simonecosci
    Nov 16 '18 at 8:08











  • @simonecosci not required, that will be taken care by datatable itself.

    – Adarsh Singh
    Nov 16 '18 at 8:18

















doesn't matter if the table has no <tbody></tbody> tag ?

– simonecosci
Nov 16 '18 at 8:08





doesn't matter if the table has no <tbody></tbody> tag ?

– simonecosci
Nov 16 '18 at 8:08













@simonecosci not required, that will be taken care by datatable itself.

– Adarsh Singh
Nov 16 '18 at 8:18





@simonecosci not required, that will be taken care by datatable itself.

– Adarsh Singh
Nov 16 '18 at 8:18












2 Answers
2






active

oldest

votes


















0














I figured out a way of doing this.



While re-initializing the DataTable just put a property "destroy": true



$(document).ready(function() {
$('#ctsdataTable').DataTable({
"columns": [
{"data": "session_date"},
{"data": "session_number"},
{"data": "r_session_number"},
{"data": "account"},
{"data": "amount"},
{"data": "check_number"},
{"data": "tran_code"},
{"data": "isn"}
]
});

$('#SearchButton').on('click', function () {
$('#ctsdataTable').DataTable( {
"destroy": true,
"ajax": "js/data.txt",
"columns": [
{"data": "session_date"},
{"data": "session_number"},
{"data": "r_session_number"},
{"data": "account"},
{"data": "amount"},
{"data": "check_number"},
{"data": "tran_code"},
{"data": "isn"}
]
});
});
});





share|improve this answer































    0














    Try this:



    $(document).ready(function() {

    var yourTable = $('#ctsdataTable').DataTable({
    "columns": [
    {"data": "check_number"},
    {"data": "tran_code"},
    {"data": "isn"}
    ]
    });

    $('#SearchButton').on('click', function () {
    $.ajax({
    url: "js/data.txt",
    success: function (entries) {
    if (!entries)
    return;

    entries.forEach(function (entry) {
    yourTable.row.add(entry);
    });

    yourTable.draw();
    }
    });
    });});





    share|improve this answer


























    • How I'll put data here in "entries" variable. That is a txt file

      – Adarsh Singh
      Nov 16 '18 at 9:06











    • In this case we need to use FileReader to read txt file, but we can do it without that. And I am unable to do that.

      – Adarsh Singh
      Nov 16 '18 at 9:18











    • @AdarshSingh I've updated my answer. Please, check it out.

      – DiTros
      Nov 19 '18 at 7:38











    • When trying this code I am getting this error: Uncaught TypeError: entries.forEach is not a function at Object.success (home.js:97)

      – Adarsh Singh
      Nov 22 '18 at 9:01













    • @AdarshSingh probably ajax response is an object and you should get entries from there and use forEach loop then.

      – DiTros
      Nov 29 '18 at 4:59











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333748%2fi-want-to-populate-data-in-datatable-on-button-click-by-default-it-should-displ%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I figured out a way of doing this.



    While re-initializing the DataTable just put a property "destroy": true



    $(document).ready(function() {
    $('#ctsdataTable').DataTable({
    "columns": [
    {"data": "session_date"},
    {"data": "session_number"},
    {"data": "r_session_number"},
    {"data": "account"},
    {"data": "amount"},
    {"data": "check_number"},
    {"data": "tran_code"},
    {"data": "isn"}
    ]
    });

    $('#SearchButton').on('click', function () {
    $('#ctsdataTable').DataTable( {
    "destroy": true,
    "ajax": "js/data.txt",
    "columns": [
    {"data": "session_date"},
    {"data": "session_number"},
    {"data": "r_session_number"},
    {"data": "account"},
    {"data": "amount"},
    {"data": "check_number"},
    {"data": "tran_code"},
    {"data": "isn"}
    ]
    });
    });
    });





    share|improve this answer




























      0














      I figured out a way of doing this.



      While re-initializing the DataTable just put a property "destroy": true



      $(document).ready(function() {
      $('#ctsdataTable').DataTable({
      "columns": [
      {"data": "session_date"},
      {"data": "session_number"},
      {"data": "r_session_number"},
      {"data": "account"},
      {"data": "amount"},
      {"data": "check_number"},
      {"data": "tran_code"},
      {"data": "isn"}
      ]
      });

      $('#SearchButton').on('click', function () {
      $('#ctsdataTable').DataTable( {
      "destroy": true,
      "ajax": "js/data.txt",
      "columns": [
      {"data": "session_date"},
      {"data": "session_number"},
      {"data": "r_session_number"},
      {"data": "account"},
      {"data": "amount"},
      {"data": "check_number"},
      {"data": "tran_code"},
      {"data": "isn"}
      ]
      });
      });
      });





      share|improve this answer


























        0












        0








        0







        I figured out a way of doing this.



        While re-initializing the DataTable just put a property "destroy": true



        $(document).ready(function() {
        $('#ctsdataTable').DataTable({
        "columns": [
        {"data": "session_date"},
        {"data": "session_number"},
        {"data": "r_session_number"},
        {"data": "account"},
        {"data": "amount"},
        {"data": "check_number"},
        {"data": "tran_code"},
        {"data": "isn"}
        ]
        });

        $('#SearchButton').on('click', function () {
        $('#ctsdataTable').DataTable( {
        "destroy": true,
        "ajax": "js/data.txt",
        "columns": [
        {"data": "session_date"},
        {"data": "session_number"},
        {"data": "r_session_number"},
        {"data": "account"},
        {"data": "amount"},
        {"data": "check_number"},
        {"data": "tran_code"},
        {"data": "isn"}
        ]
        });
        });
        });





        share|improve this answer













        I figured out a way of doing this.



        While re-initializing the DataTable just put a property "destroy": true



        $(document).ready(function() {
        $('#ctsdataTable').DataTable({
        "columns": [
        {"data": "session_date"},
        {"data": "session_number"},
        {"data": "r_session_number"},
        {"data": "account"},
        {"data": "amount"},
        {"data": "check_number"},
        {"data": "tran_code"},
        {"data": "isn"}
        ]
        });

        $('#SearchButton').on('click', function () {
        $('#ctsdataTable').DataTable( {
        "destroy": true,
        "ajax": "js/data.txt",
        "columns": [
        {"data": "session_date"},
        {"data": "session_number"},
        {"data": "r_session_number"},
        {"data": "account"},
        {"data": "amount"},
        {"data": "check_number"},
        {"data": "tran_code"},
        {"data": "isn"}
        ]
        });
        });
        });






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 10:27









        Adarsh SinghAdarsh Singh

        468




        468

























            0














            Try this:



            $(document).ready(function() {

            var yourTable = $('#ctsdataTable').DataTable({
            "columns": [
            {"data": "check_number"},
            {"data": "tran_code"},
            {"data": "isn"}
            ]
            });

            $('#SearchButton').on('click', function () {
            $.ajax({
            url: "js/data.txt",
            success: function (entries) {
            if (!entries)
            return;

            entries.forEach(function (entry) {
            yourTable.row.add(entry);
            });

            yourTable.draw();
            }
            });
            });});





            share|improve this answer


























            • How I'll put data here in "entries" variable. That is a txt file

              – Adarsh Singh
              Nov 16 '18 at 9:06











            • In this case we need to use FileReader to read txt file, but we can do it without that. And I am unable to do that.

              – Adarsh Singh
              Nov 16 '18 at 9:18











            • @AdarshSingh I've updated my answer. Please, check it out.

              – DiTros
              Nov 19 '18 at 7:38











            • When trying this code I am getting this error: Uncaught TypeError: entries.forEach is not a function at Object.success (home.js:97)

              – Adarsh Singh
              Nov 22 '18 at 9:01













            • @AdarshSingh probably ajax response is an object and you should get entries from there and use forEach loop then.

              – DiTros
              Nov 29 '18 at 4:59
















            0














            Try this:



            $(document).ready(function() {

            var yourTable = $('#ctsdataTable').DataTable({
            "columns": [
            {"data": "check_number"},
            {"data": "tran_code"},
            {"data": "isn"}
            ]
            });

            $('#SearchButton').on('click', function () {
            $.ajax({
            url: "js/data.txt",
            success: function (entries) {
            if (!entries)
            return;

            entries.forEach(function (entry) {
            yourTable.row.add(entry);
            });

            yourTable.draw();
            }
            });
            });});





            share|improve this answer


























            • How I'll put data here in "entries" variable. That is a txt file

              – Adarsh Singh
              Nov 16 '18 at 9:06











            • In this case we need to use FileReader to read txt file, but we can do it without that. And I am unable to do that.

              – Adarsh Singh
              Nov 16 '18 at 9:18











            • @AdarshSingh I've updated my answer. Please, check it out.

              – DiTros
              Nov 19 '18 at 7:38











            • When trying this code I am getting this error: Uncaught TypeError: entries.forEach is not a function at Object.success (home.js:97)

              – Adarsh Singh
              Nov 22 '18 at 9:01













            • @AdarshSingh probably ajax response is an object and you should get entries from there and use forEach loop then.

              – DiTros
              Nov 29 '18 at 4:59














            0












            0








            0







            Try this:



            $(document).ready(function() {

            var yourTable = $('#ctsdataTable').DataTable({
            "columns": [
            {"data": "check_number"},
            {"data": "tran_code"},
            {"data": "isn"}
            ]
            });

            $('#SearchButton').on('click', function () {
            $.ajax({
            url: "js/data.txt",
            success: function (entries) {
            if (!entries)
            return;

            entries.forEach(function (entry) {
            yourTable.row.add(entry);
            });

            yourTable.draw();
            }
            });
            });});





            share|improve this answer















            Try this:



            $(document).ready(function() {

            var yourTable = $('#ctsdataTable').DataTable({
            "columns": [
            {"data": "check_number"},
            {"data": "tran_code"},
            {"data": "isn"}
            ]
            });

            $('#SearchButton').on('click', function () {
            $.ajax({
            url: "js/data.txt",
            success: function (entries) {
            if (!entries)
            return;

            entries.forEach(function (entry) {
            yourTable.row.add(entry);
            });

            yourTable.draw();
            }
            });
            });});






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 19 '18 at 7:37

























            answered Nov 16 '18 at 8:18









            DiTrosDiTros

            13




            13













            • How I'll put data here in "entries" variable. That is a txt file

              – Adarsh Singh
              Nov 16 '18 at 9:06











            • In this case we need to use FileReader to read txt file, but we can do it without that. And I am unable to do that.

              – Adarsh Singh
              Nov 16 '18 at 9:18











            • @AdarshSingh I've updated my answer. Please, check it out.

              – DiTros
              Nov 19 '18 at 7:38











            • When trying this code I am getting this error: Uncaught TypeError: entries.forEach is not a function at Object.success (home.js:97)

              – Adarsh Singh
              Nov 22 '18 at 9:01













            • @AdarshSingh probably ajax response is an object and you should get entries from there and use forEach loop then.

              – DiTros
              Nov 29 '18 at 4:59



















            • How I'll put data here in "entries" variable. That is a txt file

              – Adarsh Singh
              Nov 16 '18 at 9:06











            • In this case we need to use FileReader to read txt file, but we can do it without that. And I am unable to do that.

              – Adarsh Singh
              Nov 16 '18 at 9:18











            • @AdarshSingh I've updated my answer. Please, check it out.

              – DiTros
              Nov 19 '18 at 7:38











            • When trying this code I am getting this error: Uncaught TypeError: entries.forEach is not a function at Object.success (home.js:97)

              – Adarsh Singh
              Nov 22 '18 at 9:01













            • @AdarshSingh probably ajax response is an object and you should get entries from there and use forEach loop then.

              – DiTros
              Nov 29 '18 at 4:59

















            How I'll put data here in "entries" variable. That is a txt file

            – Adarsh Singh
            Nov 16 '18 at 9:06





            How I'll put data here in "entries" variable. That is a txt file

            – Adarsh Singh
            Nov 16 '18 at 9:06













            In this case we need to use FileReader to read txt file, but we can do it without that. And I am unable to do that.

            – Adarsh Singh
            Nov 16 '18 at 9:18





            In this case we need to use FileReader to read txt file, but we can do it without that. And I am unable to do that.

            – Adarsh Singh
            Nov 16 '18 at 9:18













            @AdarshSingh I've updated my answer. Please, check it out.

            – DiTros
            Nov 19 '18 at 7:38





            @AdarshSingh I've updated my answer. Please, check it out.

            – DiTros
            Nov 19 '18 at 7:38













            When trying this code I am getting this error: Uncaught TypeError: entries.forEach is not a function at Object.success (home.js:97)

            – Adarsh Singh
            Nov 22 '18 at 9:01







            When trying this code I am getting this error: Uncaught TypeError: entries.forEach is not a function at Object.success (home.js:97)

            – Adarsh Singh
            Nov 22 '18 at 9:01















            @AdarshSingh probably ajax response is an object and you should get entries from there and use forEach loop then.

            – DiTros
            Nov 29 '18 at 4:59





            @AdarshSingh probably ajax response is an object and you should get entries from there and use forEach loop then.

            – DiTros
            Nov 29 '18 at 4:59


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333748%2fi-want-to-populate-data-in-datatable-on-button-click-by-default-it-should-displ%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings