GetColumnLabel名称并运行ajax函数 - google viz datatable

时间:2014-03-25 00:09:21

标签: javascript jquery ajax google-visualization googlevis

这里我有谷歌可视化DataTable:enter image description here

所以我有这段代码:

function drawTroskovnik() {


  // Create and populate the data table.
  var JSONObject = $.ajax({
    url: 'getTroskovnik.php', // make this url point to the data file
    dataType: 'json',
    data:{id_akt:ajdi},
        async: false,
    type: 'POST',
    }).responseText;

  var data = new google.visualization.DataTable(JSONObject, 0.5);

for (var y = 0, maxrows = data.getNumberOfRows(); y < maxrows; y++) {
    for (var x = 1, maxcols = data.getNumberOfColumns(); x < maxcols; x++) {

      data.setValue(y, x, '<input id="costRedovi" class="form-control" value="'+data.getValue(y,x)+'">');
    }
} 
 for (var y = 0, maxrows = data.getNumberOfRows(); y < maxrows; y++) {

      data.setValue(y, 0, '<input class="span2 form-control" id="pocetak1" size="16" type="text" value="'+data.getValue(y,0)+'" readonly>');

} 


        data.addColumn('string', 'Kontrole');
        for (var y = 0, maxrows = data.getNumberOfRows(); y < maxrows; y++) {
        var mc= data.getNumberOfColumns()-1;
data.setValue(y, mc, '<a data-toggle="modal" data-target="#update" href="#" class="btn btn-success"><i class="fa fa-pencil"></i> Details</a>');

        }
new google.visualization.events.addListener(table, 'ready', function () {
        google.visualization.events.addListener(table.getChart(), 'select', function () {
            var selection = table.getChart().getSelection();
            //HOW TO GET LABEL of COLUMN an DATE from row so to use it in ajax submit
     $( "#costRedovi" ).focusout(function() {
            $.ajax({
            url: "update.php",
            type: "POST",
            async: true, 
            data: { columnName:HOW_to_get_column_name,datum:How_to_get_date_from_row_where_is_selected_cell?}, 
            dataType: "html",

            success: function(data) {
                console.log(data);   
            },  
        });
  })
                        }
        });
    });
  // Create and draw the visualization.
  visualization = new google.visualization.Table(document.getElementById('tpltroskovnik'));
  visualization.draw(data, {'allowHtml': true, cssClassNames: 'nn' }});
}

我需要做什么? 现在我将每个单元格值放入输入字段,因此我可以更改它。我希望.focusout能够在数据库中运行.ajax函数到update数据,但为了做到这一点,我需要get Column Label Name and date(正如您在照片上看到的那样 - 日期是& #39; 0&#39;排在行中......)

如何获得聚焦的单元格的列标签名称和日期?

2 个答案:

答案 0 :(得分:1)

您需要做的是就您在选择处理程序中放入表中的数据调用getValue。相关代码是:

        var row = table.getSelection()[0].row;
        console.log(data.getValue(row,0));

查看这个小提琴,如果您有更多问题请告诉我:

http://jsfiddle.net/WTb8H/

答案 1 :(得分:0)

解决这个问题很容易,所以我以更好的方式获得了columnLabel:

for (var y = 0, maxrows = data.getNumberOfRows(); y < maxrows; y++) {
    for (var x = 1, maxcols = data.getNumberOfColumns(); x < maxcols; x++) {

      data.setValue(y, x, '<input id="costRedovi" kol="'+ data.getColumnLabel(x) +'" class="form-control" value="'+data.getValue(y,x)+'">');
    }
} 

所以解决方案是:data.getColumnLabel(x),现在每个输入都有属性kol,现在很容易在数据表就绪时运行ajax代码。