Datatables external link and money currency, Thank You

Datatables external link and money currency, Thank You

kachaqkachaq Posts: 5Questions: 1Answers: 0
edited May 2021 in Buttons

  1. Im trying to add external link to "customer_id" link example has to be like this /edit-customer.php?customer_id=$customer_id(which is link to original customer id)

  2. I want to add '$' and money format has to be $ 1,250.00. this my code

// SCRIPT
$(document).ready(function(){
 var dataTable = $('#customer_table').DataTable({  
  "processing" : true,
  "serverSide" : true,
  "order" : [],
  "ajax" : {
   url:"serverside/ajax-users.php",
   type:"POST"
  }, 
 }); 
 $('#customer_table').on('draw.dt', function(){
    $('#customer_table').Tabledit({
        url:'serverside/action.php',
        dataType:'json',
        columns:{
            identifier : [0, 'customer_id'],
            editable:[[1, 'customer_store', '{"1":"B2C","2":"B2B"}'],[2, 'customer_name'], [3, 'customer_address'], [4, 'customer_phone'], [5, 'customer_email'], [6, 'customer_status', '{"1":"Inactive","2":"Active"}']]
        },       
        restoreButton:false,
        onSuccess:function(data,textStatus, jqXHR)
        {
            if (data.action == 'delete') 
            {
                $('#' + data.customer_id).remove();
                $('#customer_table').DataTable().ajax.reload();
            }     
        }
    });
}); 
});
//SCRIPT

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

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

    Is this using Editor? Or some other editor?

    Colin

  • kachaqkachaq Posts: 5Questions: 1Answers: 0

    its not editor, normal one. if you need all information is here. serverside

    //AJAX-USER

    <?inlcude your db

    $column = array("customer_id", "customer_store", "customer_name", "customer_address", "customer_phone", "customer_email", "customer_status", "customer_date", "customer_order", "customer_sale");

    $query = "SELECT * FROM customers ";

    if(isset($_POST["search"]["value"]))
    {
    $query .= '
    WHERE customer_id LIKE "%'.$_POST["search"]["value"].'%"
    OR customer_name LIKE "%'.$_POST["search"]["value"].'%"
    OR customer_store LIKE "%'.$_POST["search"]["value"].'%"
    OR customer_address LIKE "%'.$_POST["search"]["value"].'%"
    OR customer_phone LIKE "%'.$_POST["search"]["value"].'%"
    OR customer_email LIKE "%'.$_POST["search"]["value"].'%"
    OR customer_sale LIKE "%'.$_POST["search"]["value"].'%"
    OR customer_status LIKE "%'.$_POST["search"]["value"].'%"
    ';
    }

    if(isset($_POST["order"]))
    {
    $query .= 'ORDER BY '.$column[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';
    }
    else
    {
    $query .= 'ORDER BY customer_date DESC ';
    }
    $query1 = '';

    if($_POST["length"] != -1)
    {
    $query1 = 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
    }

    $statement = $connect->prepare($query);

    $statement->execute();

    $number_filter_row = $statement->rowCount();

    $statement = $connect->prepare($query . $query1);

    $statement->execute();

    $result = $statement->fetchAll();

    $data = array();

    foreach($result as $row)
    {
    $sub_array = array();

    $sub_array[] = $row['customer_id'];
    
    $sub_array[] = $row['customer_store'];
    
    $sub_array[] = $row['customer_name'];
    
    $sub_array[] = $row['customer_address'];
    
    $sub_array[] = $row['customer_phone'];
    
    $sub_array[] = $row['customer_email'];
    
    $sub_array[] = $row['customer_status'];
    
    $sub_array[] = $row['customer_date'];
    
    $sub_array[] = $row['customer_order'];
    
    $sub_array[] = $row['customer_sale'];
    
    $sub_array[] = $row['customer_detail'];
    
    $data[] = $sub_array;
    

    }

    function count_all_data($connect)
    {
    $query = "SELECT * FROM customers";
    $statement = $connect->prepare($query);
    $statement->execute();
    return $statement->rowCount();
    }

    $output = array(
    'draw' => intval($_POST['draw']),
    'recordsTotal' => count_all_data($connect),
    'recordsFiltered' => $number_filter_row,
    'data' => $data
    );

    echo json_encode($output);

    //AJAX-USER


    //ACTION

    if ($_POST['action']== 'edit') {

    $data = array(
        ':customer_store' => $_POST['customer_store'],
        ':customer_name' => $_POST['customer_name'],
        ':customer_address' => $_POST['customer_address'],
        ':customer_phone' => $_POST['customer_phone'],
        ':customer_email' => $_POST['customer_email'],
        ':customer_status' => $_POST['customer_status'],
        ':customer_id'    => $_POST['customer_id']
    );
    
    $query = "
        UPDATE customers
        SET customer_store = :customer_store,
        customer_name = :customer_name,
        customer_address = :customer_address,
        customer_phone = :customer_phone,
        customer_email = :customer_email,
        customer_status = :customer_status
        WHERE customer_id = :customer_id
    ";
    $statement = $connect->prepare($query);
    $statement->execute($data);
    echo json_encode($_POST);
    

    }

    if ($_POST['action'] == 'delete')
    {
    $query = "
    DELETE FROM customers
    WHERE customer_id = '".$_POST["customer_id"]."'
    ";
    $statement = $connect->prepare($query);
    $statement->execute();
    echo json_encode($_POST);
    }

    //ACTION

  • kthorngrenkthorngren Posts: 21,321Questions: 26Answers: 4,948
    Answer ✓

    See the columns.render docs for an example of creating a link. You can use a number renderer to format the number output.

    Kevin

  • kachaqkachaq Posts: 5Questions: 1Answers: 0

    Thanx Kevin I solved, external link like this. but i have still problem for money currency.

    "columnDefs": [ {
    "targets": 10,
    "data": 0,
    "render": function ( data, type, row, meta ) {
    return '<button class="btn btn-light btn-sm"><a href="/edit-customer.php?customer_id='+data+'">Detail</a></button>';
    }
    } ]

    for money currency.

    data: 9,
    render: $.fn.dataTable.render.number( ',', '.', 2, '$' ),

    i dont know where am i gonna put this. bcs when i added under columnDefs is working but this time hyperlink is not showing.

  • kachaqkachaq Posts: 5Questions: 1Answers: 0

    "columns":[{
    targets : 4,
    dataSrc : null,
    render: $.fn.dataTable.render.number( ',', '.', 2, '$ ' )
    }],

    can you please help me with this. I am putting this code and ID numbers shows money format. i wanna change column-4 .

  • kachaqkachaq Posts: 5Questions: 1Answers: 0

    this is the money rendering solution. thank you

    let dataTable = $('#product_table').DataTable({
    "processing" : true,
    "serverSide" : true,
    "order" : [],
    "ajax" : {
    url:"serverside/ajax-products.php",
    type:"POST"
    },
    "columnDefs": [
    {
    "targets": 6,
    "data": 0,
    "render": function ( data, type, row, meta ) {
    return '<center><button class="btn btn-sm"><a href="/edit-product.php?product_id='+data+'">Detail</a></button></center>';},
    },
    {
    "targets": 4,
    "data": 4,
    "render": $.fn.dataTable.render.number(',', '.', 2, '$ ')
    }
    ],
    });

This discussion has been closed.