1
votes
in javascript file:
```
function onSearch(filter, data, parameter) {
    this.filter = filter;
    var row = 1;
    $.ajax({
        type: "POST",
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: url,
        cache: false,
        success: function (msg) {
            var json = JSON.parse(msg.d);
            var html = "";
            $.each(json, function (index, obj) {
                //html += ""
                //    + "" + row.toString() + ""
                //    + "" + obj.Material_No + ""
                //    + "" + obj.Material_Name + ""
                //    + "";
                html += parameter;
                row++;
            });
            var table = $('#tbDetails').DataTable();
            table.destroy();
            $("#tbDetails tbody").html(html);
            $('#tbDetails').DataTable({
                "destroy": true,
                "order": [[0, "asc"]]
            });

            $('#myModal-1').modal('toggle');
        },
        error: function (e) {
            alert(e.responseText);
        }
        
    });

    return false;
};
```

in html file :

```
var parameter = ""
+ "" + row.toString() + ""
+ "" + obj.Material_No + ""
+ "" + obj.Material_Name + ""
+ "";
onSearch(filter, data, parameter);
```

Please help, i want to the variable parameter to known object, as i comment code before

1

1 Answers

0
votes

You want to tell onSearch how to format the data that it fetches, but you can't reference row and obj directly since they're not available until the network call completes. What you're looking for is a callback function. You're actually already using callbacks - you pass a callback function as the second argument to $.each, and the success parameter of $.ajax.

You'll need to change onSearch to accept a callback function as the third parameter, and then pass it when you call onSearch, something like:

function onSearch(filter, data, callback) {
  // ...
  $.ajax({
    // ...
    success: function (msg) {
      var json = JSON.parse(msg.d);
      var html = callback(json);
      // ...
    },
  });
}

onSearch(filter, data, function (json) {
  var html = "";

  $.each(json, function (index, obj) {
    html += ""
    + "" + obj.toString() + ""
    + "" + obj.Material_No + ""
    + "" + obj.Material_Name + ""
    + "";
  });

  return html;
});

There's no variable named row, so I'm assuming that should be obj. You may need to edit accordingly.