如何从表中获取选定的行值

时间:2017-01-17 08:29:57

标签: javascript jquery asp.net-mvc

我正在将HTML表转换为数组并将其传递给控制器​​以插入多行。我能够创建数组,但问题是它是在创建一个完整的表数组,但我只想获得所选的行td值。

//function to convert HTML table to  array//
var HTMLtbl = {
    getData: function (table) {
        var data = [];
        table.find('tr').not(':first').each(function(rowIndex, r) {
            var cols = [];

            // I believe the main problem is here:
            $(this).find('td').each(function(colIndex, c) {
                cols.push($(this).text().trim());
            });
            data.push(cols);
        });
        return data;
    }
}

$(document).on('click', '#btnsave', function() {
    var data = HTMLtbl.getData($('#tblresult'));
    var parameters = {};
    parameters.array = data;

    var request =  $.ajax({
        async: true,
        cache: false,
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "../Home/Save_SearchCarsDocs",        
        data: JSON.stringify(parameters)
    });
    request.done(function(msg) {
        alert("Row saved " + msg.d);
    });

2 个答案:

答案 0 :(得分:0)

你可以试试......

    //function to convert HTML table to  array//
   var excludedTD_Index = [0,5,7]; // define what you want to exclude by index
    var HTMLtbl = {
        getData: function (table) {
            var data = [];
            table.find('tr').not(':first').each(function(rowIndex, r) {
                var cols = [];

                // I believe the main problem is here:
                $(this).find('td').each(function(colIndex, c) {
                    if(excludedTD_Index.indexOf(colIndex) >=0) // exclude TD
                    continue;
                    cols.push($(this).text().trim());
                });
                data.push(cols);
            });
            return data;
        }
    }

    $(document).on('click', '#btnsave', function() {
        var data = HTMLtbl.getData($('#tblresult'));
        var parameters = {};
        parameters.array = data;

        var request =  $.ajax({
            async: true,
            cache: false,
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "../Home/Save_SearchCarsDocs",        
            data: JSON.stringify(parameters)
        });
        request.done(function(msg) {
            alert("Row saved " + msg.d);
        });

如果您可以将以下数据添加到TD标记

,则可以使其更易于管理
<td data-exclude="1"> 
...

添加上述数据后,您可以按以下方式排除这些列

 var HTMLtbl = {
        getData: function (table) {
            var data = [];
            table.find('tr').not(':first').each(function(rowIndex, r) {
                var cols = [];

                // I believe the main problem is here:
                $(this).find('td').each(function(colIndex, c) {
                    if($(this).data("exclude") == 1) // exclude TD
                    continue;
                    cols.push($(this).text().trim());
                });
                data.push(cols);
            });
            return data;
        }
    }

答案 1 :(得分:0)

感谢Rory McCrossan终于得到了我的回答我正在添加我的解决方案

     var CB=1;
var HTMLtbl =
    {
        getData: function (table) {
            var data = [];

            table.find('tr').not(':first').each(function (rowIndex, r) {
                if ($("#chk_" + CB).is(':checked'))
                    {
                    var cols = [];
                    $(this).find('td').each(function (colIndex, c) {                      
                        cols.push($(this).text().trim());     

                    });
                    data.push(cols);
                }
                CB++;
            });
            CB = 1;
            return data;

        }
    }