【问题标题】:Adding row in jquery-datatables-editable plugin在 jquery-datatables-editable 插件中添加行
【发布时间】:2012-10-08 06:53:24
【问题描述】:

我正在使用插件this 添加新记录。在此示例中,单击“添加”按钮时,将打开一个子表单,其中包含表中的字段。在子表单中单击“确定”后,将在表中创建一个新的可编辑行,其中包含子表单中提到的值。 但是我需要在不打开子表单的情况下添加一个可编辑的行。用户必须在表中输入值。 此代码用于从“jquery.dataTables.editable.js”文件中的子表单添加一行。

///Event handler called when a new row is added and response is returned from server
        function _fnOnRowAdded(data) {
            properties.fnEndProcessingMode();

            if (properties.fnOnNewRowPosted(data)) {

                var oSettings = oTable.fnSettings();
                var iColumnCount = oSettings.aoColumns.length;
                var values = new Array();

                $("input:text[rel],input:radio[rel][checked],input:hidden[rel],select[rel],textarea[rel],span.datafield[rel]", oAddNewRowForm).each(function () {
                    var rel = $(this).attr("rel");
                    var sCellValue = "";
                    if (rel >= iColumnCount)
                        properties.fnShowError("In the add form is placed input element with the name '" + $(this).attr("name") + "' with the 'rel' attribute that must be less than a column count - " + iColumnCount, "add");
                    else {
                        if (this.nodeName.toLowerCase() == "select" || this.tagName.toLowerCase() == "select")
                            sCellValue = $("option:selected", this).text();
                        else if (this.nodeName.toLowerCase() == "span" || this.tagName.toLowerCase() == "span")
                            sCellValue = $(this).html();
                        else
                            sCellValue = this.value;

                        sCellValue = sCellValue.replace(properties.sIDToken, data);
                        values[rel] = sCellValue;
                    }
                });

                //Add values from the form into the table
                var rtn = oTable.fnAddData(values);
                var oTRAdded = oTable.fnGetNodes(rtn);
                //Apply editable plugin on the cells of the table
                _fnApplyEditable(oTRAdded);
                //add id returned by server page as an TR id attribute
                properties.fnSetRowID($(oTRAdded), data);
                //Close the dialog
                oAddNewRowForm.dialog('close');
                $(oAddNewRowForm)[0].reset();
                $(".error", $(oAddNewRowForm)).html("");

                _fnSetDisplayStart();
                properties.fnOnAdded("success");
            }
        }

我编辑了上面的代码来添加一行而不打开子表单。但添加的行不可编辑。我应该做哪些更改才能使其可编辑?

【问题讨论】:

    标签: jquery jquery-plugins row jeditable


    【解决方案1】:

    使用 fnAddData 添加行,然后使该行可编辑。这是一个example,它显示了如何使行可编辑。

    添加行的未经测试的代码(取自 jquery 数据表文档)

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

    编辑 2:这是fiddle

    var oTable = $("table").dataTable();
    
    $("a").click(function() {
        $(oTable).dataTable().fnAddData( ["test"] );
    });
    
    $('td', oTable.fnGetNodes()).editable(function(v, s) {
        console.log(v);
        return (v);
    });
    

    请注意,您需要jEditable

    干杯!

    【讨论】:

    • 我已经尝试过“DataTables 编辑示例”。有一个问题。如果我编辑一个元素,将调用editable_ajax.php 来更新服务器中的记录。我想要做的是仅在用户提交表单时更新记录,直到那时所做的更改应该显示在浏览器中。
    • 如果您检查上面的小提琴链接,当您编辑单元格时,它们不会被提交。基本上我删除了 ajax 调用。
    • jsfiddle.net/GrfPB/5。我试过这个。但是在编辑文本'main'之后,我得到了错误。编辑不正确。还有一个新添加的行不可编辑。
    • 编辑此行后,我在此行中获取此值。 "{"error": "Shell 表单不验证{'html_initial_name': u'initial-js_lib', 'form': , 'html_name': 'js_lib', 'label': u'Js lib', 'field' : , 'help_text': '', 'name': 'js_lib'}"}"
    • 检查新的小提琴链接。 :)
    猜你喜欢
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    相关资源
    最近更新 更多