Once an item is added, the table vanishes into thin air

I have a DataTables setup to show a list of project names, and I have a button that should add an item to the displayed array. However, when I click the button, the table disappears.

Below is the code snippet:

view.html

<button ng-click="vm.add()">Add item</button>
<table datatable="ng" dt-instance="vm.dtInstance" dt-options="vm.dtOptions" dt-column-defs="vm.dtColumnDefs"
       class="table table-hover table-striped table-bordered table-condensed">
    <thead>
    <tr style="background-color: #00b3ee; color: white; border:none">
        <th style="text-align: center">Nom</th>
        <th style="text-align: center">Actions</th>
    </tr>
    </thead>
    <tbody>
    <tr ng-repeat="project in vm.project.projects">
        <td style="width: 90%; vertical-align: middle">{{project}}</td>
        <td align="center" style="vertical-align: middle">
            <button data-toggle="modal" ng-click="vm.setProject(project)"
                    data-target="#deleteEureciaProjectModal" type="button" class="btn btn-danger">
                <i class="fa fa-trash">&nbsp;&nbsp;</i>Supprimer
            </button>
        </td>
    </tr>
    </tbody>
</table>

controller.js

vm.add = () => vm.project.projects.push('new project');

fetchProject().then(result => {
    vm.project = result;

    // vm.project = {
    //     projects: [
    //         "project1",
    //         "project2",
    //         "project3",
    //         "project4"
    //     ],
    //     etc...
    // }

    vm.project.projects.push("newProject"); //it's working here, which is normal

    //init datatable
    vm.dtInstance = {};
    vm.dtOptions = DTOptionsBuilder.newOptions()
        .withBootstrap();
    vm.dtColumnDefs = [];
    for (var i = 0; i < 2; i++) {
        if (i == 1) {
            vm.dtColumnDefs.push(DTColumnDefBuilder.newColumnDef(i).notSortable());
        } else {
            vm.dtColumnDefs.push(DTColumnDefBuilder.newColumnDef(i));
        }
    }
})

I've attempted using objects instead of strings, but that didn't solve the issue. I'm unsure of what's causing this problem. I'm unable to utilize server-side processing, and I need to find a solution within Angular methods.

Answer №1

After some investigation, I managed to solve the issue:

By initializing vm.project.project before making the promise call, the problem was resolved. I created the vm.project object and added the project property to it with an empty array. This allowed me to add items without encountering the disappearing table issue.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to apply a hover effect to a specific element?

Within my CSS stylesheet, I've defined the following: li.sort:hover {color: #F00;} All of my list items with the 'sort' class work as intended when the Document Object Model (DOM) is rendered. However, if I dynamically create a brand new ...

A valid path is required in the form of a string when working with the HTTP module in Node.js

Currently, I'm working on an update checker that doesn't involve downloading the update. My main goal is to compare the version in the package.json file on GitHub with the one in my Electron app. However, when using this code, I encountered a "p ...

AngularJS's $resource module returns an empty array as a response

I am trying to display a table with items from JSON data. I have created a Service that returns the JSON data. In my controller, I am querying the Service to receive an array of data. It's a little confusing because I am putting the response in a new ...

Looking for assistance with transferring a data attribute to a form redirection

I'm seeking assistance with a coding dilemma I've encountered. To provide some background, I have a list of items on my website, each featuring a 'Book Now' button that redirects users to different pages. Recently, I incorporated a mod ...

Dealing with problematic hover behaviors in Cypress: A guide

I encountered an issue with Cypress hover functionality while trying to access a sub menu that appears after hovering over a main menu item. The error message I received was This element is not visible because it has CSS property: position: fixed and it&ap ...

actions with frontend routing for CRUD operations

Imagine you are creating a simple CRUD todo application. Whether you choose to use Angular, React, or Vue for routing, the setup will be similar: /todos => see all todos /todos/:id => view one todo by id /todos/:id/edit => edit one todo by id /todos/new ...

How to build custom middleware with parameters in Node.js

I'm working on creating a middleware in nodejs for access levels, and I've written the following middleware: class AccessUser extends middlware { async AccessUser(access,req, res, next) { const getTokenFrom = (req) => { const autho ...

The functionality of v-tooltip ceases to operate when the element is deactivated

<button v-tooltip="'text'" :disabled=true>Some button</button> Can you provide an explanation for why the button is disabled without disabling the tooltip as well? ...

Google Maps autocomplete feature is causing the input to update only after clicking in ng-model

After fetching a Google Maps place ID from a REST API, I utilize the Google Maps API to retrieve the place object in this manner: var geocoder = new google.maps.Geocoder; geocoder.geocode({ 'placeId': data.city }, fun ...

Developing web components using angular.js while ensuring compatibility with IE11

I have an angular.js application where I need to initialize a web component. Everything runs smoothly on different browsers, however IE11 seems to have problems with document.importNode The angular.js onInit function is as follows: vm.$onIni ...

A TypeError was not captured when attempting to access information about Ionic1 through the angular.module

For the past 48 hours, I've been struggling with an issue that's really getting on my nerves. If anyone knows how to fix this, it would be greatly appreciated: I'm working on an Ionic 1 project and need to make some updates, but nothing I t ...

The latest error in the Next.js 13 app directory: React child is not valid (detected: [object Promise])

I am currently utilizing the new app directory feature in Next.js 13. Within my project, I have a page component located at app/page.tsx. This component contains the following code snippet: "use client"; import { useState } from "react" ...

Vue.js component mismatch in the layout

Attempting to set up a Vue application with vuetify while incorporating layouts. As a newcomer to Vue, I may have made some beginner errors. Here is the structure of my app: main.js // The Vue build version to load with the `import` command // (runtime- ...

Utilizing the document.createDocumentFragment() method along with innerHTML for manipulating a Document Object Model (

I am currently in the process of creating a document fragment using the code below: let fullHTMLDocument = '<!doctype html> <html><head></head><body><h1>hello world</h1></body></html>'; let f ...

Is using $timeout still considered the most efficient method for waiting on an Angular directive template to load?

When it comes to waiting for a directive's template to render, our team has been following the approach of enclosing our DOM manipulation code in a $timeout within the directive's link function. This method was commonly used in the past, but I&ap ...

Click the button in Javascript to add new content

I am currently experimenting with JavaScript to dynamically add new content upon clicking a button. Although I have successfully implemented the JavaScript code to work when the button is clicked once, I would like it to produce a new 'hello world&ap ...

We encountered an error: The function setName is not defined

I am working on some code where I have defined a function, but it is still showing me errors related to setname, setemail, and password. import React, {useState} from 'react' import './Auth.css' import icon from '../../assets/logo. ...

Tips for transmitting one or multiple data using jquery.ajax

I found this code on stackoverflow and it is working well for uploading multiple files. However, I am facing an issue where I cannot send additional parameters along with the file upload. Despite trying various methods, I have been unsuccessful in sending ...

What are the distinctions between manually and programmatically changing URLs in Angular 6?

My query pertains to differentiating navigation methods, specifically between clicking a button with [routerLink] and manually entering a URL in the browser's search bar. Update: I have a fixed menu on a certain page that appears as follows: <ul& ...

Display personalized error messages using jQuery and PHP

I implemented an Ajax autocomplete feature on an input field within a form using jQuery. Here are the ajax settings I have set up: $.ajax({ type: "POST", url: myUrl, data: $("#id__form").serialize(), success: function(data){ ...