【问题标题】:jQuery DataTables Editable - read only if empty valuejQuery DataTables 可编辑 - 仅在空值时读取
【发布时间】:2012-04-11 13:54:48
【问题描述】:

我正在使用jQuery DataTables Editable 来编辑表格中的数据。 (使用 jQuery 1.7.2)数据是从 Asp.net Web 服务中获取的。 (见下面的代码)

当一个值为空时(例如,如果列表中的一个项目没有类别)我不希望该特定项目的类别是可编辑的。因此,该项目的类别应该是只读的。我没找到办法,这可能吗?

<table id="admin_list" cellpadding="0" cellspacing="0" border="0">
    <thead>
        <tr>
            <th>Title</th>
            <th>Category</th>
        </tr>                
    </thead>
    <tbody>
    </tbody>
</table>

<script type="text/javascript">
$(document).ready(function () {
    function renderTable(result) {
        var dtData = [];
        $.each(result, function () {
            dtData.push([
                this.title,
                this.category
            ]);
        });

        $('#admin_list').dataTable({
            'aaData': dtData
        }).makeEditable({
            sReadOnlyCellClass: "read_only",
            sUpdateURL:"Service.svc/update",
            "aoColumns":
            [
                {}, //title
                {} //category
            ]
        });
    }

    $.ajax({
        type: "GET",
        url: "Service.svc/list",
        dataType: "json", cache: false, data: {}, contentType: "application/json; charset=utf-8",
        success: function (data) {
            renderTable(data.d);
        },
        error: function (data) {}
    });
});
</script>

【问题讨论】:

    标签: jquery datatable


    【解决方案1】:

    是的,有一个解决方案。我还提供了一个example

    function renderTable(result) {
        var dtData = [];
        $.each(result, function () {
            dtData.push([
                this.title,
                this.category ? this.category : "&nbsp;"
            ]);
        });
    
        $('#admin_list').dataTable({
            'aaData': dtData
        }).makeEditable();
    }
    $.ajax({
        type: "GET",
        url: "Service.svc/list",
        dataType: "json", cache: false, data: {}, contentType: "application/json; charset=utf-8",
        success: function (data) {
            renderTable(data.d);
        },
        error: function (data) {}
    });
    ​
    $("#admin_list tr").live("mousedown", function() {
        console.log($(this).find("td:eq(1)").text());
        if (/^(?:\s+|Click to edit)?$/.test($(this).find("td:eq(1)").text())) {
            $(this)
                .find("td:eq(1)")
                .empty()
                .unbind();
        }
    });
    

    【讨论】:

    • 谢谢!只是为了确保我解释正确:如果该行的类别为空,我想禁用对该特定行的类别列的编辑。这段代码能解决这个问题吗?
    • 我认为我们彼此误解了。行是指水平列,对吗?我的意思是行作为每个垂直行。因此,如果一行有一个类别为空的类别列,我应该无法编辑该行的类别,但我仍然需要能够编辑其他行的类别。我怎样才能做到这一点?
    • @Martin 到您的第一条评论。是的,它会解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多