Select event throwing errors and CRUD call not saving

Select event throwing errors and CRUD call not saving

dynasoftdynasoft Posts: 446Questions: 69Answers: 3

I have 2 issues

  • I am getting errors when trying to select rows in a table w/ the mouse. The programme loads rows in table tblFileFieldTable via CRUDAdvFileField() which is called in JS $(document).ready function. The user then clicks a row and there's an ajax call via method CRUDAdvRepVal() to load another table (tblRepValTable) in a select event for tblFileFieldTable. The data loads fine in the 1st table, then clicking a row in it loads the data fine in the 2nd table but selecting rows causes error 'Cannot read property 'AdvBillingFileRepVal' of undefined' on line 'repValId = data[0].AdvBillingFileRepVal.id;' in the select event for the 2nd table.
  • After selecting a row in 1st table, the user needs to be able to add a new record to 2nd table but clicking add button and filling in values for the 2 fields fails to save the new data. The CRUD method CRUDAdvRepVal gets called but nothing gets saved. Please note the get call from the server requires 2 arguments (id and coCode) to get the correct records from CRUDAdvRepVal. Not sure if these get in the way when posting the new row to server.

I'd be very grateful for some help.

Here's my code:

  • js:

    function CRUDAdvFileField() {
    
        editor3 = new $.fn.dataTable.Editor({
    
            ajax: '/BillingFiles/CRUDAdvFileField/',
            table: '#tblFileFieldTable',
            fields: [ {
                    label: 'id',
                    name: 'id'
                }, {
                    label: 'FieldID',
                    name: 'FieldID'
                }, {
                    label: 'FieldType',
                    name: 'FieldType'
                }, {
                    label: 'FieldCode',
                    name: 'FieldCode'
                }, {
                    label: 'FieldName',
                    name: 'FieldName'
                }, {
                    label: 'FieldValue',
                    name: 'FieldValue'
                }
            ],
            i18n: {
                error: {
                    system: '@(lblo.lblError5)'
                }
            }
        });
    
        var dataTable = $('#tblFileFieldTable').DataTable( {
    
            order: [[0, 'desc']],
            columnDefs: [
                { 'bVisible': false, 'targets': 0 },
                { 'bVisible': false, 'targets': 1 },
                { 'bVisible': false, 'targets': 3 }
            ],
            dom: 'Bfrtip',
            ajax: {
                url: '/BillingFiles/CRUDAdvFileField/',
                type: 'GET',
                async: true,
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
            },
            columns: [
                { data: 'AdvBillingFileCol.id' },
                { data: 'AdvBillingFileCol.FieldID' },
                { data: 'AdvBillingFileCol.FieldType' ,
                    render: function ( data, type, row ) {
                        strTp = '';
                        if (data != null)
                        {
                            if (data == '0')
                            {
                                strTp = '@(lblo.lblDefault)';
                            }
                            else
                            {
                                strTp = '@(lblo.lblUserDefined)';
                            }
                        }
                        return strTp;
                    }
                },
                { data: 'AdvBillingFileCol.FieldCode' },
                { data: 'AdvBillingFileCol.FieldName' },
                { data: 'AdvBillingFileAddCol.FieldValue' }
            ],
            select: true,
               buttons: [
            ],
            language: {
                zeroRecords: '@(lblo.lblNoDataFound)',
                loadingRecords: '@(lblo.lblLoading)',
                emptyTable: '@(lblo.lblNoDataFound)'
            }
        });
    
        dataTable.on( 'select', function ( e, dt, type, indexes ) {
            if ( type === 'row' ) {
                var data = dataTable.rows( indexes ).data().toArray();
                colTy = data[0].AdvBillingFileCol.FieldType;
                colId = data[0].AdvBillingFileCol.FieldID;
                colCode = data[0].AdvBillingFileCol.FieldCode;
                colName = data[0].AdvBillingFileCol.FieldName;
                colValue = data[0].AdvBillingFileAddCol.FieldValue;
                CRUDAdvRepVal(colId, colCode);
            }
        } );
    
    }
    
    function CRUDAdvRepVal(id, coCode) {
    
        var editor4 = new $.fn.dataTable.Editor({
    
            ajax: '/BillingFiles/CRUDAdvRepVal/' + id.toString() + "/" + coCode,        //*HERE* - not saving new rows to db
            table: '#tblRepValTable',
            fields: [ {
                    label: '@(lblo.lblOriginalValue):',
                    name: 'FindValue'
                }, {
                    label: '@(lblo.lblNewValue):',
                    name: 'ReplValue'
                }
            ],
            i18n: {
                error: {
                    system: '@(lblo.lblError5)'
                }
            }
        });
    
        var dataTable = $('#tblRepValTable').DataTable( {
    
            destroy: true,
            order: [[0, 'desc']],
            columnDefs: [
                { 'bVisible': false, 'targets': 0 },
                { 'bVisible': false, 'targets': 1 },
                { 'bVisible': false, 'targets': 2 },
            ],
            dom: 'Bfrtip',
            ajax: {
                url: '/BillingFiles/CRUDAdvRepVal/' + id.toString() + "/" + coCode,
                type: 'GET',
                async: true,
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
            },
            columns: [
                { data: 'AdvBillingFileRepVal.id' },
                { data: 'AdvBillingFileRepVal.FieldID' },
                { data: 'AdvBillingFileCol.FieldCode' },
                { data: 'AdvBillingFileRepVal.FindValue' },
                { data: 'AdvBillingFileRepVal.ReplValue' }
            ],
            select: true,
               buttons: [
                { extend: 'create', editor: editor4 },
                { extend: 'edit', editor: editor4 },
                { extend: 'remove', editor: editor4 }
            ],
            language: {
                zeroRecords: '@(lblo.lblNoDataFound)',
                loadingRecords: '@(lblo.lblLoading)',
                emptyTable: '@(lblo.lblNoDataFound)'
            }
        });           
    
        dataTable.on( 'select', function ( e, dt, type, indexes ) {     //*HERE* - selecting row not showing the row cells in data var
            if ( type === 'row' ) {
                var data = dataTable.rows( indexes ).data().toArray();
                repValId = data[0].AdvBillingFileRepVal.id;
                repValFlid = data[0].AdvBillingFileRepVal.FieldID;
                repValCode = data[0].AdvBillingFileCol.FieldCode;
                repValFind = data[0].AdvBillingFileRepVal.FindValue;
                repValRepl = data[0].AdvBillingFileRepVal.ReplValue;
                lstRepValIndex = indexes;
            }
        } );            
    
    }
    

Answers

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3
    • server:

      public static DtResponse CRUDAdvFileField()
      {
          string strTp = string.Empty;
          Editor editor = null;
      
          try
          {
              HttpRequest formData = System.Web.HttpContext.Current.Request;
      
              using (Database db = new Database(SetGetDbType2, SetGetDbConnection))
              {
                  editor = new Editor(db, "AdvBillingFileCol").Model<BillingFilesAdvFileFieldDBModel>();
                  editor.Field(new Field("AdvBillingFileCol.id")
                      .Set(false)
                  );
                  editor.Field(new Field("AdvBillingFileCol.FieldID")
                      .SetFormatter(Format.IfEmpty(null))
                  );
                  editor.Field(new Field("AdvBillingFileCol.FieldType")
                      .SetFormatter(Format.IfEmpty(null))
                  );
                  editor.Field(new Field("AdvBillingFileCol.FieldCode")
                      .SetFormatter(Format.IfEmpty(null))
      
                  );
                  editor.Field(new Field("AdvBillingFileCol.FieldName")
                      //.SetFormatter(Format.IfEmpty(null))
                      .SetFormatter((val, host) => CommonUtilities.IsNullOrEmpty(val) == true ? "" : val)
                  );
                  editor.Field(new Field("AdvBillingFileAddCol.FieldValue")
                      .SetFormatter(Format.IfEmpty(null))
                  );
                  editor.LeftJoin("AdvBillingFileAddCol", "AdvBillingFileCol.FieldID", "=", "AdvBillingFileAddCol.id" );
                  editor.TryCatch(false);
                  editor.Debug(true);
                  editor.Process(formData);
                  editor.Data();
              }
          }
          catch (Exception ex)
          {
              MainUtilities.HandleErrorEvent(false, false, DateTime.Now, string.Empty, ex);
          }
          return editor.Data();
      }
      
      public static DtResponse CRUDAdvRepVal(long id, string coCode)
      {
          string strTp = string.Empty;
          Editor editor = null;
      
          try
          {
              HttpRequest formData = System.Web.HttpContext.Current.Request;
      
              using (Database db = new Database(SetGetDbType2, SetGetDbConnection))
              {
                  editor = new Editor(db, "AdvBillingFileRepVal").Model<BillingFilesAdvRepValDBModel>();
                  editor.Field(new Field("AdvBillingFileRepVal.id")
                      .Set(false)
                  );
                  editor.Field(new Field("AdvBillingFileRepVal.FieldID")
                      .SetFormatter(Format.IfEmpty(null))
                  );
                  editor.Field(new Field("AdvBillingFileCol.FieldCode")
                      .SetFormatter(Format.IfEmpty(null))
                  );
                  editor.Field(new Field("AdvBillingFileRepVal.FindValue")
                      //.SetFormatter(Format.IfEmpty(null))
                      .SetFormatter((val, host) => CommonUtilities.IsNullOrEmpty(val) == true ? null : val)
                  );
                  editor.Field(new Field("AdvBillingFileRepVal.ReplValue")
                      //.SetFormatter(Format.IfEmpty(null))
                      .SetFormatter((val, host) => CommonUtilities.IsNullOrEmpty(val) == true ? null : val)
                  );
                  editor.LeftJoin("AdvBillingFileCol", "AdvBillingFileRepVal.FieldID", "=", "AdvBillingFileCol.FieldID" );
                  editor.Where("AdvBillingFileRepVal.FieldID", null, "!=");
                  editor.Where("AdvBillingFileRepVal.FieldID", id);
                  editor.Where("AdvBillingFileCol.FieldCode", coCode);
                  editor.TryCatch(false);
                  editor.Debug(true);
                  editor.Process(formData);
                  editor.Data();
              }
          }
          catch (Exception ex)
          {
              MainUtilities.HandleErrorEvent(false, false, DateTime.Now, string.Empty, ex);
          }
          return editor.Data();
      }
      
  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954
    edited June 2019

    'Cannot read property 'AdvBillingFileRepVal' of undefined' on line 'repValId = data[0].AdvBillingFileRepVal.id;'

    That would seem to suggest that data[0] doesn't exist. The place to start is to see what data is. Try using a console.log statement, like this:

                var data = dataTable.rows( indexes ).data().toArray();
                console.log(data);
                repValId = data[0].AdvBillingFileRepVal.id;
    

    Kevin

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Hi
    Thanks. Yes the data isn't there when I debug or send to console, yet when I click to trigger the select event, I do so on a real row that holds data. That's weird.

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Btw, tblRepValTable is the correct name for the table as it gets populated correctly.

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Not sure if it is related yet but I raed colId = data[0].AdvBillingFileCol.FieldID; has a value of false when a numeric should be expected. Still checking...

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    I think that accounts for some of the issues but I can't see why it sets FieldID to a bool when the declaration in the Model is a long

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Data in Network tab of browser is fine

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    I tried your basic select event code here and it captures the rows.
    http://live.datatables.net/necakibe/1/edit

    Without seeing it in action it would be hard to say. Please post a link to your page or a test case replicating the issue.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Hi

    Thanks. It works selecting from the 1st table but not the other one when the code is virually the same apart from some field names and types. What would make a variable set to be one type in the model change to another once in the js? If I knew where to look for that would be good.

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Hi

    Would remote access via teamviewer be suitable? I can leave the access open at a certain time for you to access. Let me know.

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    I can do this anytime tomorrow Monday. Thank you.

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    I wonder if the select event for the second table is becoming abandoned since you are destroying destroy: true the Datatable. If you run that function multiple times you may be building up many select events. I would try turning of the select event before re-initializing the Datatable. Something like this:

        dataTable.off( 'select' );
    
        var dataTable = $('#tblRepValTable').DataTable( {
     
            destroy: true,
            .....
    

    Kevin

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Hi

    This has fixed the earlier issue where I had a strange behaviour where the select event would repeatedly trigger itself and data would return nothing. Thanks.

    An issue which is still a problem but that I have not documented before here is, clicking on editor's edit button brings up empty fields when selecting a row.

    Strangely, the issue of FieldID giving a value of false is still there. However, it goes away if I replace this:

                columns: [
                    { data: 'AdvBillingFileCol.id' },
                    { data: 'AdvBillingFileCol.FieldID' },
                    { data: 'AdvBillingFileCol.FieldType' }
    

    with this in the datatable code:

                columns: [
                    { data: 'AdvBillingFileCol.id' },
                    { data: 'AdvBillingFileCol.FieldID' },
                        render: function ( data, type, row ) {
                            return data;
                        }
                    },
                    { data: 'AdvBillingFileCol.FieldType' }
    

    Before the change you suggested doing the above did nothing as I tried it before so it appears it was something to do with the DT component. Why would the code { data: 'AdvBillingFileCol.FieldID' } not work? Anyway thanks.

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    should be:

    columns: [
    { data: 'AdvBillingFileCol.id' },
    { data: 'AdvBillingFileCol.FieldID' ,
    render: function ( data, type, row ) {
    return data;
    }
    },
    { data: 'AdvBillingFileCol.FieldType' }

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Sorry but putting that code makes no difference. I thought I'd seen the correct FieldIDs but its still showing false.

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Tries some more and it seems to be random, I get the FieldIDs but then values change to false 90% of times. I don't understand what's going on. This is frustrating.

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Tried...

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    I have fixed the 2nd issue reported at top of this thread by simplifying the code for CRUDAdvRepVal and the DT for table tblRepValTable: I removed the leftjoin.

    The issue with the select event is still there though. I wonder if the problem might be with the leftjoin in method CRUDAdvRepVal given above ? The fact it comes up ramdomly is strange.

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    We'd really need a test case showing the issue I'm afraid. It doesn't sounds like a particularly easy one to debug! If you can create an example of http://live.datatables.net , JSFiddle, CodePen or similar, I'll look into it.

    Allan

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3
    edited June 2019

    Just removed all leftjoin's but the problem remains. FieldID gets set to a bool. Not sure how building a "test case" and spending time doing it is better than giving you Tv access to VS.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @dynasoft ,

    If there's a test case it allows us to use debuggers on it - it saves everyone time in the long run.

    Cheers,

    Colin

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    I'll see what I can do

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Hi
    I think I fixed it. After the data loads in the tables, I had code that iterates through the tables to remove entries from the source tables to reflect the user's choice of fields. In one of the if statements I had a single equal sign instead of 2 which set the FieldID to a bool. Silly error but that proved hard to identify. Thanks to everyone who helped on this.

This discussion has been closed.