数据表可编辑删除

发布于 2024-12-24 02:42:32 字数 2126 浏览 1 评论 0原文

我已将 DataTables Editable 添加到我的表中,以便能够添加和删除一些行。 添加部分效果很好,但我在删除部分遇到一些问题,因为我无法选择行。

根据我在示例中看到的,我所要做的就是添加删除 URL,如下所示:

.makeEditable({sDeleteURL: "/DeleteURL"});

但这并不会使我的行变得可选择,这样我就可以删除任何内容。

我的完整代码是:

$(function () {
    var oTable = $('[email protected]').dataTable(
        {
            "oLanguage": { "sUrl": "/LanguageURL" },
            "bProcessing": true,
            "bFilter": false,
            "sPaginationType": "full_numbers",
            "iDisplayLength": 10,
            "bLengthChange": false,
            "aoColumnDefs": [{ "sClass": "center-col", "aTargets": ['align-center-col'] },
                { "sClass": "read_only", "aTargets": ['read-only-col'] },
                { "sClass": "small-width-col", "aTargets": ['small-col'] }],
            "aaSorting": [[0, "desc"]],
            "bScrollCollapse": true,
            "bServerSide": true,
            "sAjaxSource": '/DataURL',
            "fnServerData": function (sSource, aoData, fnCallback) {
                aoData.push({ "name": "Numero", "value": $(this).find("#Numero").val() });
                $.ajax({
                    "dataType": 'json',
                    "type": "POST",
                    "url": sSource,
                    "data": aoData,
                    "success": fnCallback
                });
            }
        }).makeEditable({
            sAddNewRowFormId: '[email protected]',
            sAddNewRowButtonId: '[email protected]',
            btnDeleteRow: '[email protected]',
            sAddURL: "/AddURL",
            sDeleteURL: "/DeleteURL"
        });
});

I've add DataTables Editable to my table to able to add and delete some rows.
The add part works great, but I'm having some problem with the delete part, because I can't select the rows.

By what I've seen in the example all I've to do is to add the delete URL, something like this:

.makeEditable({sDeleteURL: "/DeleteURL"});

But that dosent make my rows selectable so I can delete anthing.

My full code is:

$(function () {
    var oTable = $('[email protected]').dataTable(
        {
            "oLanguage": { "sUrl": "/LanguageURL" },
            "bProcessing": true,
            "bFilter": false,
            "sPaginationType": "full_numbers",
            "iDisplayLength": 10,
            "bLengthChange": false,
            "aoColumnDefs": [{ "sClass": "center-col", "aTargets": ['align-center-col'] },
                { "sClass": "read_only", "aTargets": ['read-only-col'] },
                { "sClass": "small-width-col", "aTargets": ['small-col'] }],
            "aaSorting": [[0, "desc"]],
            "bScrollCollapse": true,
            "bServerSide": true,
            "sAjaxSource": '/DataURL',
            "fnServerData": function (sSource, aoData, fnCallback) {
                aoData.push({ "name": "Numero", "value": $(this).find("#Numero").val() });
                $.ajax({
                    "dataType": 'json',
                    "type": "POST",
                    "url": sSource,
                    "data": aoData,
                    "success": fnCallback
                });
            }
        }).makeEditable({
            sAddNewRowFormId: '[email protected]',
            sAddNewRowButtonId: '[email protected]',
            btnDeleteRow: '[email protected]',
            sAddURL: "/AddURL",
            sDeleteURL: "/DeleteURL"
        });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗心人 2024-12-31 02:42:32

我个人一直在尝试让数据表删除功能适用于我的一个项目。

我发现我需要在文档的头部包含 jquery-ui.js 脚本。

我还发现您需要像这样设置表格行的格式。 以便数据表可以自动从所选行中获取正确的 id # 并将其传递到您的 php 页面以进行进一步处理。

Datatables 可以通过使用 DT_RowId 特殊属性自动向每个表行添加 id。

如果您已将数据表设置为使用服务器端实现,则可以在服务器返回的 JSON 数据中包含 DT_RowId 来响应 ajax 请求。

例如 JSON 格式:

{ 
 "sEcho": 1,
 "iTotalRecords": "1",
 "iTotalDisplayRecords": "1",
 "aaData": [
  {
    "DT_RowId": "1",
    "engine": "Trident",
    "browser": "Internet Explorer 4.0",
    "platform": "Win 95+",
"version": "4",
    "grade": "X"
  }
 ]
}

这是我的示例模型代码。此示例中不包含服务器端实现,我只是使用数据表附带的示例源objects.txt 作为示例。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> 
  <head> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
    <script src="datatables/media/js/jquery.dataTables.js"></script> 
    <script src="examples/examples_support/jquery.jeditable.js"></script> 
    <script src="http://jquery-datatables-editable.googlecode.com/svn/trunk/media/js/jquery-ui.js"></script> 
    <script src="datatables/media/js/jquery.datatables.editable.js"></script> 
    <style type="text/css">
        @import "datatables/media/css/demo_table.css";
        @import "datatables/media/css/demo_table_jui.css";
        @import "examples/examples_support/themes/smoothness/jquery-ui-1.8.4.custom.css";
    </style> 
    <script> 
        $(document).ready(function() {
            var anOpen = [];
            var sImageUrl = "/datatables/examples/examples_support/";

            var oTable = $('#example').dataTable({
                "sPaginationType": "full_numbers",
                "bJQueryUI": true,
                "bProcessing": true,
                "sAjaxSource": "/datatables/examples/ajax/sources/objects.txt",
                "aoColumns": 
                [
                    {
                       "mDataProp": null,
                       "sClass": "control center",
                       "sDefaultContent": '<img src="'+sImageUrl+'details_open.png'+'">'
                    },
                    { "mDataProp": "engine" },
                    { "mDataProp": "browser" },
                    { "mDataProp": "grade" }
                ]
            }).makeEditable({
                sDeleteURL: "DeleteData.php",
                //Converts to JQuery UI button (full list of options on http://jqueryui.com/demos/button)
                oDeleteRowButtonOptions:
                {
                    label: "Remove",
                    icons: {primary:'ui-icon-trash'}
                },
                //Additional Paramaters can be sent for processing...
                oDeleteParameters:
                {
                    action:"delete_dt_projects_record",
                    dtSecurity:"9999999999"
                }
            });
            //For Expandable Collapsible Rows
            $('#example td.control').live( 'click', function () {

                var nTr = this.parentNode;
                //var projID = $(this).closest("td").next().text();
                //console.log(projID);
                //console.log(anOpen);
                //console.log(nTr);
                var i = $.inArray( nTr, anOpen );
                //console.log(i);
                if ( i === -1 ) 
                {
                    $('img', this).attr( 'src', sImageUrl+"details_close.png" );
                    var nDetailsRow = oTable.fnOpen( nTr, fnFormatDetails(oTable, nTr), 'details' );
                    $('div.innerDetails', nDetailsRow).slideDown();

                    anOpen.push( nTr );
                }
                else 
                {
                $('img', this).attr( 'src', sImageUrl+"details_open.png" );
                $('div.innerDetails', $(nTr).next()[0]).slideUp( function () 
                    {
                        oTable.fnClose( nTr );
                        anOpen.splice( i, 1 );
                    });
                }
            });
            function fnFormatDetails( oTable, nTr )
            {
              var oData = oTable.fnGetData( nTr );
              var sOut =
                '<div class="innerDetails">'+
                  '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
                    '<tr><td>Rendering engine:</td><td>'+oData.engine+'</td></tr>'+
                    '<tr><td>Browser:</td><td>'+oData.browser+'</td></tr>'+
                    '<tr><td>Platform:</td><td>'+oData.platform+'</td></tr>'+
                    '<tr><td>Version:</td><td>'+oData.version+'</td></tr>'+
                    '<tr><td>Grade:</td><td>'+oData.grade+'</td></tr>'+
                  '</table>'+
                '</div>';
              return sOut;
            }
        });

    </script> 
  </head> 
  <body> 
    <div style="width:900px">
    <div class="add_delete_toolbar">
        <button id="btnDeleteRow">Delete</button>
    </div>
      <table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
    <thead>
        <tr>
            <th></th>
            <th>Rendering engine</th>
            <th>Browser</th>
            <th>CSS grade</th>
        </tr>
    </thead>
    <tbody></tbody>
    </table>
    </div> 
  </body> 
</html> 

注意您可能需要将脚本和样式 URL 路径更改为您所在的位置才能查看工作示例。

更新:您还需要手动将其包含在 HTML 源代码中的任何位置。

<button id="btnDeleteRow">Delete</button>

I've personally been playing around with getting the datatables delete function to work for one of my projects.

What I've found is I needed to include the jquery-ui.js script in the head of the document.

I also discovered that you need to format your table rows like this. <tr id="1"> so that datatables can automatically grab the correct id # from the selected row and pass it on to your php page for further processing.

Datatables can automatically add an id to each table row through the use of DT_RowId special property.

If you have set up datatables to use server side implementation, then you can include DT_RowId in the JSON data returned from the server in response to the ajax request .

For example JSON format:

{ 
 "sEcho": 1,
 "iTotalRecords": "1",
 "iTotalDisplayRecords": "1",
 "aaData": [
  {
    "DT_RowId": "1",
    "engine": "Trident",
    "browser": "Internet Explorer 4.0",
    "platform": "Win 95+",
"version": "4",
    "grade": "X"
  }
 ]
}

Here is my example mockup code. Server side implementation isn't included with this example I'm just using the example source objects.txt that came with datatables for an example.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> 
  <head> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
    <script src="datatables/media/js/jquery.dataTables.js"></script> 
    <script src="examples/examples_support/jquery.jeditable.js"></script> 
    <script src="http://jquery-datatables-editable.googlecode.com/svn/trunk/media/js/jquery-ui.js"></script> 
    <script src="datatables/media/js/jquery.datatables.editable.js"></script> 
    <style type="text/css">
        @import "datatables/media/css/demo_table.css";
        @import "datatables/media/css/demo_table_jui.css";
        @import "examples/examples_support/themes/smoothness/jquery-ui-1.8.4.custom.css";
    </style> 
    <script> 
        $(document).ready(function() {
            var anOpen = [];
            var sImageUrl = "/datatables/examples/examples_support/";

            var oTable = $('#example').dataTable({
                "sPaginationType": "full_numbers",
                "bJQueryUI": true,
                "bProcessing": true,
                "sAjaxSource": "/datatables/examples/ajax/sources/objects.txt",
                "aoColumns": 
                [
                    {
                       "mDataProp": null,
                       "sClass": "control center",
                       "sDefaultContent": '<img src="'+sImageUrl+'details_open.png'+'">'
                    },
                    { "mDataProp": "engine" },
                    { "mDataProp": "browser" },
                    { "mDataProp": "grade" }
                ]
            }).makeEditable({
                sDeleteURL: "DeleteData.php",
                //Converts to JQuery UI button (full list of options on http://jqueryui.com/demos/button)
                oDeleteRowButtonOptions:
                {
                    label: "Remove",
                    icons: {primary:'ui-icon-trash'}
                },
                //Additional Paramaters can be sent for processing...
                oDeleteParameters:
                {
                    action:"delete_dt_projects_record",
                    dtSecurity:"9999999999"
                }
            });
            //For Expandable Collapsible Rows
            $('#example td.control').live( 'click', function () {

                var nTr = this.parentNode;
                //var projID = $(this).closest("td").next().text();
                //console.log(projID);
                //console.log(anOpen);
                //console.log(nTr);
                var i = $.inArray( nTr, anOpen );
                //console.log(i);
                if ( i === -1 ) 
                {
                    $('img', this).attr( 'src', sImageUrl+"details_close.png" );
                    var nDetailsRow = oTable.fnOpen( nTr, fnFormatDetails(oTable, nTr), 'details' );
                    $('div.innerDetails', nDetailsRow).slideDown();

                    anOpen.push( nTr );
                }
                else 
                {
                $('img', this).attr( 'src', sImageUrl+"details_open.png" );
                $('div.innerDetails', $(nTr).next()[0]).slideUp( function () 
                    {
                        oTable.fnClose( nTr );
                        anOpen.splice( i, 1 );
                    });
                }
            });
            function fnFormatDetails( oTable, nTr )
            {
              var oData = oTable.fnGetData( nTr );
              var sOut =
                '<div class="innerDetails">'+
                  '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
                    '<tr><td>Rendering engine:</td><td>'+oData.engine+'</td></tr>'+
                    '<tr><td>Browser:</td><td>'+oData.browser+'</td></tr>'+
                    '<tr><td>Platform:</td><td>'+oData.platform+'</td></tr>'+
                    '<tr><td>Version:</td><td>'+oData.version+'</td></tr>'+
                    '<tr><td>Grade:</td><td>'+oData.grade+'</td></tr>'+
                  '</table>'+
                '</div>';
              return sOut;
            }
        });

    </script> 
  </head> 
  <body> 
    <div style="width:900px">
    <div class="add_delete_toolbar">
        <button id="btnDeleteRow">Delete</button>
    </div>
      <table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
    <thead>
        <tr>
            <th></th>
            <th>Rendering engine</th>
            <th>Browser</th>
            <th>CSS grade</th>
        </tr>
    </thead>
    <tbody></tbody>
    </table>
    </div> 
  </body> 
</html> 

NOTE You may have to change the script and Style URL paths to where yours are located in order to see a working example.

Update: You also need to manually include this anywhere in your HTML source.

<button id="btnDeleteRow">Delete</button>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文