2013-05-16 1 views
1

문자열의 배열을 handsontables 열에 자동 완성으로 지정하려고합니다. AJAX 호출에서이 JSON 데이터를 가져오고 있습니다.JSON의 문자열 배열을 javascript의 Handsontables 객체에 할당 하시겠습니까?

source:에 할당 할 권한이 없습니다. 코드를 따르십시오.

var loadBU = function(data) {   
       $.ajax({ 
       url: "/EditInitiatives.svc/GetBUData", 
       data: "clientId=" + $value.val(), 
       type: "GET", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (res) {      
        data(res); 
       }, 
       error: function (error) { 
        alert("Error: " + error.responseText); 
       } 
       });  
       };  

    $("#example2").handsontable({ 
     data: getCarData(), 
     startRows: 7, 
     startCols: 4, 
     columns: [ 
        { data:'BusinessUnit', 
        type:'autocomplete', 
        source:loadBU(function(output){        
           var results = output.d       
           var arr = [], item; 
           for (var i = 0, len = results.length; i < len; i++) { 
            item = results[i]; 
            arr.push([[item]]); 
           } 
           return arr; 
          }),  
         strict: true 
        }, 
       ] 
     }); 

내가 소스에 배열을 할당하는 방법을 이해하지 않는 EX: source: ["yellow", "red", "orange", "green", "blue", "gray", "black", "white"],

enter image description here

같은 것으로 가정합니다.

Reference

답변

0

귀하의 "반환 편곡;" "source :"로 돌아 가지 않으면 "loadBU"기능으로 돌아갑니다.

예를 들어, 당신은 할 수 :이 할당되지 않는 이유

   success: function (res) {      
       var arr = data(res); 
      }, 

이입니다.

$("#example2").handsontable({ 전에 Ajax 호출을 시도하고 someVariable에 저장 한 후 Ajax 호출이 반환, 당신은 또한 몇 가지 조작을해야 할 수도 있습니다 무엇에 따라 source: someVariable

을 설정합니다. 예를 들어, 나는 통해 루프 필요하고 배열로로드 :이

0

도움이되기를 바랍니다

function AppendToArray(ajaxValues, theArray) { 
    for (var i = 0; i < ajaxValues.length; i++) { 
     theArray.push('' + ajaxValues[i].Text + ''); 
    } 
} 

나는이처럼 사용

var workers = null; 
$.ajax({ 
    url: siteUrl + "/Worker/Get", 
    dataType: 'json', 
    type: 'GET', 
    cache: false 
}) 
.done(function (data) { 
    $("#worker-grid").handsontable({ 
     data: data, 
     rowHeaders: true, 
     colHeaders: ["internal<BR />identification", "name", "mobile", "e-mail address", "national<BR />identification", "partner", "source"], 
     colWidths: [100, 150, 100, 250, 150, 150, 100], 
     columns: [ 
      { data: "intId" }, 
      { data: "name" }, 
      { data: "mobile" }, 
      { data: "mail" }, 
      { data: "extId" }, 
      { 
       data: "partner", type: 'dropdown', source: function (query, process) { 
        $.ajax({ 
         url: siteUrl + "/Partner/Get", 
         dataType: 'json', 
         type: 'GET', 
         cache: false 
        }) 
        .done(function (data) { 
         var values = []; 
         for (i in data) values.push(data[i].name); 
         process(values); 
        }); 
       } 
      }, 
      { data: "source" } 
     ], 
     columnSorting: true, 
     minSpareRows: 1 
    }); 
    workers = $("#worker-grid").data("handsontable"); 
}); 
소스에 할당의 핵심은

소스 함수의 프로세스 매개 변수

그러나이 접근법은 모든 용도로 서버에서 데이터를 가져 오는 것으로 추가하고 싶습니다. 위의 예제는 이해할 수없는 드롭 다운을 사용합니다. 그러나 자동 완성과 함께 사용할 때는 올바른 방법입니다.