AlpacaJS Tables - Populating tables with dynamic data and incorporating placeholders

Recently, I posted this query on Alapaca's Stack Overflow. However, considering their busy schedule and slow response rate, I am hopeful that someone else here can offer assistance with my question.

The issue at hand involves a table whose number of rows is determined by another element through a postRender script. Despite pushing values into the elements, I am unable to get them set properly. Below is the code snippet:

Schema

"required": false,
"properties": {
  "study_group_radio": {
    "enum": [
      "2",
      "3",
      "4",
      "5",
      "6"
    ]
  },
  "study_group_table": {
    "type": "array",
    "required": false,
    "items": {
      "type": "object",
      "properties": {
        "study_group": {
          "title": "Study Groups",
          "type": "number",
          "readonly": true
        },
        "dose": {
          "title": "Dose",
          "type": "string"
         },
        "route": {
          "title": "Route",
          "type": "string"
        },
        "doses": {
          "title": "# Doses",
          "type": "string"
        },
        "animals_main": {
          "type": "string",
          "title": "Animals Main"
        },
        "animals_recovery": {
          "type": "string",
          "title": "Animals Recovery"
        }
      }
    }
  }
}

Options

{
"type": "object",
"fields": {
  "study_group_radio": {
    "type": "radio",
    "helper": "the number of selected will generate a table"
  },
  "study_group_table": {
    "type": "table",
    "id": "study_group_table",
    "label": "New table",
    "animate": true,
    "hideNone": true,
    "toolbarStyle": "button",
    "actionbarStyle": "right",
    "items": {
      "type": "tablerow"
    },
    "hideToolbarWithChildren": true,
    "datatables": {
      "paging": false,
      "lengthChange": false,
      "info": false,
      "searching": false,
      "ordering": true,
      "columns": [
        {
          "orderable": false,
          "name": "actions",
          "bSortable": false,
          "sName": "actions"
        }
      ],
      "bLengthChange": false,
      "bInfo": false,
      "aoColumns": [
        {
          "orderable": false,
          "name": "actions",
          "bSortable": false,
          "sName": "actions"
        }
      ],
      "bSort": true,
      "bPaginate": false,
      "bFilter": false
    },
    "dragRows": false,
    "showActionsColumn": false,
    "fields": {
      "dose": {
        "placeholder": "e.g. 1 mg/kg"
      }
    }
  }
}

Postrender

control.childrenByPropertyId["study_group_radio"].on("change", function() {
  var times = this.getValue();
  var value = 
  control.childrenByPropertyId["study_group_table"].getValue(value);

  if (value.length < times) {
      for (var i = value.length; i < times; i++) {
          console.log(i+1);
          value.push({
              "study_group": (i+1),
              "dose": "",
              "route": "",
              "doses": "",
              "animals_main": "",
              "animals_recovery": ""
          });
      }
  } else {
      while (value.length > times) {
          value.pop();
      }
  }
  console.log(value.study_group);
  control.childrenByPropertyId["study_group_table"].setValue(value);
});

I initially populated the table Options using the sample form builder. Unfortunately, I am facing challenges when manually inputting the code. Despite closely studying AlpacaJS's table documentation examples, what worked for others isn't working in my case!

Issues:

I am trying to automatically number the "study_group" column. While the postRender code correctly generates rows, they appear blank even after adding a value (i+1).

I am struggling to add placeholders using Alpaca. Currently, I have to manipulate the HTML manually after rendering. You can see my attempt on "dose" in Options.

Thank you for your help!

Answer №1

Your code appears to be accurate, but in order to address the issue of empty rows, you need to include a data object for the table and set it as empty.

"data": {
  "study_group_table": []
}

To resolve the placeholder problem, ensure that you encapsulate "fields" within the "items" property.

"items": {
  "fields": {
    "dose": {
      "placeholder": "e.g. 1 mg/kg"
    }
  }
}

Here is a helpful fiddle link for reference. Feel free to reach out if you require further assistance.

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

When it comes to assigning a background to a div using jQuery and JSON

I have been experimenting with creating a database using only JSON and surprisingly, it worked once I added a "js/" in the URL. However, my current issue lies with CSS. Let me elaborate. Here is the JSON data: [ { "title":"Facebook", ...

Creating a responsive DataTable filled from a $.ajax request is a straightforward process that can greatly

I am in the process of creating an application that retrieves a lot of data from a web-service query and populates a data table with it. The data shows up correctly and styled properly on desktop, but when I switch to viewing it on a mobile device, the dat ...

Utilize JSON categories to assign groups to TextFields or Selects according to a JSON data attribute

I have retrieved multiple JSON groups from an API, each containing one or more questions objects. My goal is to display each question along with its corresponding response in a MUI TextField or Select component, based on the value of QuestionType. Current ...

The result of the AJAX GET request is not defined

Despite seeing the response data in the browser's developer tool network tab, my AJAX GET response is showing as undefined. The AJAX request code I am using is as follows: $.ajax({ url: "https://api.leroymerlin.it/product-api-v2/v1/allStoreS ...

Retrieve the value of an AngularJS expression and display it in a window alert using AngularJS

Hey there, I am in the process of trying to display the value of an expression using AngularJs As a beginner in angular, I am working on figuring out how to retrieve the value of the expression either with an alert or in the console. I'm utilizing A ...

Troubleshooting the issue of missing object in Django template

I keep encountering the issue that json is not serializable. views.py def get_post(request): if request.is_ajax(): if request.method=="GET": craves = CraveData.objects.filter(person=request.user) print craves ...

How to Resolve Line Breaks in JSON using PHP

When parsing this JSON array, I need to extract the type object and place it in a new column called type2. However, I am encountering an issue with the foreach() function due to new lines in some of the JSON rows. How can I resolve this? The first row is ...

No IOException was found in the variable e

Related Question: Exception is NULL always I'm facing an unusual issue involving an IOException object that I haven't been able to resolve. The code in question is as follows: try { // This section may not be critical, but it could be rela ...

Gson throwing an error with an unparsable date format?

I am encountering an issue while attempting to parse JSON data with a Java POJO class. Below is the JSON and POJO structure: JSON: { "startDate": "2018-09-18T12:00:00+0000", "endDate": "2018-09-18T14:00:00+0000", "startDateAsDate": 153 ...

Building a Laravel PHP application that dynamically generates a custom JSON object fetched from the database and passes it from PHP to

I am faced with the task of generating a custom JSON object by organizing data retrieved from PHP in my Controller. I have full control over what information goes where and in what specific order. To accomplish this, it seems like I will need to go throug ...

Which is more suitable for storing data for boardgame session data: redisJSON or traditional redis?

Recently set up a Redis server for my backend using ioredis. I've discovered that if I want to store data in JSON format, I need to use the redisJSON module because hashes are only string typed and flat. However, since I'm only storing one objec ...

Expected end of file for JSON, received a comma instead

When I was in the process of converting my JSON file to .csv, I encountered an error stating "expected EOF, got ','" { "id": 22970, "type": "message", "date": "2018-11-24T21:08:21", "edited": "1970-01-01T03:00:00", "from": "lox", "fr ...

Deliver JSON data to APNS using the Push-Sharp framework

I want to send JSON data to an iOS device in the following format: {"aps":{"content-available":1}} Would it be better to use the AppleNotification class or the AppleNotificationPayLoad class? Can you please provide a sample code for reference? Currently, ...

A comprehensive guide on extracting Java objects from a JSON string

Is there a way to extract Java objects from a JSON string without knowing the object's name within the string? [ { "eqid": "c0001xgp", "magnitude": 8.8, "lng": 142.369, "src": "us", "datetime": "2011-03-11 04:46 ...

Exploring Data within Data Structures

I am currently making two JSON requests in my code: d3.json("path/to/file1.json", function(error, json) { d3.json("path/to/file2.json", function(error, json2) { }); }); The structure of json 2 looks like this: [ { "city" : "LA", "locati ...

Converting JSON arrays into structured arrays using Spark

Does anyone know how to convert an Array of JSON strings into an Array of structures? Sample data: { "col1": "col1Value", "col2":[ "{\"SubCol1\":\"ABCD\",\"SubCol ...

Parsing a multidimensional array using JSON arrays

My current challenge involves working with an array called jArray, which is structured as follows: {"users":[ { "user_id":6, "user_name":"Ted Vanderploeg", "email":"<a href="/cdn-cgi/l/email ...

Having difficulty accessing a JSON imported object beyond the FOR loop

I am facing an issue when trying to reference my imported JSON data. The problem arises when I attempt to access the data outside of a FOR loop, resulting in the error message "Uncaught TypeError: Cannot read property 'Tname' of undefined" The f ...

Parsing Json in Android does not provide any results

I have a JSON parsing Class structured like this: public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; public JSONParser() { } // method to fetch json from URL // using HTTP POST or GET method ...

What could be causing the version error when running 'pip install json'?

When attempting to install 'pip install json' in my command prompt, I encountered an error - ERROR: Could not find a version that satisfies the requirement json (from versions: none) ERROR: No matching distribution found for json. What steps shou ...