0

I am trying to implement an inline bubble editing in my data table...

I would like to implement it in many place. So I would like to create a global function. The following code is working fine:

"fnDrawCallback":function(){
    $('#data_table_list td').editable({
        params: function(params) {
            var pk = $(this).data('pk');
            var name = $(this).data('name');
            var data = {};
            data['field'] = name;
            data['value'] = params.value;
            data['id'] = pk;
            data['slug'] = slug;
            return data;
        },
        url: "{% url 'request_access' %}",
            success : function(data) {
            if (data.status == true) {
                toastr.success(data.msg);
            }
            else {
                toastr.error(data.msg);
            }
         },
         error: function () {
             toastr.error('Something went wrong');
         }
    });
},

But I have tried the following code to implement as a global function.

In my core.js file:

function python(params)
{
    var pk = $(this).data('pk');
    var name = $(this).data('name');
    var data = {};
    data['field'] = name;
    data['value'] = params.value;
    data['id'] = pk;
    data['slug'] = slug;
    return data;
}

And in my HTML page:

"fnDrawCallback":function(){
    $('#data_table_list td').editable({
        params: python(params),
        url: "{% url 'request_access' %}",
            success : function(data) {
            if (data.status == true) {
                toastr.success(data.msg);
            }
            else {
                toastr.error(data.msg);
            }
         },
         error: function () {
             toastr.error('Something went wrong');
         }
    });
},

But it shows an error:

ReferenceError: params is not defined

How can I create a global function?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anoop
  • 505
  • 8
  • 23
  • 1
    You want `params: python,` without the `()` – Phil Nov 12 '19 at 05:56
  • It's working ....But I want to inform you that I need to pass the slug variable to the python function .... – Anoop Nov 12 '19 at 06:07
  • @Phil could you help me to pass a variable to the function if we are not using paranthesis – Anoop Nov 12 '19 at 06:20
  • Your `python` function doesn't accept a `slug` argument. You should change it to `function python(params, slug) { ... }`. Then try `params: params => python(params, slug),` – Phil Nov 12 '19 at 06:30
  • @Phil if I pass I like that it again shows ReferenceError: params is not defined I am only define the "params" at here params => python(params, slug) – Anoop Nov 12 '19 at 06:37
  • @Phil my core.js like this no function python(params,slug) { } And my html script like this params: python(params,slug), – Anoop Nov 12 '19 at 06:40
  • That's completely different to what's in my comment. I suggest you read it again closely – Phil Nov 12 '19 at 07:48

0 Answers0