简体   繁体   English

jQuery DataTable - 添加新行,但无法使其可编辑(可编辑)

[英]jQuery DataTable - Add new row works, but not able to make it editable (jeditable)

I'm having a PHP page where I'm using a DataTable(jQuery) plugin to display all the data from the database. 我有一个PHP页面,我正在使用DataTable(jQuery)插件来显示数据库中的所有数据。

Here I want to give the User the option to add new row, ie a new record for the user to enter data. 在这里,我想给用户提供添加新行的选项,即用户输入数据的新记录。 I followed the example at: 我按照以下示例:

http://www.datatables.net/examples/api/add_row.html http://www.datatables.net/examples/api/add_row.html

and was able to create a new row. 并能够创建一个新行。

But I'm totally not sure on how to add the "id" property for the being generated and also, I'm not sure on how to make it editable. 但我完全不确定如何为生成的内容添加“id”属性,而且,我不确定如何使其可编辑。

As of now, all the other fields are editable using jeditable. 截至目前,所有其他字段都可以使用jeditable进行编辑。
The code is as follows: 代码如下:

            $(document).ready(function() {
            /* Init DataTables */
            var oTable = $('#example').dataTable({
            "iDisplayLength": 5,
            //"bRetrieve": true,
            "aLengthMenu": [[5, 10, 15, -1], [5, 10, 15, "All"]]
            }
            );


            /* Apply the jEditable handlers to the table */
            $('td', oTable.fnGetNodes()).editable( 'expenseFieldsUpdater.php', {
                "callback": function( sValue, y ) {
                    var aPos = oTable.fnGetPosition( this );
                    oTable.fnUpdate( sValue, aPos[0], aPos[1] );
                },
                "submitdata": function ( value, settings ) {
                    return {
                        "row_id": this.parentNode.getAttribute('id'),
                        "column": oTable.fnGetPosition( this )[2],
                        "form_id": document.getElementById('formID').value
                    };
                },
                "height": "14px"
            } );
        } );

        var giCount = 1;
        function fnClickAddRow() {
            $('#example').dataTable().fnAddData( [
                ".1",
                giCount+".2",
                giCount+".3",
                giCount+".4",
                giCount+".5" ] );
                    giCount++;
        }

But then, I'm totally not sure on how to go about making it editable. 但是,我完全不确定如何使其可编辑。 Any pointers would be very helpful. 任何指针都会非常有用。

I found the following post in the datatables site, but was not able to understand on how to go about making them work: 我在datatables网站上发现了以下帖子,但无法理解如何使它们工作:
http://datatables.net/forums/comments.php?DiscussionID=181 http://datatables.net/forums/comments.php?DiscussionID=181

You can find a plugin that can help you on the http://code.google.com/p/jquery-datatables-editable/ . 您可以在http://code.google.com/p/jquery-datatables-editable/找到可以帮助您的插件。 This plugin enchances standard JQuery DataTables plugin and handles add, delete, and edit actions. 该插件增强了标准的JQuery DataTables插件,并处理添加,删除和编辑操作。 You can download HTML example there. 您可以在那里下载HTML示例。

I typically find I need to re-run $(document).ready events that decorate table rows after I add or edit rows. 我通常发现在添加或编辑行之后,我需要重新运行$(document).ready事件来装饰表行。 So you should probably refactor out the call to: 所以你应该重构一下这个调用:

$('td', oTable.fnGetNodes()).editable()

into a separate method that you can call from $(document).ready and from fnClickAddRow(). 进入一个单独的方法,您可以从$(document).ready和fnClickAddRow()调用。

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

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