Alasql: Object.alasql.from.FILE Wrong usage of FILE() function

Created on 23 Nov 2016  路  10Comments  路  Source: agershun/alasql

  loadFile(event) {
            alasql('SELECT * FROM FILE(?,{headers:true})', [event], function (data) {
                console.log(data);
                // You can data to div also.
            });
        }
 let buttonUpload = $("#uploadBtn");
 buttonUpload.on("change", (evt) => this.loadFile(evt));

the code is above

I am using the above code in controller of angular js 1.5 and typescript app

please help

All 10 comments

Please ask question like this on Stack Overflow

Wrong usage of FILE() is thrown by alasql

Have a look at http://jsfiddle.net/3ve90afo/

I beleave your problem is with how angular is used in conjunction with alasql

There is issue while working with angularjs. You can not directly call anglarjs method with plane javascript. Same $event doesn not work with ng-change. As Angular recommend, I come to a solution. As they say, create directive for any native javascript/jquery event binding. Please refer to below given code.

angular.directive('nngFileUpload', ['$rootScope', function ($rootScope) {
    return {
        restrict: 'A',
        scope: {},
        link: function (scope, elm, attrs) {
            //elm should be input field else find input field(elm.find("input[type='file']"))
            elm.on('change', function (event) {
                $rootScope.$emit("FILE_UPLOADED", event.originalEvent);
            });
        }
    }
}]);

```js
//in controller
$rootScope.$on('FILE_UPLOADED', function(event, originalEvent){
alasql('SELECT RECORDSET * FROM FILE(?,{headers:true})',[originalEvent],function(data){
console.log("Data from file=",data.columns);
});
});

```html
<!-- HTML syntax-->
<input type="file" value="alasqlupload" nng-file-upload>

Note: Things you should note down here. alasql does not work with jquery event, so pass _event.originalEvent_ instead of event.

nice - thanks for a great description.

WOuld it make senese to let alasql check IF there is a .originalEvent and use that instead - or does it make more sense to learn people to give the original event?

Not Sure man :) Just make a check. It will be convenient for a user but people might used other library apart from jquery. So just make a note on the README.md. I will suggest.

Or i can write angular version of your module :-D what you say ?

The README it is...

For people working wil alasql and react.js, then you need to get the nativeEvent from the event, otherwise it will give you a error

    window.alasql('SELECT * FROM FILE(?,{headers:true})', [e.nativeEvent], function (data) {
      console.log(data)
    });

There is issue while working with angularjs. You can not directly call anglarjs method with plane javascript. Same $event doesn not work with ng-change. As Angular recommend, I come to a solution. As they say, create directive for any native javascript/jquery event binding. Please refer to below given code.

angular.directive('nngFileUpload', ['$rootScope', function ($rootScope) {
    return {
        restrict: 'A',
        scope: {},
        link: function (scope, elm, attrs) {
            //elm should be input field else find input field(elm.find("input[type='file']"))
            elm.on('change', function (event) {
                $rootScope.$emit("FILE_UPLOADED", event.originalEvent);
            });
        }
    }
}]);
//in controller
$rootScope.$on('FILE_UPLOADED', function(event, originalEvent){
    alasql('SELECT RECORDSET * FROM FILE(?,{headers:true})',[originalEvent],function(data){
        console.log("Data from file=",data.columns);
    });
});
<!-- HTML syntax-->
<input type="file" value="alasqlupload" nng-file-upload>

Note: Things you should note down here. alasql does not work with jquery event, so pass _event.originalEvent_ instead of event.

Instead of $rootScope you should use $scope. $rootScope events aren't destroyed when you leave the page and so can be fired twice.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

daffodilistic picture daffodilistic  路  3Comments

karubimania picture karubimania  路  6Comments

thierrygervais picture thierrygervais  路  6Comments

SWard1992 picture SWard1992  路  3Comments

Serge-SDL picture Serge-SDL  路  4Comments