Jquery-Bootgrid,Row在点击时为空

时间:2015-07-24 11:00:54

标签: jquery jquery-bootgrid

我创建了一个通用函数,所以我可以在不同的页面上使用相同的代码。

我使用Ajax加载数据,当DropDown的值发生变化时刷新我的表。当我单击一行时,该值为null:

这是我的dataTable函数

function dataTable() {

    var self = this;


    self.url = "";
    self.gridObject = null;

    self.Initilize = function (tableDiv, url) {
        self.url = url;
        self.gridObject = $("#" + tableDiv).bootgrid({
            formatters: {
                "commands": function (column, row) {
                    return "<button type=\"button\" class=\"btn btn-sm btn-success command-edit\" data-row-id=\"" + row.Id + "\"><span class=\"fa fa-pencil\"></span></button> " +
                        "<button type=\"button\" class=\"btn btn-sm btn-danger command-delete\" data-row-id=\"" + row.Id + "\"><span class=\"fa fa-trash-o\"></span></button>";
                }
            },
            requestHandler: function (request) {

                var model = fleetMaintenance.filterModel.GetModel();
                model.Current = request.current;
                model.RowCount = request.rowCount;
                model.Search = request.searchPhrase;
                return JSON.stringify(model);
            },
            ajaxSettings: {
                method: "POST",
                contentType: "application/json"
            },
            ajax: true,
            url: self.url,
            selection: true,
        }).on("loaded.rs.jquery.bootgrid", function () {


            self.gridObject.find(".command-edit").on("click", function (e) {
                //   e.stopPropagation();
                alert("You pressed edit on row: " + $(this).data("row-id"));
            }).end().find(".command-delete").on("click", function (e) {
                //   e.stopPropagation();
                alert("You pressed delete on row: " + $(this).data("row-id"));
            });

            self.gridObject.on("click.rs.jquery.bootgrid", function (e, columns, row) {
                console.log(row);

                debugger;
            });
        });
    },
    self.RefreshTable = function () {
        self.gridObject.bootgrid('reload');
    }
}

HTML:

<div class="box">
    <div class="box-header">
        <div class="col-md-6">
            <h3 class="box-title">Sites</h3>
        </div>
    </div>

    <div class="box-body">
        <div class="row">
            <div class="col-md-12">
                <table id="sitesList" class="table  table-condensed table-hover table-striped">
                    <thead>
                        <tr>
                            <th data-column-id="iSiteId" data-identifier="true">Id</th>
                            <th data-column-id="sSiteName" data-order="desc">Site Name</th>
                            <th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>

    </div>

</div>

然后我用它来创建JQGrid表:

<script type="text/javascript">

      var tble = new dataTable();

    $(function () {

        tble.Initialize('sitesList', '/Sites/SitesList')


        $('*[data-action="RefreshTable"]').change(function (e) {
            var dropDown = $(this);

            tble.RefreshTable();

        });

    });

</script>

当用户点击此表中的一行时,我必须重新加载另一个表:但是Row是NULL

enter image description here

  

注意:当我没有使用Ajax加载数据时,这并没有发生:

1 个答案:

答案 0 :(得分:4)

以防其他人遇到此问题:

似乎我们必须在Id列上设置data-type="numeric",如果我删除此数据属性,则单击的行为NULL:

<table id="sitesList" class="table  table-condensed table-hover table-striped">
    <thead>
        <tr>
            <th data-column-id="iSiteId" data-identifier="true" data-type="numeric">Id</th>
            <th data-column-id="sSiteName" data-order="desc">Site Name</th>
            <th data-column-id="sNotes">Notes</th>
            <th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
        </tr>
    </thead>
    <tbody></tbody>
</table>