简体   繁体   English

jquery datatable 使用表单编辑表行数据

[英]jquery datatable edit table row data using form

在此处输入图片说明

 var tb = $('#example').DataTable(); $('#addRow').on('click', function() { var typeName = $("#type option:selected").val(); var amount = $("#amount").val(); tb.row.add([typeName, amount]).draw(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script> <label>Type</label> <select id="type"> <option> Type 01</option> <option> Type 02</option> </select> <label>Amount</label> <input type="text" id="amount" /> <button id="addRow"> Add </button> <table id="example" class="display" cellspacing="0" width="100%"> <thead> <tr> <th>Type</th> <th>Amount</th> </tr> </thead> </table>

i need to append edit and delete button for every row.我需要为每一行附加编辑和删除按钮。 when click the edit button, row data should load to dropdown and textbox.单击编辑按钮时,行数据应加载到下拉列表和文本框。 can u guide me to do this ?你能指导我这样做吗?

With certain changes to the architecture of your app, I would suggest the following approach that employs native DataTables options and API methods:随着应用程序架构的某些更改,我建议采用以下方法来使用本机 DataTables 选项和 API 方法:

 //initialize DataTable const tb = $('#example').DataTable({ //remove non-essential controls for the sake of cleaner view dom: 't', //use columns option to setup header titles columns: [ {title: 'Type'}, { title: 'Amount', //user 'render' to append Edit/Delete buttons for each entry render: data => `${data}<button action="delete">Delete</button><button action="edit">Edit</button>` } ] }); //click handler for dual purpose 'Submit' button that adds new rows and submits edits $('#submit').on('click', function() { //when submit button acts to append new row to the table (default) if($(this).attr('action') == 'addRow'){ tb.row.add([$("#type").val(), $("#amount").val()]).draw(); } //when submit button acts to submit edits if($(this).attr('action') == 'confirmEdit'){ //change affected row data and re-draw the table tb.row($(this).attr('rowindex')).data([$("#type").val(), $("#amount").val()]).draw(); } //clean up form, switch it to default state $('#type').val(""); $('#amount').val(""); $('#submit').attr('action', 'addRow'); }); //'Delete' button click handler $('#example').on('click', 'tbody tr button[action="delete"]', function(event){ tb.row($(event.target).closest('tr')).remove().draw(); }); //'Edit' button click handler $('#example').on('click', 'tbody tr button[action="edit"]', function(){ //get affected row entry const row = tb.row($(event.target).closest('tr')); //get affected row().index() and append that to 'Submit' button attributes //you may use global variable for that purpose if you prefer $('#submit').attr('rowindex', row.index()); //switch 'Submit' button role to 'confirmEdit' $('#submit').attr('action', 'confirmEdit'); //set up 'Type' and 'Amount' values according to the selected entry $('#type').val(row.data()[0]); $('#amount').val(row.data()[1]); });
 tbody tr button { display: block; float: right; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"> <label>Type</label> <select id="type"> <option value="" selected></option> <option value="Type 01">Type 01</option> <option value="Type 02">Type 02</option> </select> <label>Amount</label> <input type="text" id="amount" /> <button id="submit" action="addRow">Submit</button> <table id="example" class="display" cellspacing="0" width="100%"></table>

Add your HTML directly.直接添加您的 HTML。 I've added button, you can similarly add a drop down too.我添加了按钮,您也可以类似地添加下拉菜单。 Consider the following:考虑以下:

 var tb = $('#example').DataTable(); $('#addRow').on('click', function() { var typeName = $("#type option:selected").val(); var amount = $("#amount").val(); var row = tb.row.add([typeName, amount, "<span><button>Edit</button><button>Delete</button></span>"]).draw(); var edit = row.node().getElementsByTagName("button")[0]; edit.onclick = function() { document.getElementById('typeEdit').value = row.data()[0]; document.getElementById('amtEdit').value = row.data()[1]; } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script> <label>Type</label> <select id="type"> <option> Type 01</option> <option> Type 02</option> </select> <label>Amount</label> <input type="text" id="amount" /> <button id="addRow"> Add </button> <br/ > <br/ > Edit Type <select id="typeEdit"> <option> Type 01</option> <option> Type 02</option> </select> Edit Amount <input id="amtEdit" /> <br/ > <br/ > <table id="example" class="display" cellspacing="0" width="100%"> <thead> <tr> <th>Type</th> <th>Amount</th> <th>Ops</th> </tr> </thead> </table>

RowEditor.js行编辑器.js

I had a similiar issue and wrote a tiny JS tool, that targets editing of rows inline.我有一个类似的问题,写了一个小的 JS 工具,它的目标是编辑行内联。 The repo can be found here .可以在这里找到回购。 I think its functionality is best described by the picture below, but you can also find a running example here .我认为下图最好地描述了它的功能,但您也可以在此处找到运行示例。

行编辑器

Setting it up设置它

What you have to do to integrate it, is你必须做的来整合它,是

  1. Download and integrate the file下载并整合文件

    <script type="text/javascript" charset="utf8" src="/js/RowEditor.js"></script>

  2. Set up a configuration about which columns shall be editable and if they should be editable as dropdown or input (compare it to the example picture, you will quickly figure out what it does):设置关于哪些列应可编辑以及它们是否应作为下拉或输入可编辑的配置(将其与示例图片进行比较,您将很快弄清楚它的作用):

     "1":{"type":"input"}, "2":{"type":"input"}, "3":{"type":"select", "options":{ "1":{"value":'Sales Assistant', "title":'Sales Assistant'}, "2":{"value":'Tech Lead', "title":'Tech Lead'}, "3":{"value":'Secretary', "title":'Secretary'}, "4":{"value":'Developer', "title":'Developer'}, "5":{"value":'Trainee', "title":'Trainee'} } } }
  3. Call the editor, after you have initialized your DataTable:在初始化 DataTable 后调用编辑器:

     $(document).ready( function () { table = $('#table').DataTable(); rowEditor = new RowEditor('#table', table, editRowSettings); });
  4. Call the function editRow of rowEditor (or however you have named it above) with the index of the row you want to edit.呼叫rowEditor的功能editRow(或者无论你在上面把它命名为)与该行的索引要编辑。 I have the button placed in a sepperate column of the datatable, but you can call it anyway you want it.我将按钮放在数据表的单独列中,但您可以随意调用它。 <button onclick="rowEditor.editRow(1)">Edit</button>

If you have questions, feel free to ask or issue a pull request :)如果您有任何疑问,请随时提出或发出拉取请求 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM