How to copy one row in a Datateble to another identical Datatable

How to copy one row in a Datateble to another identical Datatable

msm_baltazarmsm_baltazar Posts: 59Questions: 22Answers: 0

Hi friends, I have two identical table as named left and rifht table. When I select or clicked one row on left table, I want to move data to another identical Datatable or reverse. I find a solution but it doesnt work.I think it is valid for old version of Datatable.How can I handle this task.

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954
    edited January 2020 Answer ✓

    Here is an example using the Select Extension to move selected rows from table 1 to table 2:
    http://live.datatables.net/cocejife/1/edit

    It uses rows().indexes() with a selector-modifier of {selected:true} to get the indexes of the selected rows in table 1. It uses rows().data() to get the selected rows data and uses toArray() to place it in a Javascript array.

    rows().remove() is used to delete the rows from table 1 and rows.add() is used to add the rows to table 2.

    Kevin

  • msm_baltazarmsm_baltazar Posts: 59Questions: 22Answers: 0

    Thank you very much brother:)

  • msm_baltazarmsm_baltazar Posts: 59Questions: 22Answers: 0

    but I have a little problem. Once clicked the button all the record move to right table. But I select any row in left table. When I debug code with developer tools, I see that all the record was selected ut I didnt select. At the same time I dont see selection effect(highlight)

  • msm_baltazarmsm_baltazar Posts: 59Questions: 22Answers: 0

    @kthorngren could you help me to solve selection problem?

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    Not sure I understand the problem. Are these issues with my example? If so then please provide the exact steps to replicate the issue. Otherwise update the example or provide a test case showing the problems you are having.

    Kevin

  • msm_baltazarmsm_baltazar Posts: 59Questions: 22Answers: 0
    edited January 2020

    In your example this two lines should be

    var indexes = table.rows( {selected: true} ).indexes();
    var data = table.rows(selected: true).data().toArray();

    replaced with

    var indexes = table.rows('.selected').indexes();
    var data = table.rows('.selected').data().toArray();

    But I have already select problem.When you try to select a line, there is no change.there is no row selection in my table

    This is my code block

    $(document).ready(function () {
    var surveyId = $("#SurveyId").val();
    var leftTable = $("#leftTable").DataTable({
    select: true,
    responsive: true,
    //"order": [[0, "asc"]],
    //"scrollX": true,
    //"scrollY": "auto",
    "columnDefs": [
    { width: '10%', targets: 0 }
    ],
    "columns": [
    { "data": "QuestionId" },
    { "data": "QuestionName" }
    ],
    "ajax": {
    "url": "/Survey/GetAllQuestions",
    "type": "POST",
    "data": { "surveyId": surveyId },
    "datatype": "int"
    },
    "language": {
    "emptyTable":
    "<b>Ankete ilişkilendirilecek soru bulunamadı.</b>"
    }
    });

    This is my datatables

    My data

    {
    "data": [
    {
    "Responses": [],
    "Options": [],
    "SurveyQuestions": [],
    "QuestionId": 1,
    "QuestionName": "Merinos ürünlerinden ne kadar memnunsunuz?",
    "QuestionTypeId": 1,
    "CreatedDate": "/Date(1577048400000)/",
    "CreatedUserId": 1,
    "IsActive": true,
    "Status": true,
    "UpdatedDate": "/Date(1577964703320)/"
    },
    {
    "Responses": [],
    "Options": [],
    "SurveyQuestions": [],
    "QuestionId": 2,
    "QuestionName": "Merinosun satis magazalarindan memnun musunuz?",
    "QuestionTypeId": 1,
    "CreatedDate": "/Date(1577048400000)/",
    "CreatedUserId": 1,
    "IsActive": true,
    "Status": true,
    "UpdatedDate": "/Date(1577048400000)/"
    },
    {
    "Responses": [],
    "Options": [],
    "SurveyQuestions": [],
    "QuestionId": 3,
    "QuestionName": "Merinosun müsteri temsilcilerinden memnun musunuz?",
    "QuestionTypeId": 1,
    "CreatedDate": "/Date(1577048400000)/",
    "CreatedUserId": 1,
    "IsActive": true,
    "Status": true,
    "UpdatedDate": "/Date(1577048400000)/"
    }
    ]
    }

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954
    Answer ✓

    {selected: true} works and is the documented way to get the selected rows when using the Select Extension. It is documented here in the selector-modifier docs. However using the class .selected should work too.

    But I have already select problem.When you try to select a line, there is no change.there is no row selection in my table

    Are you using the Select Extension?

    Again we will need to see the problem replicated in a web page so we can help debug.

    Kevin

  • msm_baltazarmsm_baltazar Posts: 59Questions: 22Answers: 0

    I added the following links to my page to use datatable. Do I need to download files separately for the Select feature? Is that what you're saying? I use the datatable structure for the first time.

  • msm_baltazarmsm_baltazar Posts: 59Questions: 22Answers: 0

    I am using Datasource in asp.net Mvc project.Do you know how to add Datatable files in asp.net project.

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954
    Answer ✓

    You can either download or use the CDN links for any of the Datatables extension. You can use the Download Builder to generate the CDN links or download for the desired set of Datatables features.

    The order they are installed is also important due the dependencies. The Download Builder or any of the examples will show the proper order.

    Kevin

  • msm_baltazarmsm_baltazar Posts: 59Questions: 22Answers: 0

    I have add all the dependencies in Bundle at ASP:NET MVC.But I have a serious style problem. Anad I am getting error on browser.

    style bundle

            bundles.Add(new StyleBundle("~/bundles/datatablecss").Include(
                "~/DataTables/DataTables-1.10.20/css/dataTables.bootstrap4.min.css",
                "~/DataTables/AutoFill-2.3.4/css/autoFill.bootstrap4.css",
                "~/DataTables/Buttons-1.6.1/css/buttons.bootstrap4.min.css",
                "~/DataTables/ColReorder-1.5.2/css/colReorder.bootstrap4.min.css",
                "~/DataTables/FixedColumns-3.3.0/css/fixedColumns.bootstrap4.min.css",
                "~/DataTables/FixedHeader-3.1.6/css/fixedHeader.bootstrap4.min.css",
                "~/DataTables/KeyTable-2.5.1/css/keyTable.bootstrap4.min.css",
                "~/DataTables/Responsive-2.2.3/css/responsive.bootstrap4.min.css",
                "~/DataTables/RowGroup-1.1.1/css/rowGroup.bootstrap4.min.css",
                "~/DataTables/RowReorder-1.2.6/css/rowReorder.bootstrap4.min.css",
                "~/DataTables/Scroller-2.0.1/css/scroller.bootstrap4.min.css",
                "~/DataTables/Select-1.3.1/css/select.bootstrap4.min.css"
            ));
    

    js bundle

          bundles.Add(new ScriptBundle("~/bundles/datatables").Include(
                      "~/DataTables/JSZip-2.5.0/jszip.min.js",
                      "~/DataTables/pdfmake-0.1.36/pdfmake.min.js",
                      "~/DataTables/pdfmake-0.1.36/vfs_fonts.js",
                      "~/DataTables/DataTables-1.10.20/js/jquery.dataTables.min.js",
                      "~/DataTables/DataTables-1.10.20/js/dataTables.bootstrap4.min.js",
                      "~/DataTables/AutoFill-2.3.4/js/dataTables.autoFill.min.js",
                      "~/DataTables/AutoFill-2.3.4/js/autoFill.bootstrap4.min.js",
                      "~/DataTables/Buttons-1.6.1/js/dataTables.buttons.min.js",
                      "~/DataTables/Buttons-1.6.1/js/buttons.bootstrap4.min.js",
                      "~/DataTables/Buttons-1.6.1/js/buttons.colVis.min.j",
                      "~/DataTables/Buttons-1.6.1/js/buttons.flash.min.js",
                      "~/DataTables/Buttons-1.6.1/js/buttons.html5.min.js",
                      "~/DataTables/Buttons-1.6.1/js/buttons.print.min.js",
                      "~/DataTables/ColReorder-1.5.2/js/dataTables.colReorder.min.js",
                      "~/DataTables/FixedColumns-3.3.0/js/dataTables.fixedColumns.min.js",
                      "~/DataTables/FixedHeader-3.1.6/js/dataTables.fixedHeader.min.js",
                      "~/DataTables/KeyTable-2.5.1/js/dataTables.keyTable.min.js",
                      "~/DataTables/Responsive-2.2.3/js/dataTables.responsive.min.js",
                      "~/DataTables/Responsive-2.2.3/js/responsive.bootstrap4.min.js",
                      "~/DataTables/RowGroup-1.1.1/js/dataTables.rowGroup.min.js",
                      "~/DataTables/RowReorder-1.2.6/js/dataTables.rowReorder.min.js",
                      "~/DataTables/Scroller-2.0.1/js/dataTables.scroller.min.js",
                      "~/DataTables/Select-1.3.1/js/dataTables.select.min.js"));
    

    Everything is broken how do i solve it?

  • msm_baltazarmsm_baltazar Posts: 59Questions: 22Answers: 0

    I downloaded the datatable dependencies. Then I added Bundle at ASP:NET MVC.But I have a big problem. I am getting error on browser debug.

    js Files

            bundles.Add(new ScriptBundle("~/bundles/datatables").Include(
                      "~/DataTables/JSZip-2.5.0/jszip.min.js",
                      "~/DataTables/pdfmake-0.1.36/pdfmake.min.js",
                      "~/DataTables/pdfmake-0.1.36/vfs_fonts.js",
                      "~/DataTables/DataTables-1.10.20/js/jquery.dataTables.min.js",
                      "~/DataTables/DataTables-1.10.20/js/dataTables.bootstrap4.min.js",
                      "~/DataTables/AutoFill-2.3.4/js/dataTables.autoFill.min.js",
                      "~/DataTables/AutoFill-2.3.4/js/autoFill.bootstrap4.min.js",
                      "~/DataTables/Buttons-1.6.1/js/dataTables.buttons.min.js",
                      "~/DataTables/Buttons-1.6.1/js/buttons.bootstrap4.min.js",
                      "~/DataTables/Buttons-1.6.1/js/buttons.colVis.min.j",
                      "~/DataTables/Buttons-1.6.1/js/buttons.flash.min.js",
                      "~/DataTables/Buttons-1.6.1/js/buttons.html5.min.js",
                      "~/DataTables/Buttons-1.6.1/js/buttons.print.min.js",
                      "~/DataTables/ColReorder-1.5.2/js/dataTables.colReorder.min.js",
                      "~/DataTables/FixedColumns-3.3.0/js/dataTables.fixedColumns.min.js",
                      "~/DataTables/FixedHeader-3.1.6/js/dataTables.fixedHeader.min.js",
                      "~/DataTables/KeyTable-2.5.1/js/dataTables.keyTable.min.js",
                      "~/DataTables/Responsive-2.2.3/js/dataTables.responsive.min.js",
                      "~/DataTables/Responsive-2.2.3/js/responsive.bootstrap4.min.js",
                      "~/DataTables/RowGroup-1.1.1/js/dataTables.rowGroup.min.js",
                      "~/DataTables/RowReorder-1.2.6/js/dataTables.rowReorder.min.js",
                      "~/DataTables/Scroller-2.0.1/js/dataTables.scroller.min.js",
                      "~/DataTables/Select-1.3.1/js/dataTables.select.min.js"));
    

    css files

                bundles.Add(new StyleBundle("~/bundles/datatablecss").Include(
                "~/DataTables/DataTables-1.10.20/css/dataTables.bootstrap4.min.css",
                "~/DataTables/AutoFill-2.3.4/css/autoFill.bootstrap4.css",
                "~/DataTables/Buttons-1.6.1/css/buttons.bootstrap4.min.css",
                "~/DataTables/ColReorder-1.5.2/css/colReorder.bootstrap4.min.css",
                "~/DataTables/FixedColumns-3.3.0/css/fixedColumns.bootstrap4.min.css",
                "~/DataTables/FixedHeader-3.1.6/css/fixedHeader.bootstrap4.min.css",
                "~/DataTables/KeyTable-2.5.1/css/keyTable.bootstrap4.min.css",
                "~/DataTables/Responsive-2.2.3/css/responsive.bootstrap4.min.css",
                "~/DataTables/RowGroup-1.1.1/css/rowGroup.bootstrap4.min.css",
                "~/DataTables/RowReorder-1.2.6/css/rowReorder.bootstrap4.min.css",
                "~/DataTables/Scroller-2.0.1/css/scroller.bootstrap4.min.css",
                "~/DataTables/Select-1.3.1/css/select.bootstrap4.min.css"  ));
    

    Datatable after changing

    error on browser

  • msm_baltazarmsm_baltazar Posts: 59Questions: 22Answers: 0

    I solved the proble. it is my fault.
    Replaced
    @Sscripts.Render("~/bundles/datatablecss")

    with
    @Styles.Render("~/bundles/datatablecss")

This discussion has been closed.