【问题标题】:How I write a dynamic URL in kendo UI DataSource like "update/{id}"我如何在 kendo UI DataSource 中编写动态 URL,例如“update/{id}”
【发布时间】:2015-07-14 18:31:50
【问题描述】:

我有一个网络 API。在那我写了一个更新方法。但它需要表行的 id 才能更新到该行。 我使用网格来显示数据并使用工具栏来编辑行。 我的问题是我如何将该 id 传递给更新。 有人可以指导我吗??

【问题讨论】:

  • 请发布您的数据源代码,以便我们为您提供帮助。

标签: kendo-ui kendo-grid kendo-datasource


【解决方案1】:

更新:函数(选项){

                $.ajax( {
                    url: function(data) { return "updateUsuarios/"+data.Id,
                    dataType: "json",
                   .....

【讨论】:

    【解决方案2】:

    我建议你,多解释一下你的问题,但我认为这个例子可能会有所帮助,如果你有一个工具栏作为这样的模板:

      <script type="text/x-kendo-template" id="template">
                <div class="toolbar">
                 <button type="button" id="update">Update</button>
    
                </div>
      </script>
    

    你的“网格”需要attr“工具栏”

        $("#grid").kendoGrid({
            dataSource: dataSource,
            pageable: true,
            height: 550,
            filterable:true,
            toolbar: kendo.template($("#template").html()),
            columns: [
                { field:"username", title: "Username" , width: "120px"  },
                { field: "nombre", title:"Nombre", width: "120px" },
                { field: "apellido", title:"Apellido", width: "120px" },
                { field: "ci", title:"Documento de Identidad", width: "120px" },
                { field: "email", title:"Direccion de Correo", width: "120px" },
                { field: "activo",title:"Estatus", width: "120px" },
                { field: "fecha_caducidad",title:"Fin Demo", width: "120px",template: "#= kendo.toString(kendo.parseDate(fecha_caducidad, 'yyyy-MM-dd'), 'MM/dd/yyyy') #" },
                { field: "licencia_status",title:" ", width: "40px",template:'<img src="assets/images/#:data.licencia_status#.png"/>' },
                { command: ["edit"], title: "&nbsp;", width: "120px" }],
            editable: "popup",
            dataBound: function () {
                var rows = this.items();
                $(rows).each(function () {
                    var index = $(this).index() + 1;
                    var rowLabel = $(this).find(".row-number");
                    $(rowLabel).html(index);
                });
            },
            selectable: true
        });
    

    所以,你可以配置一个剑道按钮并在点击事件中添加功能:

     $("#update").kendoButton({
            click: function(){
    
               //Here you will have the selected row
    
                var self=$('#grid').data('kendoGrid')
                var index = self.items().index(self.select());
                var rowActual= self.dataSource.data()[index];
                rowActual=self.dataItem(self.select()); 
    
    
                if(rowActual==undefined || rowActual ==null) {
    
                    alert("No row selected");
    
    
                }else{
                      $.ajax({
                                type: "POST",
                                url: "update",
                                data: {
                                    id:rowActual.id
                                },
    
                                dataType: 'json',
                                success: function (data) {          
    
    
                                },
                                error: function(){                                  
    
                                }
                            });
                }
    
            }
        });
    

    并在 ajax 中发送行 ID,但如果您使用内联版本更新行,您可以尝试使用这样的数据源

            dataSource = new kendo.data.DataSource({
                transport: {
                    read: function(options) {
                        $.ajax( {
                            url: "readUsuarios",
                            dataType: "json",
                            success: function(result) {
                                options.success(result);
                            }
                        });
    
                    },
                    update: function(options) {
    
                        $.ajax( {
                            url: "updateUsuarios",
                            dataType: "json",
                            data: {
                                models: kendo.stringify(options.data.models)
                            },
                            success: function(data) {
    
                                   // response server; 
    
                            },
                            error: function(result) {
                                // notify the data source that the request failed
                                options.error(result);
                            }
                        });
                    }
                },
                batch: true,
                pageSize: 20,
                schema: {
                    model: {
                        id: "id",
                        fields: {
    
                            username: { editable: false, nullable: true },
                            nombre: { validation: { required: true } },
                            apellido: { type: "string", validation: { required: true} },
                            ci: { type: "string", validation: { required: true} },
                            email: { type: "string", validation: { required: true } },
                            activo: { type: "boolean",editable: false },
                            fecha_caducidad: { type: "date" },
                            licencia_status:{editable: false} 
    
                        }
                    }
                }
            });
    

    希望对你有所帮助!

    【讨论】:

    • 我发现了类似的情况。 Check this 。你能再解释一下吗。我不知道如何传递数据以进行更新。 'update: { url : function (item) { return 'update/' + item.id; } }' 在这里我应该从哪里传递这个“项目”
    • 嗯,当你在 url 中发送参数时,这有效,选择如何发送参数的方式取决于服务器端,如果你有一个 GET 类型的 web 服务,你可以在 url 中发送参数,在您建议的代码中, url 函数可以访问已编辑的项目是一个参数,您不需要传递任何内容,这是自动接收的,因此您可以获得该行的属性
    猜你喜欢
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多