2016-02-05 36 views
-1

我是kendoui领域的新角色。我已经添加自定义验证来检查名称已经存在或没有从网格数据源放入自定义验证之前,每个方法都可以完美工作,但在放置自定义验证之后,自定义验证将检查重复数据并显示错误消息。Kendo ui自定义验证不调用Angularjs应用程序的创建方法

但是对于数据库中不存在的新角色名称。我在验证方法中写入返回true,但不调用webapi的create方法。我检查我的代码,我没有发现任何错误,为什么创建方法没有调用。

<div id="grid"></div> 
    <script> 
     var remoteDataSource = new kendo.data.DataSource({ 
      transport: { 
       read: { 
        url: "http://localhost:8742/api/foo", 
        dataType: "json" 
       }, 
       create: { 
        url: "http://localhost:8742/api/foo", 
        dataType: "json", 
        type: "POST" 

       }, 
       update: { 
        url: "http://localhost:8742/api/foo", 
        dataType: "json", 
        type: "PUT" 
       }, 
       destroy: { 
        url: "http://localhost:8742/api/foo", 
        dataType: "json", 
        type: "DELETE" 
       } 
      }, 
      schema: { 
       model: { 
        id: "Id", 
        fields: { 
         Id: { editable: false, type: "number" }, 
         Name: { 
          validation: { 
           required: true, 
           customValidation: function (input) { 

            var data = remoteDataSource.data(); 
            if (input.is("[name='Name']") && input.val() != "") { 
             for (var i = 0; i < data.length; i++) { 

              if (val == data[i].Name) { 
               dup = data[i].Name; 
               input.attr('data-customValidation-msg', 'Duplicate Name') 
               return false; 
              } 

             } 

            } 
            return true; 

           } 


          } 
         } 
        } 
       } 
      } 
     }); 


     $('#grid').kendoGrid({ 
      dataSource: remoteDataSource, 
      height: 500, 
      toolbar: [{name:"create", text: "Create new foo"}], 
      editable: "popup", 
      columns: [ 
        { 
         field: "Id", 
         title: "Id" 
        }, 
        { 
         field: "Name", 
         title: "Name" 
        }, 
        { 
         command: ["edit","destroy"] 
        } 
       ] 
     }); 

回答

1

用此代码替换您的自定义验证。

customValidation: function (input) { 
            var errorCount = 0; 
            var data = remoteDataSource.data(); 
            input.attr('data-customValidation-msg','Duplicate Name'); 
            if (input.is("[name='Name']") && input.val() != "") { 
             for (var i = 0; i < data.length; i++) 
             { 

              if ($.trim(input.val()) == data[i].Name) 
              { 
                errorCount = 1 ; 
              } 
              else 
              { 
                errorCount = errorCount + 1; 
              } 
             } 
             if(errorCount == data.length) 
             { 
              return errorCount = 1; 
             } 
             else 
             { 
              return errorCount = 0; 
             } 

            } 

           } 
相关问题