尝试从Excel文件读取后在observableArray中推送项目,但不是所有数据都被推送

时间:2016-05-04 16:20:22

标签: html excel knockout.js ado knockout-3.0

下表也是我的Excel文件的格式

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <button data-bind="click: GetCount">Get Tickets</button>
    <table>
        <thead>
        <tr>
            <th>
                #REQ
            </th>
            <th>
                #RITM
            </th>
            <th>
                #TASK
            </th>
            <th>
                Start Date
            </th>
            <th>
                End Date
            </th>
            <th>
                Status
            </th>
            <th>
                Comments
            </th>
        </tr>
           </thead>

         <tbody data-bind="foreach: AddNewTicket">
             <tr>
                 <td data-bind="text: req"></td>
                 <td data-bind="text: ritm"></td>
                 <td data-bind="text: task"></td>
                 <td data-bind="text: sDate"></td>
                 <td data-bind="text: eDate"></td>
                 <td data-bind="text: status"></td>
                 <td data-bind="text: comments"></td>
             </tr>
         </tbody>
    </table>

    <script src="knockout-3.4.0.js" type="text/javascript"></script>
    <script src="ViewTicketsJS.js" type="text/javascript"></script>
</body>
</html>

JS文件如下:

function AddNewTicketFunc(req, ritm, task, sDate, eDate, status, comments) {
    var self = this;
    self.req = req;
    self.ritm = ritm;
    self.task = task;
    self.sDate = sDate;
    self.eDate = eDate;
    self.status = status;
    self.comments = comments;

}

function AppViewModel() {
    var self = this;
    self.username = ko.observable();
    self.REQ = ko.observable();;
    self.RITM = ko.observable();
    self.TASK = ko.observable();
    self.user = ko.observable();
    self.URL = ko.observable();
    self.access = ko.observable();
    self.Description = ko.observable();



    self.AddNewTicket = ko.observableArray([/*
        new AddNewTicketFunc("1", "2", "3", "4", "5", "6", "7")*/
        ]);

    self.GetCount = function () {
        var cn = new ActiveXObject("ADODB.Connection");
        var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:/Users/nairas2.dv/Documents/Visual Studio 2012/Projects/TaskList.xlsx;Persist Security Info=False;Extended Properties=Excel 8.0;"
        cn.Open(strConn);
        var rs = new ActiveXObject("ADODB.Recordset");
        var SQL = "select * from [TicketsTable$]";
        rs.Open(SQL, cn);
        //rs.Open(cn);
        if (rs.bof) {
            document.write('No records available for this query');
        }

        if (!rs.bof) {

            rs.MoveFirst()
            while (!rs.eof) {
                var count = rs.fields.count;
                alert(rs.fields(0).value + rs.fields(1).value + rs.fields(2).value+ rs.fields(3).value+ rs.fields(4).value+ rs.fields(5).value+ rs.fields(6).value);
                //alert(count);
               /* for (var i = 0; i != rs.fields.count; ++i) {
                    document.write(rs.fields(i).value + " | ");
                }*/

                    //this.AddNewTicket.push(new AddNewTicketFunc(rs.fields(--count).value, rs.fields(--count).value, rs.fields(--count).value, rs.fields(--count).value, rs.fields(--count).value, rs.fields(--count).value, rs.fields(--count).value));
                self.AddNewTicket.push(new AddNewTicketFunc(rs.fields(0).value, rs.fields(1).value, rs.fields(2).value, rs.fields(3).value, rs.fields(4).value, rs.fields(5).value, rs.fields(6).value));
        //self.AddNewTicket.push(new AddNewTicketFunc("1", "2", "3", "4", "5", "6", "7"));

              //  document.write("<br />");
                rs.MoveNext();
            }
        }
        rs.Close();
        cn.Close();
    }
}

ko.applyBindings(new AppViewModel());

在调试时,我在推送功能

中发现了错误

以下是输出的屏幕截图: As you can see the remaining fields are blank

1 个答案:

答案 0 :(得分:0)

问题在您定义的函数中,在您定义的函数中添加 return self;

 function AddNewTicketFunc(req, ritm, task, sDate, eDate, status, comments) {
    var self = this;

    self.req = req;
    self.ritm = ritm;
    self.task = task;
    self.sDate = sDate;
    self.eDate = eDate;
    self.status = status;
    self.comments = comments;

    return self;
}