Add new row, BUT if data doesn't already exist in table

Add new row, BUT if data doesn't already exist in table

canwejustcodecanwejustcode Posts: 31Questions: 8Answers: 0

I have looked at some of the examples of adding a new row. However, is there a way to add a new row if the data doesn't already exist in the table?

Example:
In my table I have:

ID Make Model Year
1 Chevy 3500 2016
2 Ford F-350 2021
78 BMW X5 2022

I have a list box that shows the makes as well and has the ID set in that list box. So, when the user clicks on a
data value in the list box and want's to add it to the table, is there a way to loop through the table and if that selected ID doesn't exist, add it to the table, if it does exist, kick back a message that { item already exists } ?

Answers

  • allanallan Posts: 61,787Questions: 1Answers: 10,115 Site admin

    Sure - use the API. Specifically you could use rows() with a function selector to find out if any rows exist with the data you are matching against (then use any() to determine if any matched).

    Use row.add() to add the new row.

    I'm presuming it isn't with Editor you are doing this? If it is with Editor, then I'd use a server-side validator to check if a row exists.

    Allan

  • canwejustcodecanwejustcode Posts: 31Questions: 8Answers: 0

    No, I'm not using Editor. So, once the user clicks on the item, then clicks the button to add, the button function will first.

    1. look through the table to see if it exists or not in the table.
    2. if it exist, kick back a message { exists }
    3. if not, take that ID, call my API and get the data for that ID to add to the table row on the page. The table is already rendered on the page
  • kthorngrenkthorngren Posts: 20,348Questions: 26Answers: 4,776

    Basically repeating what Allan suggested:

    look through the table to see if it exists or not in the table.

    Use rows() with a row-selector of a function to find matching rows. Chain any() to see if it exists.

    if not, take that ID, call my API and get the data for that ID

    Use jQuery ajax() to send the ID to the server API.

    add to the table row on the page.

    Isn the success function use row.add() to add the row to the table.

    Kevin

  • canwejustcodecanwejustcode Posts: 31Questions: 8Answers: 0

    I'm looking at the selector now. I have the API call already built and working for another section of the app.

Sign In or Register to comment.