如何覆盖编辑行并在jsgrid中调用自定义编辑

时间:2018-09-05 04:34:06

标签: jsgrid

对此How to customize edit event in JsGrid进行了如下尝试。但似乎不起作用

$("#jsgrd").jsGrid({
  data: ds,
  fields: [{
    type: "control",
    editItem: editrow(item)
  }, ]
});

function editrow(item) {
  var $row = this.rowByItem(item);
  if ($row.length) {
    console.log('$row: ' + JSON.stringify($row)); // I modify this
    this._editRow($row);
  }
}

我现在得到的错误是未定义“项目”。

我要寻找的是,当用户单击“编辑”时,我想将rowid存储在一个隐藏的col中,并使用它从服务器中获取更多数据并在jsgrid外部进行填充。并避免将行更改为编辑模式

2 个答案:

答案 0 :(得分:3)

您没有使用记录的方式。您应该使用editTemplate

一个简单的工作示例是:

 $(document).ready(function() {
 $("#grid").jsGrid({
    width: "100%",
    editing: true,
    autoload: true,
    data: [ { id:1, name:"Tom"}, {id:2, name:"Dick"}],
    fields: [
             { name: "id", type: "text", title: "Id"},
             { name: "name", type: "text", title: "Name", 
                 editTemplate: function(item) {
                   return "<input type='checkbox'>" + item;
             }},
             { type: "control"}
           ]
     });
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid-theme.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.js"></script>

<div id="grid">
Test
</div>

出于说明目的,我将名称字段的编辑从标准文本框中转换为复选框。

答案 1 :(得分:2)

您可以使用itemTemplate来获得所需的结果。

  • itemTemplate 是用于创建单元格内容的功能。它应该以字符串,DomNode或jQueryElement的形式返回标记。函数签名为function(value, item),其中value是数据项的列属性的value,而item是行数据item

itemTemplate内,您可以根据需要自定义dom元素。

运行演示

$("#jsGrid").jsGrid({
    width: "100%",
    height: "auto",
    paging: false,

    //for loadData method Need to set auto load true
    autoload: true,

    noDataContent: "Directory is empty",

    controller: {
        loadData: function(filter) {
            var data = [{
                id: 1,
                nickname: "Test",
                email: "t@gmail.com"
            }, {
                id: 2,
                nickname: "Test 1",
                email: "t1@gmail.com"
            }, {
                id: 3,
                nickname: "Test 2",
                email: "t2@gmail.com"
            }, {
                id: 4,
                nickname: "Test 3",
                email: "t3@gmail.com"
            }];
            return data;
        }
    },

    fields: [{
        name: "nickname",
        type: "text",
        width: 80,
        title: "Name"
    }, {
        name: "email",
        type: "text",
        width: 100,
        title: "Email Address",
        readOnly: false
    }, {
        type: "control",

        itemTemplate: function(value, item) {
            var editDeleteBtn = $('<input class="jsgrid-button jsgrid-edit-button" type="button" title="Edit"><input class="jsgrid-button jsgrid-delete-button" type="button" title="Delete">')
                .on('click', function (e) {
                    console.clear();
                    if (e.target.title == 'Edit') {
                        //Based on button click you can make your customization  
                        console.log(item); //You can access all data based on item clicked
                        e.stopPropagation();
                    } else {
                        //Based on button click you can make your customization
                        console.log('Delete')
                    }
                });

            return editDeleteBtn; //
        },
    }]
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.css" />

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid-theme.min.css" />

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.js"></script>

<div id="jsGrid"></div>