使用jquery生成表

时间:2016-10-07 04:52:34

标签: jquery asp.net-mvc

Updated Image

    var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

            subGroup.push(now.getFullYear());

            for (var i = 1; i < 7; ++i) {
                var future = now.setMonth(now.getMonth() + 1, i);
                if ((months[now.getMonth()]) == "January") {
                    subGroup.push((now.getFullYear()))
                }
                subGroup.push(months[now.getMonth()]);
            }
            subGroups.push(subGroup);
        });

        alert(subGroups);
        //debugger;
        var array = subGroups.toString().split(',');

        var arrayLength = array.length;
        var theTable = document.createElement('table');
        tr = document.createElement('tr');


        for (var i = 0, tr, td; i < arrayLength; i++) {

            td = document.createElement('td');
            td.appendChild(document.createTextNode(array[i]));
            tr.appendChild(td);
            theTable.appendChild(tr);
        }

        document.getElementById('table').appendChild(theTable);`

我有一个多选下拉列表,根据下拉列表的选择数量,我必须动态生成表格,如下图所示。未来6个月将根据当月显示。请有人帮帮我!! Dynamic generation of table based on dropdown selection

1 个答案:

答案 0 :(得分:1)

当您的下拉列表发生更改时,您应该使用Knockkout等绑定库,使用月份和空值构建视图模型。

例如: 在您的页面中包含KnockoutJS,从此处下载http://knockoutjs.com/

建立模型,例如:

var MonthViewModel = function(monthID, value) {
    this.monthID= ko.observable(monthID);
    this.monthValue= ko.observable(value);
};

var ViewModel = function(
var that = this;
that.months = ko.obserabaleArray();

that.addMonth = function(month){
  that.months.push(month);
}; 
){};

   var viewModel = new ViewModel();
   ko.applyBindings(viewModel); // This makes Knockout get to work

您的HTML应该是这样的:

<table id="months">
<tr data-bind="foreach:months">
 <td>
   <span data-bind="text:monthID"></span>
   <input type="text" data-bind="value:monthValue"/>
</td>
</tr>
</table>

如果您在更改下拉选项时触发,则应获取所选月份,将其添加到viewModel变量,HTML将自动绑定