【问题标题】:Assign array of strings from JSON to Handsontables object in javascript?在javascript中将字符串数组从JSON分配给Handsontables对象?
【发布时间】:2013-05-11 17:07:13
【问题描述】:

我正在尝试将字符串数组分配给 handsontables 列作为自动完成。我从 AJAX 调用中获取此 JSON 数据。

我不能分配给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"],

我不明白如何将数组分配给源。

Reference

【问题讨论】:

    标签: json jquery handsontable


    【解决方案1】:

    你的“回报arr;”不返回“source:”,而是返回“loadBU”函数。

    例如,你可以这样做:

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

    这就是它没有被分配的原因。

    尝试在 $("#example2").handsontable({ 之前进行 Ajax 调用并将其保存到 someVariable,然后设置 source: someVariable

    根据您的 Ajax 调用返回的内容,您可能还需要进行一些操作。例如,我需要循环并加载到一个数组中:

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

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      我是这样使用的:

      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");
      });
      

      分配给源的关键是源函数中的process参数。

      我想补充一点,这种方法会在每次使用时从服务器获取数据。我上面的示例使用了一个没有意义的下拉菜单。但这是使用自动完成时的正确方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-24
        • 2012-02-19
        • 1970-01-01
        • 2011-05-21
        • 1970-01-01
        • 1970-01-01
        • 2010-10-09
        相关资源
        最近更新 更多