# Change DataSource Dynamically

A tutorial showing how to dynamically change the data source of a TableSheet in SpreadJS

## Content

TableSheet provides the ability to inject the corresponding data from the data source in real-time without refreshing the webpage. This feature, for example, helps you to switch the data sources to a backup server in case the live server returns a connection error and many other scenarios.
To update the data source dynamically, follow the steps below:

1. Invoke the **fromJSON** method.

    ```JavaScript
    spread.fromJSON(spreadJson);
    ```
2. Update the table datasource option by wrapping the datasource as an object in **Promise**’s **resolve** method.

    ```JavaScript
    let myTable = spread.dataManager().tables["myTable"];
    myTable.options = { 
       remote: {               
           read: function () { 
               return Promise.resolve(dataSource); 
                   } 
             } 
    };
    ```
3. Invoke the table’s **fetch** method with the new reload argument. This step refreshes the datasource and calls the **setDataView** method with an updated data source.

    ```JavaScript
    myTable.fetch(true).then(function()
    {
        let myView = myTable.views["myView"];
        let sheet = spread.getActiveSheetTab();
        sheet.setDataView(myView);
    });
    ```

> **Note**: View’s fetch method and Table’s fetch method have the same new reload argument. Hence, the setDataView method helps in clearing the cache status after fetching the data view again.