How can I generate table thead from serverside field

How can I generate table thead from serverside field

fatihfxfatihfx Posts: 9Questions: 3Answers: 0
edited July 2020 in Free community support

How can I create table head row from server side field name ?

// DB table to use
$table = 'raporlar_gonderilen';

// Table's primary key
$primaryKey = 'rapor_gonderilen_id';

$column_array_auto_index = 0; 
$caat = $column_array_auto_index;
$columns = array(
  array( 'db' => '`t1`.`rapor_gonderilen_id`', 'dt' => $caat++, 'field' => 'Table Head 1' ),
  array( 'db' => '`t1`.`rapor_gonderilen_id`', 'dt' => $caat++, 'field' => 'Table Head 2' ),
  array( 'db' => '`t1`.`gonderim_tarihi`', 'dt' => $caat++, 'field' => 'Table Head Example' ),
  array( 'db' => '`t1`.`gonderim_yapilan_epostalar`', 'dt' => $caat++, 'field' => 'bla bla' ),
  array( 'db' => '`t1`.`gonderim_yapilan_user_id_ler`', 'dt' => $caat++, 'field' => 'test' )
);

Answers

  • kthorngrenkthorngren Posts: 21,351Questions: 26Answers: 4,955

    There are lots of threads with this question. See if either of these help:
    https://datatables.net/forums/discussion/33886
    https://datatables.net/forums/discussion/60722

    Kevin

  • fatihfxfatihfx Posts: 9Questions: 3Answers: 0
    edited July 2020

    Thank you for your answer but I can't see any info about my question.

    Need to table head like in array: Table Head 1 - Table Head 2 - Table Head Example - ...

    here is my array

    $columns = array(
      array( 'db' => '`t1`.`rapor_gonderilen_id`', 'dt' => $caat++, 'field' => 'Table Head 1' ),
      array( 'db' => '`t1`.`rapor_gonderilen_id`', 'dt' => $caat++, 'field' => 'Table Head 2' ),
      array( 'db' => '`t1`.`gonderim_tarihi`', 'dt' => $caat++, 'field' => 'Table Head Example' ),
      array( 'db' => '`t1`.`gonderim_yapilan_epostalar`', 'dt' => $caat++, 'field' => 'bla bla' ),
      array( 'db' => '`t1`.`gonderim_yapilan_user_id_ler`', 'dt' => $caat++, 'field' => 'test' )
    );
    
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin

    See this FAQ.

    With server-side processing, you’d need to make one Ajax call to get the column information and then another to get the first page of data for the table.

    Allan

  • fatihfxfatihfx Posts: 9Questions: 3Answers: 0

    If I Understand, it's not possible what I want. Thank you.

  • fatihfxfatihfx Posts: 9Questions: 3Answers: 0

    Can I use field names instead of targets index ?

    'field' => 'table Head 1'

    like as

    targets : 'table head 1'

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin

    It is perfectly possible to do, but as the FAQ says, you need to make an extra Ajax call to get the data.

    The options for the targets parameter in the columns are defined in columnDefs.targets.

    Allan

  • ferencvarosferencvaros Posts: 9Questions: 2Answers: 0

    So how is it implemented? I see that you can target each column (for example: 0 would be column 1), but how do you give it a name and then show it in the final table?

    "columnDefs": [ {  "targets": 0 } ]
    
  • kthorngrenkthorngren Posts: 21,351Questions: 26Answers: 4,955
    edited March 2022

    @ferencvaros The -option columnDefs.targets` docs provide the available options:

    • 0 or a positive integer - column index counting from the left
    • A negative integer - column index counting from the right
    • A string - class name will be matched on the TH for the column (without a leading .)
    • The string "_all" - all columns (i.e. assign a default)

    how do you give it a name and then show it in the final table?

    Are you referring to the header titles? If so use columns.title.

    Kevin

  • ferencvarosferencvaros Posts: 9Questions: 2Answers: 0

    Sorry for the confusion. I'm referring to the <th> content in <thead>.

  • kthorngrenkthorngren Posts: 21,351Questions: 26Answers: 4,955

    @ferencvaros

    I'm referring to the <th> content in <thead>.

    Use columns.title to have Datatables build the header.

    Kevin

Sign In or Register to comment.