Issue with adding values via formatter to editor dropdown

Issue with adding values via formatter to editor dropdown

ziv@kpmbro.comziv@kpmbro.com Posts: 73Questions: 28Answers: 4
edited July 2016 in General

Hi

I wanted to set a string next to each dropdown value in my table, so i used the getFormatter function.
for example instead of:
"1"
i draw
"1 :: TRUE"

same with

"0"
i draw
"0 :: FALSE"

The problem is that when i want to update that it doesn't work because the value is "1 :: TRUE" instead of "1" so the editor cant handle it(of course).

What is the best way to add string to a value ?

Thanks

Answers

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    I would suggest using a client-side renderer so you aren't modifying the raw data, but just the display output.

    Allan

  • ziv@kpmbro.comziv@kpmbro.com Posts: 73Questions: 28Answers: 4

    Hi Allan.

    Right now i am sending something like this js array to the columns section:

    [
    {
    "data":"offers_main_table.title"
    },
    {
    "data":"offers_main_table.network_campaign_id"
    }
    ]

    I try this

    [
    {
    "data":"offers_main_table.title"
    },
    {
    "data":"offers_main_table.network_campaign_id",
    "render":"test"
    }
    ]

    and it doesnt work.

    How can i do something like:

    render: function ( data, type, row ) {
        return 'test'+ data;
    }
    

    from the columns js array(the json above)?

    thanks

  • ziv@kpmbro.comziv@kpmbro.com Posts: 73Questions: 28Answers: 4

    Ok i got it , I wrote it hard coded
    [
    {
    "data":"offers_main_table.network_campaign_id"
    },
    {
    "data":"offers_main_table.advertiser_id",
    render: function ( data, type, row ) {
    return data +'xxxx ';
    }
    ]

    but how can i do it dynamically ,can i do it with some kind of loop with conditions ?

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    but how can i do it dynamically ,can i do it with some kind of loop with conditions ?

    I don't understand what you mean by dynamically I'm afraid. Do you mean you want conditions in the renderer? If so, then sure, you can have whatever conditions you want in the renderer.

    Allan

  • ziv@kpmbro.comziv@kpmbro.com Posts: 73Questions: 28Answers: 4
    edited July 2016

    Hi Allan

    1. I meant that i am building the columns in a generic way for all the tables, i am running a php script that builds the columns array and i print that at the the table js, for example:
      <?php $columns_js_array = array(); foreach ($table_params['fields_array'] as $key => $value) { $columns_js_array[]= array("data"=> $value['field']); } ?>
    var table = $('#example').DataTable({ columns: [{ <?php echo $columns_js_array?> }] });

    so thats why i cant write each field with the render function.
    any thoughts about that?

    1. I noticed that even when i use the column render(client side) i still get the same result such as using the getFormatter function, the html inside the td is still the same.
      like:
      <td>1 :: TRUE</td> (i added a pic above), this still confuse the editor update because i only want to pass the "1".

    i saw this example online
    https://datatables.net/examples/api/form.html

    thanks

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    You would need to find some way of being able to define a Javascript rendering function from your PHP loop. Possibly just have the functions named in Javascript and have your PHP output the function names (i.e. the PHP would also need to know the Javascript function names).

    this still confuse the editor update because i only want to pass the "1".

    Yup - you need to do the rendering client-side as I said above. If you embed the rendered value into the data, then editor won't be able to read the correct value.

    Allan

This discussion has been closed.