简体   繁体   English

从表格行发布数据,如 json 格式

[英]post data from table row like json format

this is related to my last question ( NOTE: I already got some good answers there).这与我的最后一个问题有关(注意:我已经在那里得到了一些很好的答案)。 I'm doing a program that will filter.我正在做一个过滤程序。 I didn't include this question because i thought that it is easier for me to add text as long as i know how to get the data from the row.我没有包括这个问题,因为我认为只要我知道如何从行中获取数据,我就更容易添加文本。 But to my dismay, I wasn't able to code a good program til now.但令我沮丧的是,直到现在我都无法编写出好的程序。

Im currently using this javascript code (thanks to Awea):我目前正在使用这个 javascript 代码(感谢 Awea):

$('#out').click(function(){    
   $('table tr').each(function(){
      var td = '';
      $(this).find('option:selected').each(function(){
         td = td + ' ' + $(this).text();
      });
      td = td + ' ' + $(this).find('input').val();
      alert(td);
   });
})

my question is: How to add text before the data from the row?我的问题是:如何在行中的数据之前添加文本? like for example, this code alert the first row like data1.1 data1.2 data1.3 ,例如,此代码警告第一行,例如data1.1 data1.2 data1.3

then the second row like data2.1 data2.2 data2.3 ,然后第二行像data2.1 data2.2 data2.3

I want my output to be displayed like this我希望我的 output 像这样显示

[ {"name":"data1.1","comparison":"data1.2", "value":"data1.3"}, {"name":"data2.1","comparison":"data2.2", "value":"data2.3"}, {"name":"data3.1","comparison":"data3.2", "value":"data3.3"} {.....and so on......}]

but before that happen, i want to check if all the FIRST cell in a row is not empty.但在此之前,我想检查一行中的所有第一个单元格是否不为空。 if its empty, skip that row then proceed to next row.如果它为空,请跳过该行然后继续下一行。

is there somebody can help me, please...有没有人可以帮助我,请...

Building on my answer to your previous question , see http://jsfiddle.net/evbUa/1/基于对您上一个问题的回答,请参阅http://jsfiddle.net/evbUa/1/

Once you have your data in a javascript object ( dataArray in my example), you can write the JSON yourself, per my example, but you will find it much easier to use a library such as JSON-js (see this also).一旦您将数据存储在 javascript object(在我的示例中为dataArray ),您可以按照我的示例自己编写 JSON,但您会发现使用JSON-js之类库也更容易)。

// object to hold your data
function dataRow(value1,value2,value3) {
    this.name = value1;
    this.comparison = value2;
    this.value = value3;
}

$('#out').click(function(){   

    // create array to hold your data
    var dataArray = new Array();

    // iterate through rows of table
    for(var i = 1; i <= $("table tr").length; i++){

        // check if first field is used
        if($("table tr:nth-child(" + i + ") select[class='field']").val().length > 0) {

            // create object and push to array
            dataArray.push(    
                new dataRow(
                    $("table tr:nth-child(" + i + ") select[class='field']").val(),
                    $("table tr:nth-child(" + i + ") select[class='comp']").val(),
                    $("table tr:nth-child(" + i + ") input").val())
            );
        }

    }

    // consider using a JSON library to do this for you
    for(var i = 0; i < dataArray.length; i++){
        var output = "";
        output = output + '{"name":"data' + (i + 1) + '.' + dataArray[i].name + '",';
        output = output + '"comparison":"data' + (i + 1) + '.' + dataArray[i].comparison + '",';
        output = output + '"value":"data' + (i + 1) + '.' + dataArray[i].value + '"}';
        alert(output);
    }
})

There are two things you need to do here.您需要在这里做两件事。 First get the data into an array of objects, and secondly get the string representation.首先将数据放入对象数组中,其次获取字符串表示形式。

I have not tested this, but it should give you a basic idea of what to do.我没有对此进行测试,但它应该让您对如何做有一个基本的了解。

Edit Please take a look at this JS-Fiddle example I've made.编辑请看一下我制作的这个 JS-Fiddle 示例。 http://jsfiddle.net/4Nr9m/52/ http://jsfiddle.net/4Nr9m/52/

$(document).ready(function() {
    var objects = new Array();
    $('table tr').each(function(key, value) {
        if($(this).find('td:first').not(':empty')) {
        //loop over the cells
        obj = {};
        $(this).find('td').each(function(key, value) {
                var label = $(this).parents('table').find('th')[key].innerHTML;
                obj[label] = value.innerHTML;

        });
        objects.push(obj);

        }
    });
    //get JSON.
    var json = objects.toSource();
    $('#result').append(json);
});

var a = []; var a = []; a[0] = "data1.1 data1.2 data1.3" a[1] = "data1.6 data1.2 data1.3" var jsonobj = {}; a[0] = "data1.1 data1.2 data1.3" a[1] = "data1.6 data1.2 data1.3" var jsonobj = {}; var c = [] for (var i = 0;i var c = [] for (var i = 0;i

alert(c);警报(c); //it will give ["{"name":"data1.1","comp...1.2","value":"data1.3"}", "{"name":"data1.6","comp...1.2","value":"data1.3"}"] //它将给出 ["{"name":"data1.1","comp...1.2","value":"data1.3"}", "{"name":"data1.6", "comp...1.2","value":"data1.3"}"]

u have to include library for function from JSON.stringify from https://github.com/douglascrockford/JSON-js/blob/master/json2.js你必须包含来自 JSON.stringify 的function的库。

hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM