jQuery DataTables 可编辑 - 如果值为空则只读



我正在使用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>

是的,有一个解决方案。我还举了一个例子。

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();
    }
});

相关内容

最新更新