如何使用javascript在datatable中呈现dataSet

时间:2016-03-21 16:32:13

标签: javascript servlets datatables

我尝试连接字符串以将其传递给数据表,我从servlet获取数据,当我放置静态字符串时它可以工作但我不知道如何使用循环来处理它,因为它& #39;动态数据

function getMsgHandler(responseTxt, statusTxt, xhr) {
    alert(responseTxt);
    if (statusTxt === "success") {
        products = responseTxt;
        var s = "";
        for (i = 0; i < products.length; i++) {
            if (i === 0) {
                s = s + '[' + products[i].productID + ', "' + products[i].name + '", "' + products[i].description + '", "' + products[i].img + '", ' + products[i].price + ' ]';
            } else {
                s = s + ', [' + products[i].productID + ', "' + products[i].name + '", "' + products[i].description + '", "' + products[i].img + '", ' + products[i].price + ']';
            }
        }
        dataSet = '[ ' + s + ' ]';
        $('#example').DataTable({
            data: dataSet
        });
    }
}

我希望它像这种格式

var dataSet = [
    ["m", "System Architect", "Edinburgh", "5421", "2011/04/25", "$320,800"],
    ["Ashton Cox", "Junior Technical Author", "San Francisco", "1562", "2009/01/12", "$86,000"],
    ["Cedric Kelly", "Senior Javascript Developer", "Edinburgh", "6224", "2012/03/29", "$433,060"],
    ["Airi Satou", "Accountant", "Tokyo", "5407", "2008/11/28", "$162,700"]
];

1 个答案:

答案 0 :(得分:0)

请改用以下代码:

<script>
    $(function () {

        $('#dropdownid').on('change', function () {
            var selected_text=$( "#dropdownid option:selected" ).text();
            selected_text=selected_text.split('|');
            $('#textfieldid').val(selected_text[2]);
        });
    })
</script>