I have developed a query tool with an angular form that is filled out and submitted using AJAX to retrieve JSON data. This JSON response is then rendered into ui-grid. Here's an example of the JSON response:
{
"success": true,
"message": "",
"columns": ["first_name", "last_name", "company", "employed"]
"results": [
{first_name: "John", last_name: "Smith", company: "Abc Inc", employed: true},
{first_name: "Johnny", last_name: "Rocket", company: "Abc Inc", employed: true}]
}
While working on both the PHP and angular components, I encountered an issue where if a new dataset is retrieved through a separate AJAX call on the same page, any columns not present in the original dataset do not render. This causes problems as the table is essentially cleared when there are no matching columns, even though I often need to display completely different data in ui-grid within the same page.
Upon receiving the JSON data, I simply bind the jsonResult.results
to the existing $scope.myData
variable which ui-grid is connected to.
I have created a plunker to demonstrate this issue. The initial dataset has a "punk" column, and by clicking "swap data," an attempt is made to load a dataset with an "employee" column instead of "punk." I have explored solutions such as directives to refresh or reload when the $scope.myData variable changes using $watch, as well as investigating options like $scope.columnDefs to inform ui-grid. However, as someone relatively new to angular and javascript, some of these concepts are still challenging for me to grasp.